Module NesList

Lists

Standard Library

This module contains everything defined for lists by the OCaml standard library. For these functions, refer to the official documentation.

include module type of Stdlib.List
type !'a t = 'a list =
  1. | []
  2. | :: of 'a * 'a list
val length : 'a list -> int
val compare_lengths : 'a list -> 'b list -> int
val compare_length_with : 'a list -> int -> int
val is_empty : 'a list -> bool
val cons : 'a -> 'a list -> 'a list
val hd : 'a list -> 'a
val tl : 'a list -> 'a list
val nth : 'a list -> int -> 'a
val nth_opt : 'a list -> int -> 'a option
val rev : 'a list -> 'a list
val init : int -> (int -> 'a) -> 'a list
val append : 'a list -> 'a list -> 'a list
val rev_append : 'a list -> 'a list -> 'a list
val concat : 'a list list -> 'a list
val flatten : 'a list list -> 'a list
val equal : ('a -> 'a -> bool) -> 'a list -> 'a list -> bool
val compare : ('a -> 'a -> int) -> 'a list -> 'a list -> int
val iter : ('a -> unit) -> 'a list -> unit
val iteri : (int -> 'a -> unit) -> 'a list -> unit
val map : ('a -> 'b) -> 'a list -> 'b list
val mapi : (int -> 'a -> 'b) -> 'a list -> 'b list
val rev_map : ('a -> 'b) -> 'a list -> 'b list
val filter_map : ('a -> 'b option) -> 'a list -> 'b list
val concat_map : ('a -> 'b list) -> 'a list -> 'b list
val fold_left_map : ('acc -> 'a -> 'acc * 'b) -> 'acc -> 'a list -> 'acc * 'b list
val fold_left : ('acc -> 'a -> 'acc) -> 'acc -> 'a list -> 'acc
val fold_right : ('a -> 'acc -> 'acc) -> 'a list -> 'acc -> 'acc
val iter2 : ('a -> 'b -> unit) -> 'a list -> 'b list -> unit
val map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
val rev_map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
val fold_left2 : ('acc -> 'a -> 'b -> 'acc) -> 'acc -> 'a list -> 'b list -> 'acc
val fold_right2 : ('a -> 'b -> 'acc -> 'acc) -> 'a list -> 'b list -> 'acc -> 'acc
val for_all : ('a -> bool) -> 'a list -> bool
val exists : ('a -> bool) -> 'a list -> bool
val for_all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
val exists2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
val mem : 'a -> 'a list -> bool
val memq : 'a -> 'a list -> bool
val find : ('a -> bool) -> 'a list -> 'a
val find_opt : ('a -> bool) -> 'a list -> 'a option
val find_index : ('a -> bool) -> 'a list -> int option
val find_map : ('a -> 'b option) -> 'a list -> 'b option
val find_mapi : (int -> 'a -> 'b option) -> 'a list -> 'b option
val filter : ('a -> bool) -> 'a list -> 'a list
val find_all : ('a -> bool) -> 'a list -> 'a list
val filteri : (int -> 'a -> bool) -> 'a list -> 'a list
val drop : int -> 'a list -> 'a list
val take_while : ('a -> bool) -> 'a list -> 'a list
val drop_while : ('a -> bool) -> 'a list -> 'a list
val partition : ('a -> bool) -> 'a list -> 'a list * 'a list
val partition_map : ('a -> ('b, 'c) Stdlib.Either.t) -> 'a list -> 'b list * 'c list
val assoc : 'a -> ('a * 'b) list -> 'b
val assoc_opt : 'a -> ('a * 'b) list -> 'b option
val assq : 'a -> ('a * 'b) list -> 'b
val assq_opt : 'a -> ('a * 'b) list -> 'b option
val mem_assoc : 'a -> ('a * 'b) list -> bool
val mem_assq : 'a -> ('a * 'b) list -> bool
val remove_assoc : 'a -> ('a * 'b) list -> ('a * 'b) list
val remove_assq : 'a -> ('a * 'b) list -> ('a * 'b) list
val split : ('a * 'b) list -> 'a list * 'b list
val combine : 'a list -> 'b list -> ('a * 'b) list
val sort : ('a -> 'a -> int) -> 'a list -> 'a list
val stable_sort : ('a -> 'a -> int) -> 'a list -> 'a list
val fast_sort : ('a -> 'a -> int) -> 'a list -> 'a list
val sort_uniq : ('a -> 'a -> int) -> 'a list -> 'a list
val merge : ('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
val to_seq : 'a list -> 'a Stdlib.Seq.t
val of_seq : 'a Stdlib.Seq.t -> 'a list

Additional Contents

Sort Functions

val sort_count : ('a -> 'a -> int) -> 'a list -> ('a * int) t

Same as sort_uniq, but count duplicates.

Others

val hd_opt : 'a list -> 'a option
val hd_tl : 'a list -> 'a * 'a list

Pair of hd and tl.

  • raises Failure

    if the list is empty.

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

Remove the element at the given indice in the list.

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

Swap elements at the given indices in the list.

Contexts
type 'a context = {
  1. element : 'a;
  2. index : int;
  3. previous : 'a option;
  4. next : 'a option;
  5. total : int;
}

The context of an element

val find_context : ('a -> bool) -> 'a t -> 'a context option

Finds the given element and return it and its context.

val findi_context : (int -> 'a -> bool) -> 'a t -> 'a context option

Same as find_context but also provides the index in the test.

val map_context : ('a -> 'b) -> 'a context -> 'b context

Maps the given function on all the elements of the context.

Bodies and feet
val ft_opt : 'a list -> 'a option
val ft : 'a t -> 'a

Return the foot of the given list, that is the last element of the list.

  • raises Failure

    if the list is empty.

val bd_ft : 'a t -> 'a list * 'a

Return the pair of the body and the foot of the given list, that is the list without its last element and the last element.

  • raises Failure

    if the list is empty.

val intersperse : ?last:'a -> 'a -> 'a t -> 'a t

intersperse ?last x l is the list l with x “interspersed” between all the elements of l. If ?last is not None, then the last occurence is not x but last. For instance, intersperse ?last:" & " "," ["a"; "b"; "c"; "d"] = ["a"; ", "; "b"; ", "; "c"; " & "; "d"].

val interspersei : ?last:(int -> 'a) -> (int -> 'a) -> 'a t -> 'a t

Same as intersperse but with the indice passed as argument.

val singleton : 'a -> 'a list

singleton x is [x].

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

Return Some if all the elements of the list are of the form Some x, or None if at least one element is None.

Association lists

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

Same as extract_assoc but returns None instead of raising Not_found.

val to_option : ?more:('a list -> 'a option) -> 'a list -> 'a option

Converts a lists to an option. The function ?more is used for when the list is neither empty nor a singleton. It defaults to raising Invalid_arg.

val group : by:('a -> 'a -> bool) -> 'a list -> 'a list list

Group the elements of the given list using the “equality” by. The equality is assumed to be transitive.

val contains_duplicates : ?eq:('a -> 'a -> bool) -> 'a list -> bool

Checks whether the list contains duplicate values. This function only requires an equality (which defaults to Stdlib.(=)) but runs in O(n²).

val deduplicate : ?eq:('a -> 'a -> bool) -> 'a list -> 'a list

Keep only the first of each equivalence classes given by ?eq, which defaults to Stdlib.(=). Runs in O(n²).

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

take n, applied to a list xs, returns the prefix of xs of length n, or xs itself if n >= length xs.