List operations.
OCaml's lists are immutable, singly-linked lists, which therefore give fast access to the front of the list, and slow (i.e., O(n)) access to the back of the list. The comparison functions on lists are lexicographic.
Checks whether the provided element is there, using polymorphic compare if equal
is not provided
fold t ~init ~f
returns f (... f (f (f init e1) e2) e3 ...) en
, where e1..en
are the elements of t
Returns true
if and only if there exists an element for which the provided
function evaluates to true
. This is a short-circuiting operation.
Returns true
if and only if the provided function evaluates to true
for all
elements. This is a short-circuiting operation.
Returns the number of elements for which the provided function evaluates to true.
Returns as an option
the first element for which f
evaluates to true.
Returns the first evaluation of f
that returns Some
, and returns None
if there
is no such element.
Returns a minimum (resp maximum) element from the collection using the provided
cmp
function, or None
if the collection is empty. In case of a tie, the first
element encountered while traversing the collection is returned. The implementation
uses fold
so it has the same complexity as fold
.
A monad is an abstraction of the concept of sequencing of computations. A value of type 'a monad represents a computation that returns a value of type 'a.
return v
returns the (trivial) computation that returns v.
Return the n
-th element of the given list.
The first element (head of the list) is at position 0.
Raise if the list is too short or n
is negative.
fold_left
is the same as fold
, and one should always use fold
rather than
fold_left
, except in functors that are parameterized over a more general signature
where this equivalence does not hold.
Like List.for_all, but passes the index as an argument.
Like List.exists, but passes the index as an argument.
partition_tf l ~f
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. The "tf" suffix is mnemonic to remind readers at a call that the result
is (trues, falses).
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 current implementation uses Merge Sort. It runs in linear heap space and logarithmic stack space.
Presently, the sort is stable, meaning that two equal elements in the input will be in the same order in the output.
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 containing all the
elements of l1
and l2
. If several elements compare equal, the elements of l1
will be before the elements of l2
.
Return the first element of the given list. Raise if the list is empty.
find_exn t ~f
returns the first element of t
that satisfies f
. It raises
Not_found
if there is no such element.
Returns the first evaluation of f
that returns Some
. Raises Not_found
if f
always returns None
.
Like find_map
and find_map_exn
, but pass the index as an argument.
List.fold_right [a1; ...; an] ~f ~init:b
is
f a1 (f a2 (... (f an b) ...))
.
iteri is just like iter, but it also passes in the index of each element as the first argument to the iter'd function. Tail-recursive.
foldi is just like fold, but it also passes in the index of each element as the first argument to the folded function. Tail-recursive.
reduce_exn [a1; ...; an] ~f
is f (... (f (f a1 a2) a3) ...) an
. It fails on the
empty list. Tail recursive.
reduce_balanced
returns the same value as reduce
when f
is associative, but
differs in that the tree of nested applications of f
has logarithmic depth.
This is useful when your 'a
grows in size as you reduce it and f
becomes more
expensive with bigger inputs. For example, reduce_balanced ~f:(^)
takes n*log(n)
time, while reduce ~f:(^)
takes quadratic time.
group l ~break
returns a list of lists (i.e., groups) whose concatenation is
equal to the original list. Each group is broken where break returns true on
a pair of successive elements.
Example
group ~break:(<>) 'M';'i';'s';'s';'i';'s';'s';'i';'p';'p';'i'
->
['M'];['i'];['s';'s'];['i'];['s';'s'];['i'];['p';'p'];['i']
This is just like group, except that you get the index in the original list of the current element along with the two elements.
Example, group the chars of Mississippi into triples
groupi ~break:(fun i _ _ -> i mod 3 = 0)
'M';'i';'s';'s';'i';'s';'s';'i';'p';'p';'i'
->
['M'; 'i'; 's']; ['s'; 'i'; 's']; ['s'; 'i'; 'p']; ['p'; 'i']
The final element of a list. The _exn version raises on the empty list.
find_consecutive_duplicate t ~equal
returns the first pair of consecutive elements
(a1, a2)
in t
such that equal a1 a2
. They are returned in the same order as
they appear in t
. equal
need not be an equivalence relation; it is simply used as
a predicate on consecutive elements.
contains_dup
True if there are any two elements in the list which are the same.
find_a_dup
returns a duplicate from the list (no guarantees about which
duplicate you get), or None if there are no dups.
find_all_dups
returns a list of all elements that occur more than once, with
no guarantees about order.
exn_if_dup ?compare ?context t ~to_sexp
will run find_a_dup
on t
, and raise
Duplicate_found
if a duplicate is found. The context
is the second argument of
the exception
count l ~f
is the number of elements in l
that satisfy the predicate f
.
range ?stride ?start ?stop start_i stop_i
is the list of integers from start_i
to
stop_i
, stepping by stride
. If stride
< 0 then we need start_i
> stop_i
for
the result to be nonempty (or start_i
= stop_i
in the case where both bounds are
inclusive).
range'
is analogous to range
for general start/stop/stride types. range'
raises
if stride x
returns x
or if the direction that stride x
moves x
changes from
one call to the next.
init n ~f
is [(f 0); (f 1); ...; (f (n-1))]
. It is an error if n < 0
.
permute ?random_state t
returns a permutation of t
.
permute
side affects random_state
by repeated calls to Random.State.int
.
If random_state
is not supplied, permute
uses Random.State.default
.
is_sorted t ~compare
returns true
iff forall adjacent a1; a2
in t
, compare a1
a2 <= 0
.
is_sorted_strictly
is similar, except it uses <
instead of <=
.
transpose m
transposes the rows and columns of the matrix m
,
considered as either a row of column lists or (dually) a column of row lists.
Example,
transpose [1;2;3];[4;5;6]
= [1;4];[2;5];[3;6]
On non-empty rectangular matrices, transpose
is an involution
(i.e., transpose (transpose m) = m
). Transpose returns None when called
on lists of lists with non-uniform lengths.
*
Quickcheck generator for lists with additional customization.
List.gen' t
produces a generator for arbitrary lists of values from t
.
Adding ~unique:true
guarantees that every value from t
is included in each list
at most once.
~length:(`Exactly n)
produces only lists of length n
.~length:(`At_least n)
produces only lists of length n
or greater.~length:(`At_most n)
produces only lists of length n
or less.~length:(`Between_inclusive (m,n))
produces only lists of length k
such
that m <= k
and k <= n
.Adding ~sorted:`Arbitrarily
produces lists that are sorted in a deterministic
order based on the construction of t
, but not guaranteed to correspond to any
specific comparison function.
Adding ~sorted:(`By cmp)
produces lists that are sorted in ascending order by
cmp
.
The optional arguments can be combined; for example, the following expression creates lists of 10 to 20 integers each that are strictly sorted (no duplicates):
gen' Int.gen
~unique:true
~sorted:(`By Int.compare)
~length:(`Between_inclusive (10,20))
Regardless of the options provided, the lists in the output of list t
are
generated uniquely, so long as the values in t
are generated uniquely.