empty list constructor
[] :: [a]
non-empty list constructor
(:) :: a -> [a] -> [a]
head - returns the first value of a list
head :: [a] -> a
last - returns the last value of a list
last :: [a] -> a
tail - returns a list without the first item
tail :: [a] -> [a]
init - returns a list without the last item
init :: [a] -> [a]
xs !! i - return the element at an index i in list xs
(!!) :: Int -> [a] -> a
take n xs - return new list containing n first elements of the list xs
take :: Int -> [a] -> [a]
map :: (a -> b) -> [a] -> [b]
filter :: (a -> Bool) -> [a] -> [a]
(++) :: [a] -> [a]
concat :: [[a]] -> [a]
[a]
is equivalent to [] a
.[]
constructs the empty list.[]
in a function definition LHS, e.g. f [] = ...
, is the empty list pattern.x:xs
constructs a list where an element x
is prepended to the list xs
f (x:xs) = ...
is a pattern match for a non-empty list where x
is the head and xs
is the tail.f (a:b:cs) = ...
and f (a:(b:cs)) = ...
are the same. They are a pattern match for a list of at least two elements where the first element is a
, the second element is b
, and the rest of the list is cs
.f ((a:as):bs) = ...
is NOT the same as f (a:(as:bs)) = ...
. The former is a pattern match for a non-empty list of lists, where a
is the head of the head, as
is the tail of the head, and bs
is the tail.f (x:[]) = ...
and f [x] = ...
are the same. They are a pattern match for a list of exactly one element.f (a:b:[]) = ...
and f [a,b] = ...
are the same. They are a pattern match for a list of exactly two elements.f [a:b] = ...
is a pattern match for a list of exactly one element where the element is also a list. a
is the head of the element and b
is the tail of the element.[a,b,c]
is the same as (a:b:c:[])
. Standard list notation is just syntactic sugar for the (:)
and []
constructors.all@(x:y:ys)
in order to refer to the whole list as all
(or any other name you choose) instead of repeating (x:y:ys)
again.