package ocaml-base-compiler
-
bigarray
-
dynlink
-
ocamlbytecomp
-
ocamlcommon
-
ocamlmiddleend
-
ocamloptcomp
-
odoc_info
-
stdlib
-
str
-
unix
Library
Module
Module type
Parameter
Class
Class type
List operations.
Some functions are flagged as not tail-recursive. A tail-recursive function uses constant stack space, while a non-tail-recursive function uses stack space proportional to the length of its list argument, which can be a problem with very long lists. When the function takes several list arguments, an approximate formula giving stack usage (in some unspecified constant unit) is shown in parentheses.
The above considerations can usually be ignored if your lists are not longer than about 10000 elements.
The labeled version of this module can be used as described in the StdLabels
module.
Compare the lengths of two lists. compare_lengths l1 l2
is equivalent to compare (length l1) (length l2)
, except that the computation stops after reaching the end of the shortest list.
- since 4.05.0
Compare the length of a list to an integer. compare_length_with l len
is equivalent to compare (length l) len
, except that the computation stops after at most len
iterations on the list.
- since 4.05.0
Return the first element of the given list.
- raises Failure
if the list is empty.
Return the given list without its first element.
- raises Failure
if the list is empty.
Return the n
-th element of the given list. The first element (head of the list) is at position 0.
- raises Failure
if the list is too short.
- raises Invalid_argument
if
n
is negative.
Return the n
-th element of the given list. The first element (head of the list) is at position 0. Return None
if the list is too short.
- raises Invalid_argument
if
n
is negative.
- since 4.05
init ~len ~f
is f 0; f 1; ...; f (len-1)
, evaluated left to right.
- raises Invalid_argument
if
len < 0
.
- since 4.06.0
Concatenate two lists. Same function as the infix operator @
. Not tail-recursive (length of the first argument). The @
operator is not tail-recursive either.
rev_append l1 l2
reverses l1
and concatenates it with l2
. This is equivalent to (
rev
l1) @ l2
, but rev_append
is tail-recursive and more efficient.
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).
Same as concat
. Not tail-recursive (length of the argument + length of the longest sub-list).
Comparison
equal eq [a1; ...; an] [b1; ..; bm]
holds when the two input lists have the same length, and for each pair of elements ai
, bi
at the same position we have eq ai bi
.
Note: the eq
function may be called even if the lists have different length. If you know your equality function is costly, you may want to check compare_lengths
first.
- since 4.12.0
compare cmp [a1; ...; an] [b1; ...; bm]
performs a lexicographic comparison of the two input lists, using the same 'a -> 'a -> int
interface as Stdlib.compare
:
a1 :: l1
is smaller thana2 :: l2
(negative result) ifa1
is smaller thana2
, or if they are equal (0 result) andl1
is smaller thanl2
- the empty list
[]
is strictly smaller than non-empty lists
Note: the cmp
function will be called even if the lists have different lengths.
- since 4.12.0
Iterators
iter ~f [a1; ...; an]
applies function f
in turn to a1; ...; an
. It is equivalent to begin f a1; f a2; ...; f an; () end
.
Same as iter
, but the function is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.
- since 4.00.0
map ~f [a1; ...; an]
applies function f
to a1, ..., an
, and builds the list [f a1; ...; f an]
with the results returned by f
. Not tail-recursive.
Same as map
, but the function is applied to the index of the element as first argument (counting from 0), and the element itself as second argument. Not tail-recursive.
- since 4.00.0
filter_map ~f l
applies f
to every element of l
, filters out the None
elements and returns the list of the arguments of the Some
elements.
- since 4.08.0
fold_left_map
is a combination of fold_left
and map
that threads an accumulator through calls to f
.
- since 4.11.0
fold_left ~f ~init [b1; ...; bn]
is f (... (f (f init b1) b2) ...) bn
.
fold_right ~f [a1; ...; an] ~init
is f a1 (f a2 (... (f an init) ...))
. Not tail-recursive.
Iterators on two lists
iter2 ~f [a1; ...; an] [b1; ...; bn]
calls in turn f a1 b1; ...; f an bn
.
- raises Invalid_argument
if the two lists are determined to have different lengths.
map2 ~f [a1; ...; an] [b1; ...; bn]
is [f a1 b1; ...; f an bn]
.
- raises Invalid_argument
if the two lists are determined to have different lengths. Not tail-recursive.
fold_left2 ~f ~init [a1; ...; an] [b1; ...; bn]
is f (... (f (f init a1 b1) a2 b2) ...) an bn
.
- raises Invalid_argument
if the two lists are determined to have different lengths.
fold_right2 ~f [a1; ...; an] [b1; ...; bn] ~init
is f a1 b1 (f a2 b2 (... (f an bn init) ...))
.
- raises Invalid_argument
if the two lists are determined to have different lengths. Not tail-recursive.
List scanning
for_all ~f [a1; ...; an]
checks if all elements of the list satisfy the predicate f
. That is, it returns (f a1) && (f a2) && ... && (f an)
for a non-empty list and true
if the list is empty.
exists ~f [a1; ...; an]
checks if at least one element of the list satisfies the predicate f
. That is, it returns (f a1) || (f a2) || ... || (f an)
for a non-empty list and false
if the list is empty.
Same as for_all
, but for a two-argument predicate.
- raises Invalid_argument
if the two lists are determined to have different lengths.
Same as exists
, but for a two-argument predicate.
- raises Invalid_argument
if the two lists are determined to have different lengths.
mem a ~set
is true if and only if a
is equal to an element of set
.
Same as mem
, but uses physical equality instead of structural equality to compare list elements.