Additional and modified functions for lists.
The OCaml standard library provides a module for list functions. This ExtList module can be used to override the List module or as a standalone module. It provides new functions and modify the behavior of some other ones (in particular all functions are now tail-recursive).
Similar to Array.init
, init n f
returns the list containing
the results of (f 0),(f 1).... (f (n-1)).
Raise Invalid_arg "ExtList.init"
if n < 0.
Similar to String.make
, make n x
returns a
list containing n
elements x
.
Returns the first element of the list, or raise Empty_list
if
the list is empty (similar to hd
).
Returns the last element of the list, or raise Empty_list
if
the list is empty. This function takes linear time.
iteri f l
will call (f 0 a0);(f 1 a1) ... (f n an)
where
a0..an
are the elements of the list l
.
mapi f l
will build the list containing
(f 0 a0);(f 1 a1) ... (f n an)
where a0..an
are the elements of
the list l
.
rfind p l
returns the last element x
of l
such as p x
returns
true
or raises Not_found
if such element as not been found.
find_exc p e l
returns the first element of l
such as p x
returns true
or raises e
if such element as not been found.
findi p e l
returns the first element ai
of l
along with its
index i
such that p i ai
is true, or raises Not_found
if no
such element has been found.
unique cmp l
returns the list l
without any duplicate element.
Default comparator ( = ) is used if no comparison function specified.
filter_map f l
call (f a0) (f a1).... (f an)
where a0..an
are
the elements of l
. It returns the list of elements bi
such as
f ai = Some bi
(when f
returns None
, the corresponding element of
l
is discarded).
find_map pred list
finds the first element of list
for which
pred element
returns Some r
. It returns r
immediately
once found or raises Not_found
if no element matches the
predicate. See also filter_map.
split_nth n l
returns two lists l1
and l2
, l1
containing the
first n
elements of l
and l2
the others. Raise Invalid_index
if
n
is outside of l
size bounds.
remove l x
returns the list l
without the first element x
found
or returns l
if no element is equal to x
. Elements are compared
using ( = ).
remove_if cmp l
is similar to remove
, but with cmp
used
instead of ( = ).
remove_all l x
is similar to remove
but removes all elements that
are equal to x
and not only the first one.
take n l
returns up to the n
first elements from list l
, if
available.
drop n l
returns l
without the first n
elements, or the empty
list if l
have less than n
elements.
takewhile f xs
returns the first elements of list xs
which satisfy the predicate f
.
dropwhile f xs
returns the list xs
with the first
elements satisfying the predicate f
dropped.
Enumerations are important in ExtLib, they are a good way to work with abstract enumeration of elements, regardless if they are located in a list, an array, or a file.
Some minor modifications have been made to the specification of some functions, especially concerning exceptions raised.
Returns the first element of the list or raise Empty_list
if the
list is empty.
Returns the list without its first elements or raise Empty_list
if
the list is empty.
nth l n
returns the n-th element of the list l
or raise
Invalid_index
is the index is outside of l
bounds.
Sort the list using optional comparator (by default compare
).
The following functions have been improved so all of them are
tail-recursive. They have also been modified so they no longer
raise Invalid_arg
but Different_list_size
when used on two
lists having a different number of elements.
The following functions have the same behavior as the List
module ones but are tail-recursive. That means they will not
cause a Stack_overflow
when used on very long list.
The implementation might be a little more slow in bytecode, but compiling in native code will not affect performances.
The following functions were already tail-recursive in the List
module but were using List.rev
calls. The new implementations
have better performances.
These functions are already part of the Ocaml standard library and have not been modified. Please refer to the Ocaml Manual for documentation.
Empty_list
is raised when an operation applied on an empty list
is invalid : hd
for example.
Invalid_index
is raised when an indexed access on a list is
out of list bounds.
Different_list_size
is raised when applying functions such as
iter2
on two lists having different size.
the new implementation for ( @ ) operator, see List.append
.