Tail recursive version of standard List functions, plus additional operations.
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
l that satisfy the predicate
l2 is the list of all the
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
l2 are sorted according to the comparison
merge cmp l1 l2 will return a sorted list containting all the
l2. If several elements compare equal, the elements of
will be before the elements of
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.
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']
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
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
t, and raise
Duplicate_found if a duplicate is found. The
context is the second argument of
count l ~f is the number of elements in
l that satisfy the
range ?stride ?start ?stop start_i stop_i is the list of integers from
stop_i, stepping by
stride < 0 then we need
the result to be nonempty (or
stop_i in the case where both bounds are
init n ~f is
[(f 0); (f 1); ...; (f (n-1))]. It is an error if
n < 0.
slice, doesn't use python-style indices!
is_sorted t ~compare returns
true iff forall adjacent
a1; a2 in
a2 <= 0.
is_sorted_strictly is similar, except it uses
< instead of
transpose m transposes the rows and columns of the matrix
considered as either a row of column lists or (dually) a column of row lists.
On non-empty rectangular matrices,
transpose is an involution
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
intersperse [1;2;3] ~sep:0 = [1;0;2;0;3]