Shadowing unsafe functions to avoid all exceptions.
Safe lookups, scans, retrievals
Return option rather than raise Not_found, Failure _, or Invalid_argument _
val hd : 'a list->'a option
hd xs is the head (first element) of the list or None if the list is empty.
val tl : 'a list->'a list option
tl xs is the tail of the list (the whole list except the first element) or None if the list is empty.
val nth : 'a list->int ->'a option
nth xs n is the nth element of the list or None if the list has fewer than n elements.
nth xs 0 = tl xs
val nth_opt : 'a list->int ->'a option
nth_opt is an alias for nth provided for backwards compatibility.
val last : 'a->'a list->'a
last x xs is the last element of the list xs or x if xs is empty.
The primary intended use for last is after destructing a list: match l with | None -> … | Some x :: xs -> last x xs but it can also be used for a default value: last default_value_if_empty xs.
val last_opt : 'a list->'a option
last_opt xs is the last element of the list xs or None if the list xs is empty.
val find : ('a-> bool)->'a list->'a option
find predicate xs is the first element x of the list xs such that predicate x is true or None if the list xs has no such element.
val find_opt : ('a-> bool)->'a list->'a option
find_opt is an alias for find provided for backwards compatibility.
val mem : equal:('a->'a-> bool)->'a->'a list-> bool
mem ~equal a l is true iff there is an element e of l such that equal a e.
val assoc : equal:('a->'a-> bool)->'a->('a * 'b) list->'b option
assoc ~equal k kvs is Some v such that (k', v) is the first pair in the list such that equal k' k or None if the list contains no such pair.
val assoc_opt : equal:('a->'a-> bool)->'a->('a * 'b) list->'b option
assoc_opt is an alias for assoc provided for backwards compatibility.
val assq : 'a->('a * 'b) list->'b option
assq k kvs is the same as assoc ~equal:Stdlib.( == ) k kvs: it uses the physical equality.
val assq_opt : 'a->('a * 'b) list->'b option
assq_opt is an alias for assq provided for backwards compatibility.
val mem_assoc : equal:('a->'a-> bool)->'a->('a * 'b) list-> bool
mem_assoc ~equal k l is equivalent to Option.is_some @@ assoc ~equal k l.
val mem_assq : 'a->('a * 'b) list-> bool
mem_assq k l is mem_assoc ~equal:Stdlib.( == ) k l.
val remove_assoc :
equal:('a->'a-> bool)->'a->('a * 'b) list->('a * 'b) list
remove_assoc ~equal k l is l without the first element (k', _) such that equal k k'.
val remove_assq : 'a->('a * 'b) list->('a * 'b) list
remove_assoq k l is remove_assoc ~equal:Stdlib.( == ) k l.
Initialisation
val init :
when_negative_length:'trace->int ->(int ->'a)->('a list, 'trace)result
init ~when_negative_length n f is Error when_negative_length if n is strictly negative and Ok (Stdlib.List.init n f) otherwise.
Basic traversal
val length : 'a list-> int
val rev : 'a list->'a list
val concat : 'a list list->'a list
val append : 'a list->'a list->'a list
val rev_append : 'a list->'a list->'a list
val flatten : 'a list list->'a list
Double-list traversals
These safe-wrappers take an explicit value to handle the case of lists of unequal length.
val combine :
when_different_lengths:'trace->'a list->'b list->(('a * 'b) list, 'trace)result
combine ~when_different_lengths l1 l2 is either
Error when_different_lengths if List.length l1 <> List.length l2
a list of pairs of elements from l1 and l2
E.g., combine ~when_different_lengths [] [] = Ok []
Note: combine ~when_different_lengths l1 l2 is equivalent to try Ok (Stdlib.List.combine l1 l2)
with Invalid_argument _ -> when_different_lengths
The same equivalence almost holds for the other double traversors below. The notable difference is if the functions passed as argument to the traversors raise the Invalid_argument _ exception.
val rev_combine :
when_different_lengths:'trace->'a list->'b list->(('a * 'b) list, 'trace)result
rev_combine ~when_different_lengths xs ys is rev (combine ~when_different_lengths xs ys) but more efficient.
val split : ('a * 'b) list->'a list * 'b list
val iter2 :
when_different_lengths:'trace->('a->'b-> unit)->'a list->'b list->(unit, 'trace)result
val map2 :
when_different_lengths:'trace->('a->'b->'c)->'a list->'b list->('c list, 'trace)result
val rev_map2 :
when_different_lengths:'trace->('a->'b->'c)->'a list->'b list->('c list, 'trace)result
val fold_left2 :
when_different_lengths:'trace->('a->'b->'c->'a)->'a->'b list->'c list->('a, 'trace)result
val fold_right2 :
when_different_lengths:'trace->('a->'b->'c->'c)->'a list->'b list->'c->('c, 'trace)result
This function is not tail-recursive
val for_all2 :
when_different_lengths:'trace->('a->'b-> bool)->'a list->'b list->(bool, 'trace)result
val exists2 :
when_different_lengths:'trace->('a->'b-> bool)->'a list->'b list->(bool, 'trace)result
Monad-aware variants
The functions below are strict extensions of the standard Stdlib.List module. It is for result-, lwt- and lwt-result-aware variants. The meaning of the suffix is as described above, in Lwtreslib, and in Sigs.Seq.
Initialisation variants
Note that for asynchronous variants (_s, _es, _p, and _ep), if the length parameter is negative, then the promise is returned already fulfilled with Error when_different_lengths.
val init_e :
when_negative_length:'trace->int ->(int ->('a, 'trace)result)->('a list, 'trace)result
val init_s :
when_negative_length:'trace->int ->(int ->'aLwt.t)->('a list, 'trace)resultLwt.t
val init_es :
when_negative_length:'trace->int ->(int ->('a, 'trace)resultLwt.t)->('a list, 'trace)resultLwt.t
val init_ep :
when_negative_length:'error->int ->(int ->('a, 'error)resultLwt.t)->('a list, 'error list)resultLwt.t
val init_p :
when_negative_length:'trace->int ->(int ->'aLwt.t)->('a list, 'trace)resultLwt.t
Query variants
val find_e :
('a->(bool, 'trace)result)->'a list->('a option, 'trace)result
val find_s : ('a->bool Lwt.t)->'a list->'a optionLwt.t
val exists_ep :
('a->(bool, 'trace)resultLwt.t)->'a list->(bool, 'trace list)resultLwt.t
val exists_p : ('a->bool Lwt.t)->'a list->bool Lwt.t
Double-scanning variants
As mentioned above, there are no _p and _ep double-scanners. Use combine (and variants) to circumvent this.
val for_all2_e :
when_different_lengths:'trace->('a->'b->(bool, 'trace)result)->'a list->'b list->(bool, 'trace)result
val for_all2_s :
when_different_lengths:'trace->('a->'b->bool Lwt.t)->'a list->'b list->(bool, 'trace)resultLwt.t
val for_all2_es :
when_different_lengths:'trace->('a->'b->(bool, 'trace)resultLwt.t)->'a list->'b list->(bool, 'trace)resultLwt.t
val exists2_e :
when_different_lengths:'trace->('a->'b->(bool, 'trace)result)->'a list->'b list->(bool, 'trace)result
val exists2_s :
when_different_lengths:'trace->('a->'b->bool Lwt.t)->'a list->'b list->(bool, 'trace)resultLwt.t
val exists2_es :
when_different_lengths:'trace->('a->'b->(bool, 'trace)resultLwt.t)->'a list->'b list->(bool, 'trace)resultLwt.t
Combine variants
These are primarily intended to be used for preprocessing before applying a traversor to the resulting list of pairs. They give alternatives to the when_different_lengths mechanism of the immediate double-traversors above.
In case the semantic of, say, map2_es was unsatisfying, one can use map_es on a combine-preprocessed pair of lists. The different variants of combine give different approaches to different-length handling.
val combine_drop : 'a list->'b list->('a * 'b) list
combine_drop ll lr is a list l of pairs of elements taken from the common-length prefix of ll and lr. The suffix of whichever list is longer (if any) is dropped.
More formally nth l n is:
None if n >= min (length ll) (length lr)
Some (Option.get @@ nth ll n, Option.get @@ nth lr n) otherwise
val combine_with_leftovers :
'a list->'b list->('a * 'b) list * [ `Left of 'a list| `Right of 'b list ] option
combine_with_leftovers ll lr is a tuple (combined, leftover) where combined is combine_drop ll lr and leftover is either `Left lsuffix or `Right rsuffix depending on which of ll or lr is longer. leftover is None if the two lists have the same length.
compare / equal
val compare : ('a->'a-> int)->'a list->'a list-> int
val equal : ('a->'a-> bool)->'a list->'a list-> bool
Sorting
val sort : ('a->'a-> int)->'a list->'a list
val stable_sort : ('a->'a-> int)->'a list->'a list