Skip to main content

List

val length : nat

let length: nat

Get the number of elements in a list.

let xs : int list = [1; 2; 3]

let length : nat = List.length xs
let xs : list<int> = list([1, 2, 3]);

let length : nat = List.length (xs);

val size : nat

let size: nat

Get the number of elements in a list.

Synonym for List.length.

let size : nat = List.size xs
let size : nat = List.size (xs);

val head_opt : 'a list -> 'a option

let head_opt : (list: list<'a>) => option<'a>

Get the head of a list

let head_opt : int option = List.head_opt xs
let head_opt : option<int>  = List.head_opt (xs);

val tail_opt : 'a list -> 'a list option

let tail_opt : (list: list<'a>) => option<list<'a>>

Get the tail of a list

let tail_opt : int list option = List.tail_opt xs
let tail_opt : option<list<int>> = List.tail_opt (xs);

val iter : ('a -> unit) -> 'a list -> unit

let iter: (iterator: ((item: 'a) => unit), list: list<'a>) => unit

Iterate over items in a list.

let iter_op (l : int list) : unit =
let predicate = fun (i : int) -> assert (i > 3)
in List.iter predicate l
let iter_op = (l: list<int>): unit => {
let predicate = (i: int): unit => assert(i > 3);
List.iter(predicate, l);
};

val map : ('a -> 'b) -> 'a list -> 'b list

let map: (mapper: ((item: 'a) => 'b), list: list<'a>) => list<'b>

Apply a function to items of a list to create a new list.

let larger_list: int list = [1; 2; 3]

let increment (i : int) : int = i + 1

// Creates a new list with all elements incremented by 1
let plus_one : int list = List.map increment larger_list
let larger_list: list<int> = list([1, 2, 3]);

let increment = (i : int): int => i + 1;

// Creates a new list with all elements incremented by 1
let plus_one : list<int> = List.map(increment, larger_list);

val fold : ('acc * 'item -> 'acc) -> 'item list -> 'acc -> 'acc

let fold: ((folder: [acc: 'acc, item: 'item]) => 'acc, list: list<'item>, acc: 'acc) => 'acc

Fold over items in a list;

let my_list : int list = [1; 2; 3]

let sum (acc, i : int * int) : int = acc + i

let sum_of_elements : int = List.fold sum my_list 0
let my_list_fold: list<int> = list([1, 2, 3]);

let sum_fold = ([result, i]: [int, int]): int => result + i;

let sum_of_elements_fold: int = List.fold(sum_fold, my_list_fold, 0);

val fold_left : ('acc * 'item -> 'acc) -> 'acc -> 'item list -> 'acc

let fold_left: (((a: ['acc, 'item]) => 'acc), 'acc, list<'item>) => 'acc

Fold over items in a list;

let my_list : int list = [1; 2; 3]

let sum (acc, i : int * int) : int = acc + i

let sum_of_elements : int = List.fold_left sum 0 my_list
let my_list : list<int> = list([1, 2, 3]);

let sum = ([result, i]: [int, int]): int => result + i;

let sum_of_elements : int = List.fold_left (sum, 0, my_list);

val fold_right : ('item * 'acc -> 'acc) -> 'item list -> 'acc -> 'acc

let fold_right: (((a: ['item, 'acc]) => 'acc), list<'item>, 'acc) => 'acc

Fold over items in a list;

let my_list : int list = [1; 2; 3]

let sum_right (i, acc : int * int) : int = acc + i

let sum_of_elements : int = List.fold_right sum_right my_list 0
let my_list : list<int> = list([1, 2, 3]);

let sum_right = ([i, result]: [int, int]): int => result + i;

let sum_of_elements : int = List.fold_right (sum_right, my_list, 0);

val find_opt : ('a -> bool) -> 'a list -> 'a option

let find_opt: (mapper: ((item: 'a) => bool), list: list<'a>) => option<'a>

Finds the first element satisfying the given predicate.

val filter_map : ('a -> 'b option) -> 'a list -> 'b list

let filter_map: (mapper: ((item: 'a) => option<'b>), list: list<'a>) => list<'b>

Apply a function to items of a list to create a new list, but the function can omit certain elements by returning None.

Notice: built in terms of fold_right.

val update : ('a -> 'a option) -> 'a list -> 'a list

let update: (upd: ((item: 'a) => option<'a>), list: list<'a>) => list<'a>

Apply a function to items of a list to create a new list of the same type, but certain elements can be changed using upd.

Notice: built in terms of map.

val update_with : ('a -> bool) -> 'a -> 'a list -> 'a list

let update_with: (upd: ((item: 'a) => bool), new_item: 'a, list: list<'a>) => list<'a>

Create a new list of the same type: if the predicate is satisfied on some element, this element is replaced for the new item.

Notice: built in terms of map.