Module Batteries.List

module List: BatList


List operations.
type 'a t = 'a list 
The type of lists
include BatEnum.Enumerable
include BatInterfaces.Mappable

Base operations

val is_empty : 'a list -> bool
is_empty e returns true if e does not contains any element.
val cons : 'a -> 'a list -> 'a list
cons h t returns the list starting with h and continuing as t
val first : 'a list -> 'a
Returns the first element of the list, or
Raises Empty_list if the list is empty (similar to hd).
val hd : 'a list -> 'a
Similar to first, but
Raises Failure if the list is empty.
val tl : 'a list -> 'a list
Return the given list without its first element.
Raises Failure if the list is empty.
val last : 'a list -> 'a
Returns the last element of the list, or
Raises Empty_list if the list is empty. This function takes linear time.
val length : 'a list -> int
Return the length (number of elements) of the given list.
val at : 'a list -> int -> 'a
at l n returns the n-th element of the list l or
Raises Invalid_argument is the index is outside of l bounds. O(l)
val rev : 'a list -> 'a list
List reversal.
val append : 'a list -> 'a list -> 'a list
Catenate two lists. Same function as the infix operator @. Tail-recursive O(length of the first argument).
val rev_append : 'a list -> 'a list -> 'a list
List.rev_append l1 l2 reverses l1 and concatenates it to l2.
val concat : 'a list list -> 'a list
Concatenate a list of lists. The elements of the argument are all concatenated together (in the same order) to give the result. Tail-recursive (length of the argument + length of the longest sub-list).
val flatten : 'a list list -> 'a list
Same as concat.

Constructors

