list
The operations of lists, which every module has by the name List
without importing it: List.map, List.foldl, List.length. The
type and its tags are builtin’s, [a, b] its notation, and x :: xs
and xs ++ ys the prelude’s.
map
map : {a b : Type} -> (a -> b) -> List a -> List bfoldl
foldl : {a b : Type} -> (b -> a -> b) -> b -> List a -> breverse
reverse : {a : Type} -> List a -> List afoldr
foldr : {a b : Type} -> (a -> b -> b) -> b -> List a -> bFrom the last element: f x1 (f x2 (… z)). (By way of reverse, so
a long list does not go as deep as it is long.)
keepIf
keepIf : {a : Type} -> (a -> Bool) -> List a -> List alength
length : {a : Type} -> List a -> Int>>> length (map (\x -> x * 2) (Cons 1 (Cons 2 (Cons 3 Nil))))3append
append : {a : Type} -> List a -> List a -> List axs ++ ys, by a name.
>>> append [1, 2] [3]Cons 1 (Cons 2 (Cons 3 Nil))concatMap
concatMap : {a b : Type} -> (a -> List b) -> List a -> List bnull
null : {a : Type} -> List a -> Boolhead
head : {a : Type} -> List a -> Maybe atail
tail : {a : Type} -> List a -> Maybe (List a)last
last : {a : Type} -> List a -> Maybe ainit
init : {a : Type} -> List a -> Maybe (List a)All but the last element, if there is one: last the other way.
>>> map init [[1, 2, 3], []]Cons (Just (Cons 1 (Cons 2 Nil))) (Cons Nothing Nil)take
take : {a : Type} -> Int -> List a -> List aThe first n elements (all, if there are fewer).
drop
drop : {a : Type} -> Int -> List a -> List aWhat is left after the first n.
zipWith
zipWith : {a b c : Type} -> (a -> b -> c) -> List a -> List b -> List czip
zip : {a b : Type} -> List a -> List b -> List (a, b)any
any : {a : Type} -> (a -> Bool) -> List a -> Boolall
all : {a : Type} -> (a -> Bool) -> List a -> Boolfind
find : {a : Type} -> (a -> Bool) -> List a -> Maybe aelem
elem : {a : Type} -> {{eq : Equality a}} -> a -> List a -> BoolWhether x is in the list.
>>> elem 2 [1, 2, 3] && not (elem "d" ["a", "b", "c"])Truelookup
lookup : {k v : Type} -> {{eq : Equality k}} -> k -> List (k, v) -> Maybe vThe value paired with the first key equal to k.
>>> lookup "b" [MkPair "a" 1, MkPair "b" 2]Just 2sum
sum : List Int -> Intproduct
product : List Int -> Intrange
range : Int -> Int -> List Inta, a + 1, …, b.
replicate
replicate : {a : Type} -> Int -> a -> List asortOn
sortOn : {a k : Type} -> {{ord : Order k}} -> (a -> k) -> List a -> List aSorted by key’s Order, stably: of two whose keys are equal, the
first first. The runs it has, going up or strictly down, merged two
by two, each two as soon as they are read (a sorted list is one run,
so is gone through once).
>>> sortOn (\p -> fst p) [MkPair 2 "a", MkPair 1 "b", MkPair 2 "c", MkPair 1 "d"]Cons (MkPair 1 "b") (Cons (MkPair 1 "d") (Cons (MkPair 2 "a") (Cons (MkPair 2 "c") Nil)))>>> sortOn (\x -> 0 - x) [1, 3, 2, 3, 5, 4]Cons 5 (Cons 4 (Cons 3 (Cons 3 (Cons 2 (Cons 1 Nil)))))sort
sort : {a : Type} -> {{ord : Order a}} -> List a -> List aSorted by the elements’ Order, stably.
>>> sort [3, 1, 2, 1]Cons 1 (Cons 1 (Cons 2 (Cons 3 Nil)))concat
concat : {a : Type} -> List (List a) -> List axs ++ ys ++ …, each list in turn.
>>> concat [[1, 2], [], [3]]Cons 1 (Cons 2 (Cons 3 Nil))index
index : {a : Type} -> List a -> Int -> Maybe aThe element at i, from 0, if there is one.
>>> map (index [10, 20, 30]) [0, 2, 3, -1]Cons (Just 10) (Cons (Just 30) (Cons Nothing (Cons Nothing Nil)))indexed
indexed : {a : Type} -> List a -> List (Int, a)Each element with its index, from 0.
>>> indexed ["a", "b"]Cons (MkPair 0 "a") (Cons (MkPair 1 "b") Nil)indexWhere
indexWhere : {a : Type} -> (a -> Bool) -> List a -> Maybe IntWhere the first element p holds of is, from 0, if one is.
>>> indexWhere (\x -> x > 1) [1, 2, 3]Just 1count
count : {a : Type} -> (a -> Bool) -> List a -> IntHow many elements p holds of.
>>> count (\x -> x % 2 == 0) [1, 2, 3, 4]2mapMaybe
mapMaybe : {a b : Type} -> (a -> Maybe b) -> List a -> List bWhat f gives Just of, in order: keepIf and map at once.
>>> mapMaybe (\x -> if x > 1 then Just (x * 10) else Nothing) [1, 2, 3]Cons 20 (Cons 30 Nil)partition
partition : {a : Type} -> (a -> Bool) -> List a -> (List a, List a)Those p holds of, and those it does not, each in order.
>>> partition (\x -> x % 2 == 0) [1, 2, 3, 4, 5]MkPair (Cons 2 (Cons 4 Nil)) (Cons 1 (Cons 3 (Cons 5 Nil)))takeWhile
takeWhile : {a : Type} -> (a -> Bool) -> List a -> List aThe elements up to the first p does not hold of.
>>> takeWhile (\x -> x < 3) [1, 2, 3, 1]Cons 1 (Cons 2 Nil)dropWhile
dropWhile : {a : Type} -> (a -> Bool) -> List a -> List aWhat is left from the first element p does not hold of.
>>> dropWhile (\x -> x < 3) [1, 2, 3, 1]Cons 3 (Cons 1 Nil)span
span : {a : Type} -> (a -> Bool) -> List a -> (List a, List a)takeWhile p xs and dropWhile p xs, gone through once.
>>> span (\x -> x < 3) [1, 2, 3, 1]MkPair (Cons 1 (Cons 2 Nil)) (Cons 3 (Cons 1 Nil))split
split : {a : Type} -> (at : Int) -> List a -> (List a, List a)take at xs and drop at xs, gone through once.
>>> split 2 [1, 2, 3]MkPair (Cons 1 (Cons 2 Nil)) (Cons 3 Nil)group
group : {a : Type} -> {{eq : Equality a}} -> List a -> List (List a)The runs of equal elements next to each other, in order.
>>> group [1, 1, 2, 3, 3, 1]Cons (Cons 1 (Cons 1 Nil)) (Cons (Cons 2 Nil) (Cons (Cons 3 (Cons 3 Nil)) (Cons (Cons 1 Nil) Nil)))unique
unique : {a : Type} -> {{eq : Equality a}} -> List a -> List aThe elements, each but the first of those equal left out, in order (by as many comparisons as the length times the elements kept).
>>> unique [3, 1, 3, 2, 1]Cons 3 (Cons 1 (Cons 2 Nil))intersperse
intersperse : {a : Type} -> a -> List a -> List aThe elements, with sep between each two.
>>> intersperse 0 [1, 2, 3]Cons 1 (Cons 0 (Cons 2 (Cons 0 (Cons 3 Nil))))unzip
unzip : {a b : Type} -> List (a, b) -> (List a, List b)The firsts and the seconds, zip undone.
>>> unzip [MkPair 1 "a", MkPair 2 "b"]MkPair (Cons 1 (Cons 2 Nil)) (Cons "a" (Cons "b" Nil))scanl
scanl : {a b : Type} -> (b -> a -> b) -> b -> List a -> List bz and each step of foldl f z xs, in order.
>>> scanl (\a b -> a + b) 0 [1, 2, 3]Cons 0 (Cons 1 (Cons 3 (Cons 6 Nil)))minimum
minimum : {a : Type} -> {{ord : Order a}} -> List a -> Maybe aThe first of the least, by the elements’ Order, if there are any.
>>> minimum [3, 1, 2]Just 1maximum
maximum : {a : Type} -> {{ord : Order a}} -> List a -> Maybe aThe first of the greatest, by the elements’ Order, if there are
any.
>>> map maximum [[1, 3, 2], []]Cons (Just 3) (Cons Nothing Nil)