Data.List
Singly-linked list operations.
The List(a) type and its Nil/Cons constructors are wired into the language (list literals, deriving); this module is the function surface over them. The prelude opens it, so these names are in scope unqualified everywhere; a project module reaches them with import Data.List. The container-generic queries (length, sum, all, find, and friends) live in Data.Foldable and work on any Foldable, this type included.
Functions and Values
singleton
singleton : forall a. (a) -> List(a)
The one-element list Cons(x, Nil).
is_nil
is_nil : forall a. (List(a)) -> Bool
True for the empty list.
head
head : forall a. (List(a)) -> Option(a)
The first element as Some, or None when the list is empty.
tail
tail : forall a. (List(a)) -> List(a)
Everything after the first element (Nil for the empty list).
last
last : forall a. (List(a)) -> Option(a)
The final element as Some, or None when the list is empty.
nth
nth : forall a. (Int, List(a)) -> Option(a)
The element at index n (zero-based) as Some, or None if out of range.
take
take : forall a. (Int, List(a)) -> List(a)
The first n elements (fewer if the list is shorter).
drop
drop : forall a. (Int, List(a)) -> List(a)
The list with its first n elements removed.
take_while
take_while : forall e0 a. ((a) -> Bool ! {e0}, List(a)) -> List(a) ! {e0}
The longest prefix of elements satisfying p.
drop_while
drop_while : forall e0 a. ((a) -> Bool ! {e0}, List(a)) -> List(a) ! {e0}
The suffix remaining after take_while(p, xs).
split_at
split_at : forall a. (Int, List(a)) -> (List(a), List(a))
(take(n, xs), drop(n, xs)) in one pass of intent.
map
map : forall e0 a b. ((b) -> a ! {e0}, List(b)) -> List(a) ! {e0}
Apply f to every element, preserving order and length.
map(\(x) -> x + 1, [1, 2, 3])
filter
filter : forall e0 a. ((a) -> Bool ! {e0}, List(a)) -> List(a) ! {e0}
Keep the elements satisfying p.
filter(\(x) -> x > 1, [1, 2, 3])
foldr
foldr : forall e0 a b. ((a, b) -> b ! {e0}, b, List(a)) -> b ! {e0}
Right fold: f(x0, f(x1, ... f(xn, z))).
foldl
foldl : forall e0 a b. ((a, b) -> a ! {e0}, a, List(b)) -> a ! {e0}
Left fold: f(... f(f(z, x0), x1) ..., xn). Tail-recursive.
foldl(\(a, b) -> a + b, 0, [1, 2, 3])
append
append : forall a. (List(a), List(a)) -> List(a)
Concatenate two lists.
reverse
reverse : forall a. (List(a)) -> List(a)
The list in reverse order.
flatten
flatten : forall a. (List(List(a))) -> List(a)
Concatenate a list of lists.
concat_map
concat_map : forall e0 a b. ((a) -> List(b) ! {e0}, List(a)) -> List(b) ! {e0}
Map f over the list and concatenate the resulting lists.
zip_with
zip_with : forall e0 a b c. ((b, c) -> a ! {e0}, List(b), List(c)) -> List(a) ! {e0}
Combine two lists elementwise with f, stopping at the shorter length.
zip
zip : forall a b. (List(a), List(b)) -> List((a, b))
Pair up two lists elementwise, stopping at the shorter length.
unzip
unzip : forall a b. (List((a, b))) -> (List(a), List(b))
Split a list of pairs into a pair of lists.
count
count : forall e0 a. ((a) -> Bool ! {e0}, List(a)) -> Int ! {e0}
The number of elements satisfying p.
position_go
position_go : forall e0 a. ((a) -> Bool ! {e0}, Int, List(a)) -> Option(Int) ! {e0}
Helper for position: search from index n.
position
position : forall e0 a. ((a) -> Bool ! {e0}, List(a)) -> Option(Int) ! {e0}
The index of the first element satisfying p as Some, or None.
maximum
maximum : (List(Int)) -> Option(Int)
The greatest element as Some, or None for the empty list.
minimum
minimum : (List(Int)) -> Option(Int)
The least element as Some, or None for the empty list.
replicate
replicate : forall a. (Int, a) -> List(a)
A list of n copies of x.
range
range : (Int, Int) -> List(Int)
The ascending list [lo, lo+1, ..., hi-1] (empty when lo >= hi).
range(0, 5)
tabulate
tabulate : forall e0 a. (Int, (Int) -> a ! {e0}) -> List(a) ! {e0}
The list [f(0), f(1), ..., f(n-1)].
insert_sorted
insert_sorted : (Int, List(Int)) -> List(Int)
Insert x into an already-ascending list, keeping it sorted.
list_to_option
list_to_option : forall a. (List(a)) -> Option(a)
The first element as Some, or None (an Option view of the head).
partition
partition : forall e0 a. ((a) -> Bool ! {e0}, List(a)) -> (List(a), List(a)) ! {e0}
Split into (matching, non-matching) by predicate p.
chunks_of
chunks_of : forall a. (Int, List(a)) -> List(List(a))
Break the list into consecutive chunks of up to n elements.
scan_left
scan_left : forall e0 a b. ((a, b) -> a ! {e0}, a, List(b)) -> List(a) ! {e0}
Left scan: the running accumulators of foldl, starting from z.
list_ap
list_ap : forall e0 a b. (List((b) -> a ! {e0}), List(b)) -> List(a) ! {e0}
Applicative apply for lists: every function in fs applied to every x.