val make : int -> 'a -> 'a list
Similar to String.make, make n x returns a list containing n elements x.
val init : int -> (int -> 'a) -> 'a list
Similar to Array.init, init n f returns the list containing the results of (f 0),(f 1).... (f (n-1)).
Raises Invalid_argument if n < 0.

Iterators

val iter : ('a -> unit) -> 'a list -> unit
List.iter f [a1; ...; an] applies function f in turn to a1; ...; an. It is equivalent to begin f a1; f a2; ...; f an; () end.
val iteri : (int -> 'a -> unit) -> 'a list -> unit
iteri f l will call (f 0 a0);(f 1 a1) ... (f n an) where a0..an are the elements of the list l.
val map : ('a -> 'b) -> 'a list -> 'b list
map f [a0; a1; ...; an] applies function f to a0, a1, ..., an, and builds the list [f a0; f a1; ...; f an] with the results returned by f. Tail-recursive.
val rev_map : ('a -> 'b) -> 'a list -> 'b list
List.rev_map f l gives the same result as List.rev (List.map f l).
val mapi : (int -> 'a -> 'b) -> 'a list -> 'b list
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.
val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a
List.fold_left f a [b1; ...; bn] is f (... (f (f a b1) b2) ...) bn.
val fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
List.fold_right f [a0; a1; ...; an] b is f a0 (f a1 (... (f an b) ...)). Tail-recursive.
val reduce : ('a -> 'a -> 'a) -> 'a list -> 'a
List.reduce f h::t is fold_left f h t.
Raises Empty_list on empty lists.
val max : 'a list -> 'a
max l returns the largest value in l as judged by Pervasives.compare
val min : 'a list -> 'a
min l returns the smallest value in l as judged by Pervasives.compare
val sum : int list -> int
sum l returns the sum of the integers of l
val fsum : float list -> float
fsum l returns the sum of the floats of l

Iterators on two lists

val iter2 : ('a -> 'b -> unit) -> 'a list -> 'b list -> unit
List.iter2 f [a0; a1; ...; an] [b0; b1; ...; bn] calls in turn f a0 b0; f a1 b1; ...; f an bn.
Raises Different_list_size if the two lists have different lengths.
val map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
List.map2 f [a0; a1; ...; an] [b0; b1; ...; bn] is [f a0 b0; f a1 b1; ...; f an bn].
Raises Different_list_size if the two lists have different lengths. Tail-recursive.
val rev_map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
List.rev_map2 f l1 l2 gives the same result as List.rev (List.map2 f l1 l2), but is tail-recursive and more efficient.
val fold_left2 : ('a -> 'b -> 'c -> 'a) -> 'a -> 'b list -> 'c list -> 'a
List.fold_left2 f a [b0; b1; ...; bn] [c0; c1; ...; cn] is f (... (f (f a b0 c0) b1 c1) ...) bn cn.
Raises Different_list_size if the two lists have different lengths.
val fold_right2 : ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
List.fold_right2 f [a0; a1; ...; an] [b0; b1; ...; bn] c is f a0 b0 (f a1 b1 (... (f an bn c) ...)).
Raises Different_list_size if the two lists have different lengths. Tail-recursive.

List scanning

val mem : 'a -> 'a list -> bool
mem a l is true if and only if a is equal to an element of l.
val memq : 'a -> 'a list -> bool
Same as List.mem, but uses physical equality instead of structural equality to compare list elements.

Unary predicate, One list

val for_all : ('a -> bool) -> 'a list -> bool
for_all p [a1; ...; an] checks if all elements of the list satisfy the predicate p. That is, it returns (p a1) && (p a2) && ... && (p an).
val exists : ('a -> bool) -> 'a list -> bool
exists p [a1; ...; an] checks if at least one element of the list satisfies the predicate p. That is, it returns (p a1) || (p a2) || ... || (p an).

Binary predicate, Two lists

val for_all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
Same as List.for_all, but for a two-argument predicate.
Raises Invalid_argument if the two lists have different lengths.
val exists2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
Same as List.exists, but for a two-argument predicate.
Raises Invalid_argument if the two lists have different lengths.

List searching

val find : ('a -> bool) -> 'a list -> 'a
find p l returns the first element of the list l that satisfies the predicate p.
Raises Not_found if there is no value that satisfies p in the list l.
val find_exn : ('a -> bool) -> exn -> 'a list -> 'a
find_exn p e l returns the first element of l such as p x returns true or raises e if such an element has not been found.
val findi : (int -> 'a -> bool) -> 'a list -> int * 'a
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.
val find_map : ('a -> 'b option) -> 'a list -> 'b
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 BatList.filter_map.
val rfind : ('a -> bool) -> 'a list -> 'a
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.
val filter : ('a -> bool) -> 'a list -> 'a list
filter p l returns all the elements of the list l that satisfy the predicate p. The order of the elements in the input list is preserved.
val filter_map : ('a -> 'b option) -> 'a list -> 'b list
filter_map f l calls (f a0) (f a1).... (f an) where a0,a1..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).
val find_all : ('a -> bool) -> 'a list -> 'a list
find_all is another name for List.filter.
val partition : ('a -> bool) -> 'a list -> 'a list * 'a list
partition p l 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.
val index_of : 'a -> 'a list -> int option
index_of e l returns the index of the first occurrence of e in l, or None if there is no occurrence of e in l
val index_ofq : 'a -> 'a list -> int option
index_ofq e l behaves as index_of e l except it uses physical equality
val rindex_of : 'a -> 'a list -> int option
rindex_of e l returns the index of the last occurrence of e in l, or None if there is no occurrence of e in l
val rindex_ofq : 'a -> 'a list -> int option
rindex_ofq e l behaves as rindex_of e l except it uses physical equality
val unique : ?eq:('a -> 'a -> bool) -> 'a list -> 'a list
unique cmp l returns the list l without any duplicate element. The default comparator ( = ) is used if no comparison function specified.

Implementation Note: The current implementation removes any elements where the tail of the list contains an equal element, thus it keeps the *last* copy of each equal element.

This function takes O(n^2) time.
Since 2.0
See also sort_unique to save time in cases when reordering the list is acceptable

val unique_cmp : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list
As unique, except comparator parameter returns an int. Default comparator is Pervasives.compare. This function takes O(n log n) time.

Implementation Note: The current implementation removes subsequent elements that compare as equal to earlier elements in the list, thus it keeps the *first* copy of each equal element.
Since 1.3.0

val unique_hash : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a list -> 'a list
As unique, except uses a hash table to cut down the expected runtime to linear, assuming a good hash function. ?hash defaults to Hashtbl.hash and ?eq defaults to (=).

Implementation Note: The current implementation removes subsequent elements that hash and compare as equal to earlier elements in the list, thus it keeps the *first* copy of each equal element.
Since 2.0.0


Association lists

val assoc : 'a -> ('a * 'b) list -> 'b
assoc a l returns the value associated with key a in the list of pairs l. That is, assoc a [ ...; (a,b); ...] = b if (a,b) is the leftmost binding of a in list l.
Raises Not_found if there is no value associated with a in the list l.
val assoc_inv : 'b -> ('a * 'b) list -> 'a
assoc_inv b l returns the key associated with value b in the list of pairs l. That is, assoc b [ ...; (a,b); ...] = a if (a,b) is the leftmost binding of a in list l.
Raises Not_found if there is no key associated with b in the list l.
val remove_assoc : 'a -> ('a * 'b) list -> ('a * 'b) list
remove_assoc a l returns the list of pairs l without the first pair with key a, if any. Tail-recursive.
val mem_assoc : 'a -> ('a * 'b) list -> bool
Same as List.assoc, but simply return true if a binding exists, and false if no bindings exist for the given key.
val assq : 'a -> ('a * 'b) list -> 'b
Same as List.assoc, but uses physical equality instead of structural equality to compare keys.
val assq_inv : 'b -> ('a * 'b) list -> 'a
Same as List.assoc_inv, but uses physical equality instead of structural equality to compare keys.
val remove_assq : 'a -> ('a * 'b) list -> ('a * 'b) list
Same as List.remove_assoc, but uses physical equality instead of structural equality to compare keys. Tail-recursive.
val mem_assq : 'a -> ('a * 'b) list -> bool
Same as List.mem_assoc, but uses physical equality instead of structural equality to compare keys.

List transformations

val split_at : int -> 'a list -> 'a list * 'a list
split_at n l returns two lists l1 and l2, l1 containing the first n elements of l and l2 the others.
Raises Invalid_argument if n is outside of l size bounds.
val split_nth : int -> 'a list -> 'a list * 'a list
Obsolete. As split_at.
val remove : 'a list -> 'a -> 'a list
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 ( = ).
val remove_if : ('a -> bool) -> 'a list -> 'a list
remove_if cmp l is similar to remove, but with cmp used instead of ( = ).
val remove_all : 'a list -> 'a -> 'a list
remove_all l x is similar to remove but removes all elements that are equal to x and not only the first one.
val take : int -> 'a list -> 'a list
take n l returns up to the n first elements from list l, if available.
val drop : int -> 'a list -> 'a list
drop n l returns l without the first n elements, or the empty list if l have less than n elements.
val take_while : ('a -> bool) -> 'a list -> 'a list
take_while p xs returns the (possibly empty) longest prefix of elements of xs that satisfy the predicate p.
val drop_while : ('a -> bool) -> 'a list -> 'a list
drop_while p xs returns the suffix remaining after takeWhile p xs.
val interleave : ?first:'a -> ?last:'a -> 'a -> 'a list -> 'a list
interleave ~first ~last sep [a0;a1;a2;...;an] returns first; a0; sep; a1; sep; a2; sep; ...; sep; an; last

BatEnum functions

Abstraction layer.

val enum : 'a list -> 'a BatEnum.t
Returns an enumeration of the elements of a list. This enumeration may be used to visit elements of the list in forward order (i.e. from the first element to the last one)
val of_enum : 'a BatEnum.t -> 'a list
Build a list from an enumeration. In the result, elements appear in the same order as they did in the source enumeration.
val backwards : 'a list -> 'a BatEnum.t
Returns an enumeration of the elements of a list. This enumeration may be used to visit elements of the list in backwards order (i.e. from the last element to the first one)
val of_backwards : 'a BatEnum.t -> 'a list
Build a list from an enumeration. The first element of the enumeration becomes the last element of the list, the second element of the enumeration becomes the second-to-last element of the list...

List of pairs

val split : ('a * 'b) list -> 'a list * 'b list
Transform a list of pairs into a pair of lists: split [(a0,b0); (a1,b1); ...; (an,bn)] is ([a0; a1; ...; an], [b0; b1; ...; bn]). Tail-recursive.
val combine : 'a list -> 'b list -> ('a * 'b) list
Transform a pair of lists into a list of pairs: combine [a0; a1; ...; an] [b0; b1; ...; bn] is [(a0,b0); (a1,b1); ...; (an,bn)].
Raises Different_list_size if the two lists have different lengths. Tail-recursive.

Sorting

val sort : ('a -> 'a -> int) -> 'a list -> 'a list
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 resulting list is sorted in increasing order. List.sort is guaranteed to run in constant heap space (in addition to the size of the result list) and logarithmic stack space.

The current implementation uses Merge Sort. It runs in constant heap space and logarithmic stack space.

val stable_sort : ('a -> 'a -> int) -> 'a list -> 'a list
Same as List.sort, but the sorting algorithm is guaranteed to be stable (i.e. elements that compare equal are kept in their original order) .

The current implementation uses Merge Sort. It runs in constant heap space and logarithmic stack space.

val fast_sort : ('a -> 'a -> int) -> 'a list -> 'a list
Same as List.sort or List.stable_sort, whichever is faster on typical input.
val merge : ('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
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. Not tail-recursive (sum of the lengths of the arguments).
val sort_unique : ('a -> 'a -> int) -> 'a list -> 'a list
sort_unique cmp l returns the list l sorted and without any duplicate element. cmp is a usual comparison function providing total order.

This function takes O(n log n) time.


Utilities

val group : ('a -> 'a -> int) -> 'a list -> 'a list list
group cmp l returns list of groups and each group consists of elements judged equal by comparison function cmp. Groups in the resulting list appear in order given by cmp. All groups are always nonempty. group returns [] only if l is empty.

For example group cmp [f;c;b;e;d;a] can give [[a;b];[c];[d;e;f]] if following conditions are met: cmp a b = 0, cmp b c = -1, cmp c d = -1, cmp d e = 0,...

val cartesian_product : 'a list -> 'b list -> ('a * 'b) list
Different from List.combine, this returns every pair of elements formed out of the two lists. cartesian_product [a0; a1; ...; an] [b0; b1; ...; bn] = [(a0,b0);(a0,b1); ...; (a0,bn); (a1,b0); ..; (a1, bn); ...; (an,bn)]. The lists can be of unequal size.
val n_cartesian_product : 'a list list -> 'a list list
Given n lists, return the n-way cartesian product of these lists. Given [a;b];[c];[d;e;f], returns [a;c;d];[a;c;e];[a;c;f];[b;c;d];[b;c;e];[b;c;f], all ways of choosing one element from each input list.
val transpose : 'a list list -> 'a list list
Transposes a list of lists, turning rows of the input into columns of the output and vice versa.
Since 2.0.0

Boilerplate code


Printing

val print : ?first:string ->
?last:string ->
?sep:string ->
('a BatInnerIO.output -> 'b -> unit) ->
'a BatInnerIO.output -> 'b list -> unit
Print the contents of a list
val eq : 'a BatOrd.eq -> 'a list BatOrd.eq
val ord : 'a BatOrd.ord -> 'a list BatOrd.ord
val compare : 'a BatOrd.comp -> 'a list BatOrd.comp

Comparison and equality for lists based on element comparison and equality
module Eq: 
functor (T : BatOrd.Eq) -> Eq with type t = T.t list
module Ord: 
functor (T : BatOrd.Ord) -> Ord with type t = T.t list
module Comp: 
functor (T : BatOrd.Comp) -> Comp with type t = T.t list

Obsolete functions

val nth : 'a list -> int -> 'a
Obsolete. As at.
val takewhile : ('a -> bool) -> 'a list -> 'a list
obsolete, as BatList.take_while
val dropwhile : ('a -> bool) -> 'a list -> 'a list
obsolete, as BatList.drop_while

Override modules


The following modules replace functions defined in List with functions behaving slightly differently but having the same name. This is by design: the functions meant to override the corresponding functions of List.
module Exceptionless: sig .. end
Exceptionless counterparts for error-raising operations
module Infix: sig .. end
Infix submodule regrouping all infix operators
module Labels: sig .. end
Operations on List with labels.
val (@) : 'a list -> 'a list -> 'a list
Tail recursive List.append.