March Docs

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

fnemptyempty() : List(a)#

Returns an empty list.

Examples

List.empty()  -- []
fnsingletonsingleton(x : a) : List(a)#

Returns a list containing a single element.

Examples

List.singleton(42)   -- [42]
List.singleton("hi") -- ["hi"]
fnrepeatrepeat(x : a, n : Int) : List(a)#

Returns a list with x repeated n times.

Examples

List.repeat(0, 3)    -- [0, 0, 0]
List.repeat("x", 4)  -- ["x", "x", "x", "x"]
fnrangerange(start : Int, stop : Int) : List(Int)#

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)   -- []
fnrange_steprange_step(start : Int, stop : Int, step : Int) : List(Int)#

Returns [start, start+step, ...] up to but not including stop.

fnheadhead(xs : List(a)) : a#

Returns the first element. Panics on empty list.

fntailtail(xs : List(a)) : List(a)#

Returns the tail (all but first element). Panics on empty list.

fnhead_opthead_opt(xs : List(a)) : Option(a)#

Returns Some(first element), or None if empty.

fntail_opttail_opt(xs : List(a)) : Option(List(a))#

Returns Some(tail), or None if empty.

fnlastlast(xs : List(a)) : a#

Returns the last element. Panics on empty list.

fnnthnth(xs : List(a), n : Int) : a#

Returns the nth element (0-indexed). Panics if out of bounds.

fnnth_optnth_opt(xs : List(a), n : Int) : Option(a)#

Returns Some(nth element), or None if out of bounds.

fnlengthlength(xs : List(a)) : Int#

Returns the number of elements in the list.

Examples

List.length([1, 2, 3])  -- 3
List.length([])         -- 0
fnis_emptyis_empty(xs : List(a)) : Bool#

Returns true if the list has no elements.

fnreversereverse(xs : List(a)) : List(a)#

Returns the list in reverse order.

fnappendappend(xs : List(a), ys : List(a)) : List(a)#

Concatenates two lists.

Examples

List.append([1, 2], [3, 4, 5])  -- [1, 2, 3, 4, 5]
List.append([], [1, 2])         -- [1, 2]
fnmapmap(xs : List(a), f : a -> b) : List(b)#

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"]
fneacheach(xs : List(a), f : a -> b) : ()#

Applies f to each element for side effects. Returns ().

fnflat_mapflat_map(xs : List(a), f : a -> List(b)) : List(b)#

Applies f to each element and concatenates the results.

fnfilterfilter(xs : List(a), pred : a -> Bool) : List(a)#

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"]
fnfilter_mapfilter_map(xs : List(a), f : a -> Option(b)) : List(b)#

Applies f to each element; collects Some values, discards None. Equivalent to map followed by filter for non-None values.

fnfold_leftfold_left(acc : b, xs : List(a), f : b -> a -> b) : b#

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)
fnfold_rightfold_right(xs : List(a), acc : b, f : a -> b -> b) : b#

Right fold: reduces the list from right to left.

fnscan_leftscan_left(acc : b, xs : List(a), f : b -> a -> b) : List(b)#

Like fold_left but returns a list of all intermediate accumulator values.

fnconcatconcat(xss : List(List(a))) : List(a)#

Concatenates a list of lists into a single list.

fnintersperseintersperse(xs : List(a), sep : a) : List(a)#

Inserts sep between every consecutive pair of elements.

fnfindfind(xs : List(a), pred : a -> Bool) : Option(a)#

Returns the first element satisfying pred, or None.

fnfind_indexfind_index(xs : List(a), pred : a -> Bool) : Option(Int)#

Returns the index of the first element satisfying pred, or None.

fnanyany(xs : List(a), pred : a -> Bool) : Bool#

Returns true if any element satisfies pred.

fnallall(xs : List(a), pred : a -> Bool) : Bool#

Returns true if all elements satisfy pred (vacuously true for []).

fnsort_bysort_by(xs : List(a), cmp : a -> a -> Bool) : List(a)#

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"]
fntaketake(xs : List(a), n : Int) : List(a)#

Returns the first n elements. Returns the whole list if n &gt;= length.

fndropdrop(xs : List(a), n : Int) : List(a)#

Drops the first n elements. Returns [] if n &gt;= length.

fntake_whiletake_while(xs : List(a), pred : a -> Bool) : List(a)#

Returns the longest prefix of elements satisfying pred.

fndrop_whiledrop_while(xs : List(a), pred : a -> Bool) : List(a)#

Drops elements while pred holds, returning the rest.

fnsplit_atsplit_at(xs : List(a), n : Int) : (List(a), List(a))#

Splits the list at position n, returning (take n xs, drop n xs).

fnpartitionpartition(xs : List(a), pred : a -> Bool) : (List(a), List(a))#

Splits into (elements satisfying pred, elements not satisfying pred).

fndrop_lastdrop_last(xs : List(a)) : List(a)#

Returns all elements except the last. Returns Nil for empty or singleton lists.

fnchunkschunks(xs : List(a), size : Int) : List(List(a))#

Splits the list into consecutive chunks of at most size elements.

fnzipzip(xs : List(a), ys : List(b)) : List((a, b))#

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")]
fnzip_withzip_with(xs : List(a), ys : List(b), f : a -> b -> c) : List(c)#

Like zip but applies f to each pair instead of building tuples.

fnunzipunzip(pairs : List((a, b))) : (List(a), List(b))#

Splits a list of pairs into two lists.

fnenumerateenumerate(xs : List(a)) : List((Int, a))#

Pairs each element with its 0-based index: [a, b] -&gt; [(0, a), (1, b)].

fnsum_intsum_int(xs : List(Int)) : Int#

Sums a list of integers.

fnproduct_intproduct_int(xs : List(Int)) : Int#

Multiplies all integers in a list.

fnminimum_intminimum_int(xs : List(Int)) : Int#

Returns the minimum integer in the list. Panics if empty.

fnmaximum_intmaximum_int(xs : List(Int)) : Int#

Returns the maximum integer in the list. Panics if empty.

fnmembermember(xs : List(a), x : a) : Bool#

Removes consecutive duplicate elements (keeps first of each run).

fndedupdedup(xs : List(a)) : List(a)#