List

The List module provides purely-functional, singly-linked lists.

List (type)

A singly-linked list consists of zero or more cons cells, wherein each cell contains a single list element (the cell’s head), and a pointer to the remainder of the list (the cell’s tail).

type List<T> = ?(T, List<T>);

nil

Create an empty list.

nil : <T> () -> List<T>

isNil

Check whether a list is empty and return true if the list is empty.

isNil : <T> List<T> -> Bool

push

Construct a list by pre-pending a value. This function is similar to a list.cons(item) function.

push : <T> (T, List<T>) -> List<T>

last

Return the last element of the list, if present.

last : <T> List<T> -> ?T

pop

Treat the list as a stack. This function combines the head and (non-failing) tail operations into one operation.

pop : <T> List<T> -> (?T, List<T>)

len

Return the length of the list.

len : <T> List<T> -> Nat

lenIsEqLessThan

Test the list length against a maximum value and return true if the list length is less than or equal to the value specified.

lenIsEqLessThan : <T> (List<T>, Nat) -> Bool

lenClamp

Return the list length unless the number of items in the list exceeds a maximum value. If the list length exceed the maximum, the function returns null.

lenClamp : <T> (List<T>, max : Nat) -> ?Nat

nth

Access any item in a list, zero-based.

Indexing into a list is a linear operation, and usually an indication that a list might not be the best data structure to use.
nth : <T>(List<T>, Nat) -> ?T

rev

Reverse the list; tail recursive.

rev : <T> List<T> -> List<T>

iter

Call the given function with each list element in turn.

This function is equivalent to the app function in Standard ML Basis, and the iter function in OCaml.

iter : <T>(List<T>, f : T -> ()) -> ()

map

Call the given function on each list element and collect the results in a new list.

map : <T,S>(List<T>, f : T -> S) -> List<S>

filter

Create a new list with only those elements of the original list for which the given function (often called the predicate) returns true.

filter : <T>(List<T>, p : T -> Bool) -> List<T>

split

Create two new lists from the results of a given function (f). The first list only includes the elements for which the given function f returns true and tThe second list only includes the elements for which the function returns false.

In some languages, this operation is also known as a partition function.

split : <T>(List<T>, f : T -> Bool) -> (List<T>, List<T>)

mapFilter

Call the given function on each list element, and collect the non-null results in a new list.

mapFilter : <T,S>(List<T>, f : T -> ?S) -> List<S>

append

Append the elements from one list to another list.

append : <T>(List<T>, List<T>) -> List<T>

concat

Concatenate a list of lists.

In some languages, this operation is also known as a list join.

concat : <T>(List<List<T>>) -> List<T>

take

Take the n number of elements from the prefix of the given list. If the given list has fewer than n elements, this function returns a copy of the full input list.

take : <T>(List<T>, n:Nat) -> List<T>

Drop all but the first n elements from the given list.

drop

drop : <T>(List<T>, n:Nat) -> List<T>

foldLeft

Fold the list left-to-right using the given function (f).

foldLeft : <T,S>(List<T>, S, f : (T,S) -> S) -> S

foldRight

Fold the list right-to-left using the given function (f).

foldRight : <T,S>(List<T>, S, f : (T,S) -> S) -> S

find

Return the first element for which the given predicate f is true, if such an element exists.

find : <T>(l: List<T>, f : T -> Bool) -> ?T

exists

Return true if there exists a list element for which the given predicate f is true.

exists : <T>(List<T>, f : T -> Bool) -> Bool

all

Return true if the given predicate f is true for all list elements.

all : <T>(List<T>, f : T -> Bool) -> Bool

merge

Merge two ordered lists into a single ordered list. This function requires both list to be ordered as specified by the given relation lte.

merge : <T>(List<T>, List<T>, lte : (T,T) -> Bool) -> List<T>

lessThanEq

Compare two lists using lexicographic ordering specified by the given relation lte.

lessThanEq : <T>(List<T>, List<T>, lte: (T,T) -> Bool) -> Bool

isEq

Compare two lists for equality as specified by the given relation eq on the elements.

but the former is more efficient.

isEq : <T>(List<T>, List<T>, eq : (T,T) -> Bool) -> Bool

tabulate

Generate a list based on a length and a function that maps from a list index to a list element.

tabulate : <T>(Nat, f : Nat -> T) -> List<T>

singleton

Create a list with exactly one element.

singleton : <X> X -> List<X>

replicate

Create a list of the given length with the same value in each position.

replicate : <X>(Nat, X) -> List<X>

zip

Create a list of pairs from a pair of lists.

If the given lists have different lengths, then the created list will have a length equal to the length of the smaller list.

zip : <X, Y>(List<X>, List<Y>) -> List<(X, Y)>

zipWith

Create a list in which elements are calculated from the function f and include elements occuring at the same position in the given lists.

If the given lists have different lengths, then the created list will have a length equal to the length of the smaller list.

zipWith : <X, Y, Z>(List<X>, List<Y>, f : (X, Y) -> Z) -> List<Z>

splitAt

Split the given list at the given zero-based index.

splitAt : <X>(Nat, List<X>) -> (List<X>, List<X>)

chunksOf

Split the given list into chunks of length n. The last chunk will be shorter if the length of the given list does not divide by n evenly.

chunksOf : <X>(Nat, List<X>) -> List<List<X>>

fromArray

Convert an array into a list.

fromArray : <A>[A] -> List<A>

fromArrayMut

Convert a mutable array into a list.

fromArrayMut : <A>[var A] -> List<A>

toArray

Create an array from a list.

toArray : <A> List<A> -> [A]

toArrayMut

Create a mutable array from a list.

toArrayMut : <A> List<A> -> [var A]