Module Stdlib320.List

Basic functions

val length : 'a list -> int

length l is the number of elements in l, e.g, length [1;2;3] = 3

val nth : 'a list -> int -> 'a

nth l i is the ith element of l if i >= 0 and i < length l.

Raises an exception otherwise.

val nth_opt : 'a list -> int -> 'a option

Same as the previous function, but is None in the case of failure.

val rev : 'a list -> 'a list

Reverses a list, e.g., rev [1;2;3] = [3;2;1]

val concat : 'a list list -> 'a list

Combines a list of lists from left to right, e.g., concat [[1;2;3];[4;5;6];[7;8;9]] = [1;2;3;4;5;6;7;8;9]

Higher-order functions

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

Mapping function for lists. map f l applies f to every element of l, e.g, map abs [-1;-2;-3] = [1;2;3].

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

Filtering function for lists. filter f l is the element of l (in order) which satisfy the predicate f, e.g, filter ((<) 5) [3;4;5;6;7] = [6;7].

val fold_left : ('acc -> 'a -> 'acc) -> 'acc -> 'a list -> 'acc

Left-associative folding function for lists. fold_left op init [x_1;x_2;...;x_n] is equivalent to op (...(op (op init x_1) x_2)...) x_n.

val fold_right : ('a -> 'acc -> 'acc) -> 'a list -> 'acc -> 'acc

Right-associative folding function for lists. fold_right op [x_1;x_2;...;x_n] is equivalent to op x_1 (op x_2 (...(op x_n base)...)).

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

rev_map f l is equivalent to rev (map f l)

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

filter_map f l is equivalent to the list filter Option.is_some (map f l) but with the Some constructors removed from each element.

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

concat_map f l is equivalent to concat (map f l)

Finding

val mem : 'a -> 'a list -> bool

Membership predicate for lists. mem x l is

  • true if x appears in l
  • false otherwise.
val find : ('a -> bool) -> 'a list -> 'a

Finds based on a predicate. find f l is the first appearance of an element of l which satisfies f.

Raises a Not_found exception otherwise.

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

Same as the previous function, but is None in the case of failure.

Taking/dropping

val take : int -> 'a list -> 'a list

Take a prefix of a list. take i l is the list containing the first min i (length l) elements of l, given i >= 0.

Raises an Invalid_argument exception otherwise.

val drop : int -> 'a list -> 'a list

drops the first min i (length l) elements of l and returns the remaining elements, given i >= 0.

Raises an Invalid_argument exception otherwise.

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

take_while f l is the longest prefix of l in which all elements satisfy the predicate f.

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

drop_while f l is equivalent to drop (length (take_while f l)) l

Sorting

val sort : ('a -> 'a -> int) -> 'a list -> 'a list

Generic sorting function for lists. sort f l has the same elements as l, but sorted according to the comparing function f.

Association lists

val assoc : 'a -> ('a * 'b) list -> 'b

Membership function for association lists. assoc x l is equivalent to

snd (find (fun (k, v) -> k = x))

val assoc_opt : 'a -> ('a * 'b) list -> 'b option

Same as the previous function, but is None in the case of failure.

val remove_assoc : 'a -> ('a * 'b) list -> ('a * 'b) list

remove_assoc k l is l but with the first appearance of a pair of the form (k, v) removed.