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 = | []| :: of 'a * 'a list
val length : 'a list -> intval compare_lengths : 'a list -> 'b list -> intval compare_length_with : 'a list -> int -> intval is_empty : 'a list -> boolval cons : 'a -> 'a list -> 'a listval tl : 'a list -> 'a listval nth : 'a list -> int -> 'aval nth_opt : 'a list -> int -> 'a optionval rev : 'a list -> 'a listval init : int -> (int -> 'a) -> 'a listval append : 'a list -> 'a list -> 'a listval rev_append : 'a list -> 'a list -> 'a listval concat : 'a list list -> 'a listval flatten : 'a list list -> 'a listval equal : ('a -> 'a -> bool) -> 'a list -> 'a list -> boolval compare : ('a -> 'a -> int) -> 'a list -> 'a list -> intval iter : ('a -> unit) -> 'a list -> unitval iteri : (int -> 'a -> unit) -> 'a list -> unitval map : ('a -> 'b) -> 'a list -> 'b listval mapi : (int -> 'a -> 'b) -> 'a list -> 'b listval rev_map : ('a -> 'b) -> 'a list -> 'b listval filter_map : ('a -> 'b option) -> 'a list -> 'b listval concat_map : ('a -> 'b list) -> 'a list -> 'b listval fold_left_map :
('acc -> 'a -> 'acc * 'b) ->
'acc ->
'a list ->
'acc * 'b listval fold_left : ('acc -> 'a -> 'acc) -> 'acc -> 'a list -> 'accval fold_right : ('a -> 'acc -> 'acc) -> 'a list -> 'acc -> 'accval iter2 : ('a -> 'b -> unit) -> 'a list -> 'b list -> unitval map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c listval rev_map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c listval fold_left2 :
('acc -> 'a -> 'b -> 'acc) ->
'acc ->
'a list ->
'b list ->
'accval fold_right2 :
('a -> 'b -> 'acc -> 'acc) ->
'a list ->
'b list ->
'acc ->
'accval for_all : ('a -> bool) -> 'a list -> boolval exists : ('a -> bool) -> 'a list -> boolval for_all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> boolval exists2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> boolval mem : 'a -> 'a list -> boolval memq : 'a -> 'a list -> boolval find : ('a -> bool) -> 'a list -> 'aval find_opt : ('a -> bool) -> 'a list -> 'a optionval find_index : ('a -> bool) -> 'a list -> int optionval find_map : ('a -> 'b option) -> 'a list -> 'b optionval find_mapi : (int -> 'a -> 'b option) -> 'a list -> 'b optionval filter : ('a -> bool) -> 'a list -> 'a listval find_all : ('a -> bool) -> 'a list -> 'a listval filteri : (int -> 'a -> bool) -> 'a list -> 'a listval drop : int -> 'a list -> 'a listval take_while : ('a -> bool) -> 'a list -> 'a listval drop_while : ('a -> bool) -> 'a list -> 'a listval partition : ('a -> bool) -> 'a list -> 'a list * 'a listval partition_map :
('a -> ('b, 'c) Stdlib.Either.t) ->
'a list ->
'b list * 'c listval assoc : 'a -> ('a * 'b) list -> 'bval assoc_opt : 'a -> ('a * 'b) list -> 'b optionval assq : 'a -> ('a * 'b) list -> 'bval assq_opt : 'a -> ('a * 'b) list -> 'b optionval mem_assoc : 'a -> ('a * 'b) list -> boolval mem_assq : 'a -> ('a * 'b) list -> boolval remove_assoc : 'a -> ('a * 'b) list -> ('a * 'b) listval remove_assq : 'a -> ('a * 'b) list -> ('a * 'b) listval split : ('a * 'b) list -> 'a list * 'b listval combine : 'a list -> 'b list -> ('a * 'b) listval sort : ('a -> 'a -> int) -> 'a list -> 'a listval stable_sort : ('a -> 'a -> int) -> 'a list -> 'a listval fast_sort : ('a -> 'a -> int) -> 'a list -> 'a listval sort_uniq : ('a -> 'a -> int) -> 'a list -> 'a listval merge : ('a -> 'a -> int) -> 'a list -> 'a list -> 'a listval to_seq : 'a list -> 'a Stdlib.Seq.tval of_seq : 'a Stdlib.Seq.t -> 'a listAdditional Contents
Sort Functions
val sort_count : ('a -> 'a -> int) -> 'a list -> ('a * int) tOthers
val hd_opt : 'a list -> 'a optionval hd_tl : 'a list -> 'a * 'a listval remove : int -> 'a list -> 'a listRemove the element at the given indice in the list.
val swap : int -> int -> 'a list -> 'a listSwap elements at the given indices in the list.
Contexts
type 'a context = {element : 'a;index : int;previous : 'a option;next : 'a option;total : int;
}The context of an element
val find_context : ('a -> bool) -> 'a t -> 'a context optionFinds the given element and return it and its context.
val findi_context : (int -> 'a -> bool) -> 'a t -> 'a context optionSame as find_context but also provides the index in the test.
val map_context : ('a -> 'b) -> 'a context -> 'b contextMaps the given function on all the elements of the context.
Bodies and feet
val ft_opt : 'a list -> 'a optionReturn the foot of the given list, that is the last element of the list.
val bd_ft : 'a t -> 'a list * 'aReturn the pair of the body and the foot of the given list, that is the list without its last element and the last element.
val intersperse : ?last:'a -> 'a -> 'a t -> 'a tintersperse ?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 tSame as intersperse but with the indice passed as argument.
val singleton : 'a -> 'a listval all_some : 'a option list -> 'a list optionReturn 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
Same as extract_assoc but returns None instead of raising Not_found.
val to_option : ?more:('a list -> 'a option) -> 'a list -> 'a optionConverts 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 listGroup 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 -> boolChecks 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 listKeep only the first of each equivalence classes given by ?eq, which defaults to Stdlib.(=). Runs in O(n²).
val take : int -> 'a list -> 'a listtake n, applied to a list xs, returns the prefix of xs of length n, or xs itself if n >= length xs.