List
List module: immutable singly-linked list operations.
List(a) = Cons(a, List(a)) Nil (built-in constructors)
Design:
- Partial functions (head, tail, nth, last) panic on empty/out-of-bounds
- Safe variants use the _opt suffix and return Option
- fold_left is the primitive; fold_right recurses on the structure
- sort uses merge sort
Functions
Returns an empty list.
Examples
List.empty() -- []Returns a list containing a single element.
Examples
List.singleton(42) -- [42]
List.singleton("hi") -- ["hi"]Returns a list with x repeated n times.
Examples
List.repeat(0, 3) -- [0, 0, 0]
List.repeat("x", 4) -- ["x", "x", "x", "x"]Returns [start, start+1, ..., stop-1]. Returns [] if start >= stop.
Examples
List.range(0, 5) -- [0, 1, 2, 3, 4]
List.range(3, 7) -- [3, 4, 5, 6]
List.range(5, 5) -- []Returns [start, start+step, ...] up to but not including stop.
Returns the first element. Panics on empty list.
Returns the tail (all but first element). Panics on empty list.
Returns Some(first element), or None if empty.
Returns Some(tail), or None if empty.
Returns the last element. Panics on empty list.
Returns the nth element (0-indexed). Panics if out of bounds.
Returns Some(nth element), or None if out of bounds.
Returns the number of elements in the list.
Examples
List.length([1, 2, 3]) -- 3
List.length([]) -- 0Returns true if the list has no elements.
Returns the list in reverse order.
Concatenates two lists.
Examples
List.append([1, 2], [3, 4, 5]) -- [1, 2, 3, 4, 5]
List.append([], [1, 2]) -- [1, 2]Applies f to each element, returning a new list.
Examples
List.map([1, 2, 3], fn x -> x * 2)
-- [2, 4, 6]
List.map(["a", "b"], String.upper)
-- ["A", "B"]Applies f to each element for side effects. Returns ().
Applies f to each element and concatenates the results.
Returns only elements satisfying pred.
Examples
List.filter([1, 2, 3, 4, 5], fn x -> x > 2)
-- [3, 4, 5]
List.filter(["foo", "", "bar", ""], fn s -> !String.is_empty(s))
-- ["foo", "bar"]Applies f to each element; collects Some values, discards None. Equivalent to map followed by filter for non-None values.
Left fold: reduces the list from left to right with an accumulator.
Examples
List.fold_left(0, [1, 2, 3, 4], fn acc -> fn x -> acc + x)
-- 10
List.fold_left([], [1, 2, 3], fn acc -> fn x -> Cons(x * 2, acc))
-- [6, 4, 2] (note: reversed; use fold_right or reverse for ordered)Right fold: reduces the list from right to left.
Like fold_left but returns a list of all intermediate accumulator values.
Concatenates a list of lists into a single list.
Inserts sep between every consecutive pair of elements.
Returns the first element satisfying pred, or None.
Returns the index of the first element satisfying pred, or None.
Returns true if any element satisfies pred.
Returns true if all elements satisfy pred (vacuously true for []).
Sorts by a comparator function. Uses merge sort (stable, O(n log n)).
Examples
List.sort_by([3, 1, 4, 1, 5], fn (a, b) -> a < b)
-- [1, 1, 3, 4, 5]
List.sort_by(["banana", "apple", "cherry"], fn (a, b) -> a < b)
-- ["apple", "banana", "cherry"]Returns the first n elements. Returns the whole list if n >= length.
Drops the first n elements. Returns [] if n >= length.
Returns the longest prefix of elements satisfying pred.
Drops elements while pred holds, returning the rest.
Splits the list at position n, returning (take n xs, drop n xs).
Splits into (elements satisfying pred, elements not satisfying pred).
Returns all elements except the last. Returns Nil for empty or singleton lists.
Splits the list into consecutive chunks of at most size elements.
Pairs up elements from two lists. Stops at the shorter list.
Examples
List.zip([1, 2, 3], ["a", "b", "c"])
-- [(1, "a"), (2, "b"), (3, "c")]
List.zip([1, 2], ["a", "b", "c", "d"])
-- [(1, "a"), (2, "b")]Like zip but applies f to each pair instead of building tuples.
Splits a list of pairs into two lists.
Pairs each element with its 0-based index: [a, b] -> [(0, a), (1, b)].
Sums a list of integers.
Multiplies all integers in a list.
Returns the minimum integer in the list. Panics if empty.
Returns the maximum integer in the list. Panics if empty.
Removes consecutive duplicate elements (keeps first of each run).