Tail recursive version of standard List functions, plus additional operations.
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.
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 containting all the
elements of l1
and l2
. If several elements compare equal, the elements of l1
will be before the elements of l2
.
find_exn t ~f
returns the first element of t
that satisfies f
. It raises
Not_found
if there is no such element.
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.
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
.
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.
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).
init n ~f
is [(f 0); (f 1); ...; (f (n-1))]
. It is an error if n < 0
.
Note that sub
, unlike slice
, doesn't use python-style indices!
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.
intersperse xs ~sep
places sep
between adjacent elements of xs
.
e.g. intersperse [1;2;3] ~sep:0 = [1;0;2;0;3]