Skip to content
heff

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 b

foldl

foldl : {a b : Type} -> (b -> a -> b) -> b -> List a -> b

reverse

reverse : {a : Type} -> List a -> List a

foldr

foldr : {a b : Type} -> (a -> b -> b) -> b -> List a -> b

From 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 a

length

length : {a : Type} -> List a -> Int
>>> length (map (\x -> x * 2) (Cons 1 (Cons 2 (Cons 3 Nil))))
3

append

append : {a : Type} -> List a -> List a -> List a

xs ++ 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 b

null

null : {a : Type} -> List a -> Bool
head : {a : Type} -> List a -> Maybe a

tail

tail : {a : Type} -> List a -> Maybe (List a)

last

last : {a : Type} -> List a -> Maybe a

init

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 a

The first n elements (all, if there are fewer).

drop

drop : {a : Type} -> Int -> List a -> List a

What is left after the first n.

zipWith

zipWith : {a b c : Type} -> (a -> b -> c) -> List a -> List b -> List c

zip

zip : {a b : Type} -> List a -> List b -> List (a, b)

any

any : {a : Type} -> (a -> Bool) -> List a -> Bool

all

all : {a : Type} -> (a -> Bool) -> List a -> Bool

find

find : {a : Type} -> (a -> Bool) -> List a -> Maybe a

elem

elem : {a : Type} -> {{eq : Equality a}} -> a -> List a -> Bool

Whether x is in the list.

>>> elem 2 [1, 2, 3] && not (elem "d" ["a", "b", "c"])
True

lookup

lookup : {k v : Type} -> {{eq : Equality k}} -> k -> List (k, v) -> Maybe v

The value paired with the first key equal to k.

>>> lookup "b" [MkPair "a" 1, MkPair "b" 2]
Just 2

sum

sum : List Int -> Int

product

product : List Int -> Int

range

range : Int -> Int -> List Int

a, a + 1, …, b.

replicate

replicate : {a : Type} -> Int -> a -> List a

sortOn

sortOn : {a k : Type} -> {{ord : Order k}} -> (a -> k) -> List a -> List a

Sorted 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 a

Sorted 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 a

xs ++ ys ++ …, each list in turn.

>>> concat [[1, 2], [], [3]]
Cons 1 (Cons 2 (Cons 3 Nil))

index

index : {a : Type} -> List a -> Int -> Maybe a

The 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 Int

Where the first element p holds of is, from 0, if one is.

>>> indexWhere (\x -> x > 1) [1, 2, 3]
Just 1

count

count : {a : Type} -> (a -> Bool) -> List a -> Int

How many elements p holds of.

>>> count (\x -> x % 2 == 0) [1, 2, 3, 4]
2

mapMaybe

mapMaybe : {a b : Type} -> (a -> Maybe b) -> List a -> List b

What 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 a

The 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 a

What 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 a

The 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 a

The 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 b

z 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 a

The first of the least, by the elements’ Order, if there are any.

>>> minimum [3, 1, 2]
Just 1

maximum

maximum : {a : Type} -> {{ord : Order a}} -> List a -> Maybe a

The first of the greatest, by the elements’ Order, if there are any.

>>> map maximum [[1, 3, 2], []]
Cons (Just 3) (Cons Nothing Nil)