Lists

Lists are used to store multiple values of the same type.

List Operations

这些操作并不修改原列表的值,返回的是其 copy.

1
2
3
4
5
6
7
8
9
10
11
head :: [a] -> a            -- returns the first element
last :: [a] -> a -- returns the last element
tail :: [a] -> [a] -- returns everything except the first element
init :: [a] -> [a] -- returns everything except the last element
take :: Int -> [a] -> [a] -- returns the n first elements
drop :: Int -> [a] -> [a] -- returns everything except the n first elements
(++) :: [a] -> [a] -> [a] -- lists are catenated with the ++ operator
(!!) :: [a] -> Int -> a -- lists are indexed with the !! operator
reverse :: [a] -> [a] -- reverse a list
null :: [a] -> Bool -- is this list empty?
length :: [a] -> Int -- the length of a list

Maybe Type

约等于 Rust 里的 Option<T>

1
2
3
Maybe Int
-- <=>
Nothing, Just 0, Just 114514, ...

Either Type

1
2
3
Either String Int
-- <=>
Left "asdf", Right 1, Right 67, Left "qweohuff 0", ...