Return the length (number of elements) of the given list.
val hd : 'a list->'a
Return the first element of the given list. Raise Failure "hd" if the list is empty.
val tl : 'a list->'a list
Return the given list without its first element. Raise Failure "tl" if the list is empty.
val nth : 'a list->int ->'a
Return the n-th element of the given list. The first element (head of the list) is at position 0. Raise Failure "nth" if the list is too short. Raise Invalid_argument "List.nth" if n is negative.
val rev : 'a list->'a list
List reversal.
val rev_append : 'a list->'a list->'a list
ListLabels.rev_append l1 l2 reverses l1 and concatenates it to l2. This is equivalent to ListLabels.rev l1 @ l2, but rev_append is tail-recursive and more efficient.
val concat : 'a list list->'a list
Concatenate a list of lists. The elements of the argument are all concatenated together (in the same order) to give the result. Not tail-recursive (length of the argument + length of the longest sub-list).
Iterators
val iter : f:('a-> unit)->'a list-> unit
ListLabels.iter f [a1; ...; an] applies function f in turn to a1; ...; an. It is equivalent to begin f a1; f a2; ...; f an; () end.
val rev_map : f:('a->'b)->'a list->'b list
ListLabels.rev_map f l gives the same result as ListLabels.rev (ListLabels.map f l), but is tail-recursive and more efficient.
val fold_left : f:('a->'b->'a)->init:'a->'b list->'a
ListLabels.fold_left f a [b1; ...; bn] is f (... (f (f a b1) b2) ...) bn.
Iterators on two lists
val iter2 : f:('a->'b-> unit)->'a list->'b list-> unit
ListLabels.iter2 f [a1; ...; an] [b1; ...; bn] calls in turn f a1 b1; ...; f an bn. Raise Invalid_argument if the two lists are determined to have different lengths.
val map2 : f:('a->'b->'c)->'a list->'b list->'c list
ListLabels.map2 f [a1; ...; an] [b1; ...; bn] is [f a1 b1; ...; f an bn]. Raise Invalid_argument if the two lists are determined to have different lengths. Not tail-recursive.
val rev_map2 : f:('a->'b->'c)->'a list->'b list->'c list
ListLabels.rev_map2 f l1 l2 gives the same result as ListLabels.rev (ListLabels.map2 f l1 l2), but is tail-recursive and more efficient.
val fold_left2 :
f:('a->'b->'c->'a)->init:'a->'b list->'c list->'a
ListLabels.fold_left2 f a [b1; ...; bn] [c1; ...; cn] is f (... (f (f a b1 c1) b2 c2) ...) bn cn. Raise Invalid_argument if the two lists are determined to have different lengths.
val fold_right2 :
f:('a->'b->'c->'c)->'a list->'b list->init:'c->'c
ListLabels.fold_right2 f [a1; ...; an] [b1; ...; bn] c is f a1 b1 (f a2 b2 (... (f an bn c) ...)). Raise Invalid_argument if the two lists are determined to have different lengths. Not tail-recursive.
List scanning
val for_all : f:('a-> bool)->'a list-> bool
for_all p [a1; ...; an] checks if all elements of the list satisfy the predicate p. That is, it returns (p a1) && (p a2) && ... && (p an).
val exists : f:('a-> bool)->'a list-> bool
exists p [a1; ...; an] checks if at least one element of the list satisfies the predicate p. That is, it returns (p a1) || (p a2) || ... || (p an).
val for_all2 : f:('a->'b-> bool)->'a list->'b list-> bool
Same as ListLabels.for_all, but for a two-argument predicate. Raise Invalid_argument if the two lists are determined to have different lengths.
val exists2 : f:('a->'b-> bool)->'a list->'b list-> bool
Same as ListLabels.exists, but for a two-argument predicate. Raise Invalid_argument if the two lists are determined to have different lengths.
val memq : 'a->set:'a list-> bool
Same as ListLabels.mem, but uses physical equality instead of structural equality to compare list elements.
List searching
val find : f:('a-> bool)->'a list->'a
find p l returns the first element of the list l that satisfies the predicate p. Raise Not_found if there is no value that satisfies p in the list l.
val partition : f:('a-> bool)->'a list->'a list * 'a list
partition p l returns a pair of lists (l1, l2), where l1 is the list of all the elements of l that satisfy the predicate p, and l2 is the list of all the elements of l that do not satisfy p. The order of the elements in the input list is preserved.
Association lists
val assq : 'a->('a * 'b) list->'b
Same as ListLabels.assoc, but uses physical equality instead of structural equality to compare keys.
val mem_assq : 'a->map:('a * 'b) list-> bool
Same as ListLabels.mem_assoc, but uses physical equality instead of structural equality to compare keys.
val remove_assq : 'a->('a * 'b) list->('a * 'b) list
Same as ListLabels.remove_assoc, but uses physical equality instead of structural equality to compare keys. Not tail-recursive.
Lists of pairs
Sorting
val sort : cmp:('a->'a-> int)->'a list->'a list
Sort a list in increasing order according to a comparison function. The comparison function must return 0 if its arguments compare as equal, a positive integer if the first is greater, and a negative integer if the first is smaller (see Array.sort for a complete specification). For example, Pervasives.compare is a suitable comparison function. The resulting list is sorted in increasing order. ListLabels.sort is guaranteed to run in constant heap space (in addition to the size of the result list) and logarithmic stack space.
The current implementation uses Merge Sort. It runs in constant heap space and logarithmic stack space.
val stable_sort : cmp:('a->'a-> int)->'a list->'a list
Same as ListLabels.sort, but the sorting algorithm is guaranteed to be stable (i.e. elements that compare equal are kept in their original order) .
The current implementation uses Merge Sort. It runs in constant heap space and logarithmic stack space.
val fast_sort : cmp:('a->'a-> int)->'a list->'a list
val merge : cmp:('a->'a-> int)->'a list->'a list->'a list
Merge two lists: Assuming that l1 and l2 are sorted according to the comparison function cmp, merge cmp l1 l2 will return a sorted list containting all the elements of l1 and l2. If several elements compare equal, the elements of l1 will be before the elements of l2. Not tail-recursive (sum of the lengths of the arguments).
filter ~f l returns all the elements of the list l that satisfy the predicate f. The order of the elements in the input list l is preserved. Safe version of List.filter.
val fold_map2 :
f:('acc->'a->'b->'acc * 'c)->init:'acc->'a list->'b list->'acc * 'c list
fold_map2 ~f ~init l1 l2 is to fold_map what List.map2 is to List.map.
raisesInvalid_argument
if the lists do not have the same length.
since 0.16
val fold_filter_map :
f:('acc->'a->'acc * 'b option)->init:'acc->'a list->'acc * 'b list
fold_filter_map ~f ~init l is a fold_left-like function, but also generates a list of output in a way similar to filter_map.
since 0.17
val fold_filter_map_i :
f:('acc->int ->'a->'acc * 'b option)->init:'acc->'a list->'acc * 'b list
fold_filter_map_i ~f ~init l is a foldi-like function, but also generates a list of output in a way similar to filter_map.
since 2.8
val fold_flat_map :
f:('acc->'a->'acc * 'b list)->init:'acc->'a list->'acc * 'b list
fold_flat_map ~f ~init l is a fold_left-like function, but it also maps the list to a list of lists that is then flatten'd.
since 0.14
val fold_flat_map_i :
f:('acc->int ->'a->'acc * 'b list)->init:'acc->'a list->'acc * 'b list
fold_flat_map_i ~f ~init l is a fold_left-like function, but it also maps the list to a list of lists that is then flatten'd.
since 2.8
val count : f:('a-> bool)->'a list-> int
count ~f l counts how many elements of l satisfy predicate f.
since 1.5, but only
since 2.2 with labels
val count_true_false : f:('a-> bool)->'a list-> int * int
count_true_false ~f l returns a pair (int1,int2) where int1 is the number of elements in l that satisfy the predicate f, and int2 the number of elements that do not satisfy f.
combine_gen l1 l2 transforms two lists into a gen of pairs. Lazy version of combine. Unlike combine, it does not fail if the lists have different lengths; instead, the output has as many pairs as the smallest input list.
since 1.2, but only
since 2.2 with labels
val combine_shortest : 'a list->'b list->('a * 'b) list
combine_shortest [a1; …; am] [b1; …; bn] is [(a1,b1); …; (am,bm)] if m <= n. Like combine but stops at the shortest list rather than raising.
cartesian_product [[l1];[l2]; …; [ln]] produces the cartesian product of this list of lists, by returning all the ways of picking one element per sublist. NOTE the order of the returned list is unspecified. For example:
invariant: cartesian_product l = map_product id l.
since 1.2, but only
since 2.2 with labels
val map_product_l : f:('a->'b list)->'a list->'b list list
map_product_l ~f l maps each element of l to a list of objects of type 'b using f. We obtain [l1; l2; …; ln] where length l=n and li : 'b list. Then, it returns all the ways of picking exactly one element per li.
val group_by : ?hash:('a-> int)->?eq:('a->'a-> bool)->'at->'a listt
group_by ?hash ?eq l groups equal elements, regardless of their order of appearance. precondition: for any x and y, if eq x y then hash x = hash y must hold.
since 2.3
val join : join_row:('a->'b->'c option)->'at->'bt->'ct
join ~join_row a b combines every element of a with every element of b using join_row. If join_row returns None, then the two elements do not combine. Assume that b allows for multiple iterations.
since 2.3
val join_by :
?eq:('key->'key-> bool)->?hash:('key-> int)->('a->'key)->('b->'key)->merge:('key->'a->'b->'c option)->'at->'bt->'ct
join_by ?eq ?hash key1 key2 ~merge la lb is a binary operation that takes two sequences a and b, projects their elements resp. with key1 and key2, and combine values (x,y) from (a,b) with the same key using merge. If merge returns None, the combination of values is discarded. precondition: for any x and y, if eq x y then hash x = hash y must hold.
since 2.3
val join_all_by :
?eq:('key->'key-> bool)->?hash:('key-> int)->('a->'key)->('b->'key)->merge:('key->'a list->'b list->'c option)->'at->'bt->'ct
join_all_by ?eq ?hash key1 key2 ~merge la lb is a binary operation that takes two sequences a and b, projects their elements resp. with key1 and key2, and, for each key k occurring in at least one of them:
compute the list l1 of elements of a that map to k
compute the list l2 of elements of b that map to k
call merge k l1 l2. If merge returns None, the combination of values is discarded, otherwise it returns Some c and c is inserted in the result.
since 2.3
val group_join_by :
?eq:('a->'a-> bool)->?hash:('a-> int)->('b->'a)->'at->'bt->('a * 'b list)t
group_join_by ?eq ?hash key la lb associates to every element x of the first sequence, all the elements y of the second sequence such that eq x (key y). Elements of the first sequences without corresponding values in the second one are mapped to [] precondition: for any x and y, if eq x y then hash x = hash y must hold.
since 2.3
val sublists_of_len :
?last:('a list->'a list option)->?offset:int ->len:int ->'a list->'a list list
sublists_of_len ?last ?offset n l returns sub-lists of l that have length n. By default, these sub-lists are non overlapping: sublists_of_len 2 [1;2;3;4;5;6] returns [1;2]; [3;4]; [5;6].
the number of elements skipped between two consecutive sub-lists. By default it is n. If offset < n, the sub-lists will overlap; if offset > n, some elements will not appear at all.
parameterlast
if provided and the last group of elements g is such that length g < n, last g is called. If last g = Some g', g' is appended; otherwise g is dropped. If last = CCOption.return, it will simply keep the last group. By default, last = fun _ -> None, i.e. the last group is dropped if shorter than n.
chunks n l returns consecutives chunks of size at most n from l. Each item of l will occur in exactly one chunk. Only the last chunk might be of length smaller than n. Invariant: (chunks n l |> List.flatten) = l.
since 3.2
val intersperse : x:'a->'a list->'a list
intersperse ~x l inserts the element x between adjacent elements of the list l.
since 2.1, but only
since 2.2 with labels
val interleave : 'a list->'a list->'a list
interleave [x1…xn] [y1…ym] is [x1,y1,x2,y2,…] and finishes with the suffix of the longest list.
mguard c is pure () if c is true, [] otherwise. This is useful to define a list by comprehension, e.g.:
# let square_even xs =
let* x = xs in
let* () = mguard (x mod 2 = 0) in
return @@ x * x;;
val square_even : int list -> int list = <fun>
# square_even [1;2;4;3;5;2];;
- : int list = [4; 16; 4]
all_ok l returns Ok l' if all elements of l are of the form Ok x, or Error e otherwise (with the first error met).
since 1.3, but only
since 2.2 with labels
val sorted_mem : cmp:('a->'a-> int)->'a->'a list-> bool
sorted_mem ~cmp x l and mem x l give the same result for any sorted list l, but potentially more efficiently.
since 3.5
val sorted_merge : cmp:('a->'a-> int)->'a list->'a list->'a list
sorted_merge ~cmp l1 l2 merges elements from both sorted list using the given comparison function cmp.
val sorted_diff : cmp:('a->'a-> int)->'a list->'a list->'a list
sorted_diff ~cmp l1 l2 returns the elements in l1 that are not in l2. Both lists are assumed to be sorted with respect to cmp and duplicate elements in the input lists are treated individually; for example, sorted_diff ~cmp [1;1;1;2;2;3] [1;2;2] would be [1;1;3]. It is the left inverse of sorted_merge; that is, sorted_diff ~cmp (sorted_merge ~cmp l1 l2) l2 is always equal to l1 for sorted lists l1 and l2.
since 3.5
val sort_uniq : cmp:('a->'a-> int)->'a list->'a list
sort_uniq ~cmp l sorts the list l using the given comparison function cmp and remove duplicate elements.
val sorted_merge_uniq : cmp:('a->'a-> int)->'a list->'a list->'a list
sorted_merge_uniq ~cmp l1 l2 merges the sorted lists l1 and l2 and removes duplicates.
since 0.10
val sorted_diff_uniq : cmp:('a->'a-> int)->'a list->'a list->'a list
sorted_diff_uniq ~cmp l1 l2 collects the elements in l1 that are not in l2 and then remove duplicates. Both lists are assumed to be sorted with respect to cmp and duplicate elements in the input lists are treated individually; for example, sorted_diff_uniq ~cmp [1;1;1;2;2] [1;2;2;2] would be [1]. sorted_diff_uniq ~cmp l1 l2 and uniq_succ ~eq (sorted_diff ~cmp l1 l2) always give the same result for sorted l1 and l2 and compatible cmp and eq.
since 3.5
val is_sorted : cmp:('a->'a-> int)->'a list-> bool
is_sorted ~cmp l returns true iff l is sorted (according to given order).
parametercmp
the comparison function.
since 0.17
val sorted_insert :
cmp:('a->'a-> int)->?uniq:bool ->'a->'a list->'a list
sorted_insert ~cmp ?uniq x l inserts x into l such that, if l was sorted, then sorted_insert x l is sorted too.
parameteruniq
if true and x is already in sorted position in l, then x is not duplicated. Default false (x will be inserted in any case).
since 0.17
val sorted_remove :
cmp:('a->'a-> int)->?all:bool ->'a->'a list->'a list
sorted_remove ~cmp x l removes x from a sorted list l such that the return value is sorted too. By default, it is the left inverse of sorted_insert; that is, sorted_remove ~cmp x (sorted_insert ~cmp x l) is equal to l for any sorted list l.
parameterall
if true then all occurrences of x will be removed. Otherwise, only the first x will be removed (if any). Default false (only the first will be removed).
since 3.5
val uniq_succ : eq:('a->'a-> bool)->'a list->'a list
uniq_succ ~eq l removes duplicate elements that occur one next to the other. Examples: uniq_succ [1;2;1] = [1;2;1]. uniq_succ [1;1;2] = [1;2].
since 0.10
val group_succ : eq:('a->'a-> bool)->'a list->'a list list
group_succ ~eq l groups together consecutive elements that are equal according to eq.
mapi ~f l is like map, but the function f is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.
iteri ~f l is like iter, but the function f is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.
val iteri2 : f:(int ->'a->'b-> unit)->'at->'bt-> unit
iteri2 ~f l1 l2 applies f to the two lists l1 and l2 simultaneously. The integer passed to f indicates the index of element.
raisesInvalid_argument
when lists do not have the same length.
since 2.0, but only
since 2.2 with labels
val foldi : f:('b->int ->'a->'b)->init:'b->'at->'b
foldi ~f ~init l is like fold but it also passes in the index of each element, from 0 to length l - 1 as additional argument to the folded function f. Tail-recursive.
val foldi2 : f:('c->int ->'a->'b->'c)->init:'c->'at->'bt->'c
foldi2 ~f ~init l1 l2 folds on the two lists l1 and l2, with index of each element passed to the function f. Computes f(… (f init i_0 l1_0 l2_0) …) i_n l1_n l2_n .
get_at_idx i l returns Some i-th element of the given list l or None if the list l is too short. If the index is negative, it will get element starting from the end of the list l.
set_at_idx i x l replaces the i-th element with x (removes the old one), or does nothing if index is too high. If the index is negative, it will set element starting from the end of the list.
insert_at_idx i x l inserts x at i-th position, between the two existing elements. If the index is too high, append at the end of the list. If the index is negative, it will insert element starting from the end of the list.
remove_at_idx i l removes element at given index i. Does nothing if the index is too high. If the index is negative, it will remove element starting from the end of the list.
Set Operators
Those operations maintain the invariant that the list does not contain duplicates (if it already satisfies it).
uniq ~eq l removes duplicates in l w.r.t the equality predicate eq. Complexity is quadratic in the length of the list, but the order of elements is preserved. If you wish for a faster de-duplication but do not care about the order, use sort_uniq.
range_by ~step i j iterates on integers from i to j included, where the difference between successive elements is step. Use a negative step for a decreasing list.
assq_opt k alist returns Some v if the given key k is present into alist. Like Assoc.assoc_opt but use physical equality instead of structural equality to compare keys. Safe version of assq.
since 1.5, but only
since 2.0 with labels
val mem_assoc : ?eq:('a->'a-> bool)->'a->('a * _)t-> bool
mem_assoc ?eq k alist returns true iff k is a key in alist. Like Assoc.mem.
since 2.0
val remove_assoc : eq:('a->'a-> bool)->'a->('a * 'b)t->('a * 'b)t
remove_assoc ~eq k alist returns the alist without the first pair with key k, if any. Like Assoc.remove.
of_seq seq builds a list from a given Seq.t. In the result, elements appear in the same order as they did in the source Seq.t. Renamed from of_std_seq since 3.0.