Skip to main content
Version: 1.4.0

List

Lists

type t<elt> = list<elt>

The type `t` is an alias for the predefined type `list`.

let empty: <elt>t<elt>

The value empty is the empty list. It is a synonym for list([]). In some contexts, it is useful to annotate it with its type, for example: (empty as list<int>).

let length: <elt>(_: t<elt>) => nat

The call length(l) is the number of elements in the list l. Note: List.length is another name for List.size.

let size: <elt>(_: t<elt>) => nat

The call size(l) is the number of elements in the list l.

let head: <elt>(_: t<elt>) => option<elt>

The call head(l), where l is a list, is None() if l is empty; otherwise, Some(hd), where hd is the head of the list.

let head_opt: <elt>(_: t<elt>) => option<elt>

**Deprecated:** Use `List.head` instead.

The call head_opt(l), where l is a list, is None() if l is empty; otherwise, Some(hd), where hd is the head of the list.

let tail: <elt>(_: t<elt>) => option<t<elt>>

The call tail(l), where l is a list, is None() if l is empty; otherwise, Some(tl), where tl is the tail of the list.

let tail_opt: <elt>(_: t<elt>) => option<t<elt>>

**Deprecated:** Use `List.tail` instead.

The call tail_opt(l), where l is a list, is None() if l is empty; otherwise, Some(tl), where tl is the tail of the list.

let map: <src, dst>(_: (_: src) => dst) => (_: list<src>) => list<dst>

The call map(f, list([a1; ...; an])) applies the function f to a1, ..., an (from left to right), and builds the list list([f(a1); ...; f(an)]) with the results returned by f.

let iter: <elt>(_: (_: elt) => unit) => (_: t<elt>) => unit

The call iter(f, list([a1; ...; an])) applies the function f in turn to list([a1; ...; an]). It is equivalent to {f(a1); f(a2); ...; f(an)}.

let fold_left: <elt, acc>(_: (_: [acc, elt]) => acc) => (_: acc) => (_: t<elt>) => acc

The call fold_left(f, init, list([a1; ...; an])) is f (... (f (f(init, a1)), a2), ...), an).

let fold_right: <elt, acc>(_: (_: [elt, acc]) => acc) => (_: t<elt>) => (_: acc) => acc

The call fold_right(f, list([a1; ...; an]), init) is f (a1, f (a2, (..., f (an, init))...)).

let fold: <elt, acc>(_: (_: [acc, elt]) => acc) => (_: t<elt>) => (_: acc) => acc

The call fold(f, list([a1; ...; an]), init) is f (... (f (f (init, a1), a2) ...), an). Note: fold_left(f, init, list) is the same as fold(f, list, init).

let cons: <elt>(_: elt) => (_: t<elt>) => t<elt>

The call cons(e, l) is list([e, ...l]).

let find_opt: <elt>(_: (_: elt) => bool) => (_: t<elt>) => option<elt>

The call find_opt(pred, list) is None() if no element of the list list satisfies the predicate pred; otherwise, it is Some(e), where e is the leftmost element in list that satisfies pred. The order of the calls of pred is not specified.

let filter_map: <src, dst>(_: (_: src) => option<dst>) => (_: list<src>) => list<dst>

The call filter_map(f, l) is the maximal sub-list of l such that the call of function f on its elements is not None(). Note: f is called on all elements of l. The order of the calls of f is not specified.

let update: <elt>(_: (_: elt) => option<elt>) => (_: t<elt>) => t<elt>

The call update(f, l) is the list l where the elements e such that f(e) is Some(v) have been replaced by v.

let update_with: <elt>(_: (_: elt) => bool) => (_: elt) => (_: t<elt>) => t<elt>

The call update_with(p,d,l) is the list l where the elements e such that satisfy the predicate p are replaced by d.