Module Util.List
include CList.S
val length : 'a list -> intval compare_lengths : 'a list -> 'b list -> intval compare_length_with : 'a list -> int -> intval cons : 'a -> 'a list -> 'a listval hd : 'a list -> 'aval tl : 'a list -> 'a listval nth : 'a list -> int -> 'aval nth_opt : 'a list -> int -> 'a optionval rev : 'a list -> 'a listval init : int -> (int -> 'a) -> 'a listval append : 'a list -> 'a list -> 'a listval rev_append : 'a list -> 'a list -> 'a listval concat : 'a list list -> 'a listval flatten : 'a list list -> 'a listval iter : ('a -> unit) -> 'a list -> unitval iteri : (int -> 'a -> unit) -> 'a list -> unitval map : ('a -> 'b) -> 'a list -> 'b listval mapi : (int -> 'a -> 'b) -> 'a list -> 'b listval rev_map : ('a -> 'b) -> 'a list -> 'b listval filter_map : ('a -> 'b option) -> 'a list -> 'b listval concat_map : ('a -> 'b list) -> 'a list -> 'b listval fold_left_map : ('a -> 'b -> 'a * 'c) -> 'a -> 'b list -> 'a * 'c listval fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'aval fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'bval iter2 : ('a -> 'b -> unit) -> 'a list -> 'b list -> unitval map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c listval rev_map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c listval fold_left2 : ('a -> 'b -> 'c -> 'a) -> 'a -> 'b list -> 'c list -> 'aval fold_right2 : ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'cval for_all : ('a -> bool) -> 'a list -> boolval exists : ('a -> bool) -> 'a list -> boolval for_all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> boolval exists2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> boolval mem : 'a -> 'a list -> boolval memq : 'a -> 'a list -> boolval find : ('a -> bool) -> 'a list -> 'aval find_opt : ('a -> bool) -> 'a list -> 'a optionval find_map : ('a -> 'b option) -> 'a list -> 'b optionval filter : ('a -> bool) -> 'a list -> 'a listval find_all : ('a -> bool) -> 'a list -> 'a listval filteri : (int -> 'a -> bool) -> 'a list -> 'a listval partition : ('a -> bool) -> 'a list -> 'a list * 'a listval assoc : 'a -> ('a * 'b) list -> 'bval assoc_opt : 'a -> ('a * 'b) list -> 'b optionval assq : 'a -> ('a * 'b) list -> 'bval assq_opt : 'a -> ('a * 'b) list -> 'b optionval mem_assoc : 'a -> ('a * 'b) list -> boolval mem_assq : 'a -> ('a * 'b) list -> boolval remove_assoc : 'a -> ('a * 'b) list -> ('a * 'b) listval remove_assq : 'a -> ('a * 'b) list -> ('a * 'b) listval split : ('a * 'b) list -> 'a list * 'b listval combine : 'a list -> 'b list -> ('a * 'b) listval sort : ('a -> 'a -> int) -> 'a list -> 'a listval stable_sort : ('a -> 'a -> int) -> 'a list -> 'a listval fast_sort : ('a -> 'a -> int) -> 'a list -> 'a listval sort_uniq : ('a -> 'a -> int) -> 'a list -> 'a listval merge : ('a -> 'a -> int) -> 'a list -> 'a list -> 'a listval to_seq : 'a list -> 'a Stdlib.Seq.tval of_seq : 'a Stdlib.Seq.t -> 'a list
Equality, testing
val mem_f : 'a CList.eq -> 'a -> 'a list -> boolSame as
List.mem, for some specific equality
Creating lists
val interval : int -> int -> int listinterval i jcreates the list[i; i + 1; ...; j], or[]whenj <= i.
val make : int -> 'a -> 'a listmake n xreturns a list made ofntimesx. RaiseInvalid_argument _ifnis negative.
Lists as arrays
Filtering
val filter : ('a -> bool) -> 'a list -> 'a listLike OCaml
List.filterbut tail-recursive and physically returns the original list if the predicate holds for all elements.
val filter2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> 'a list * 'b listLike
List.filterbut with 2 arguments, raiseInvalid_argument _if the lists are not of same length.
val filteri : (int -> 'a -> bool) -> 'a list -> 'a listLike
List.filterbut with an index starting from0
val filter_with : bool list -> 'a list -> 'a listfilter_with bl lselects elements oflwhose corresponding element inblistrue. RaiseInvalid_argument _if sizes differ.
Applying functorially
val map_left : ('a -> 'b) -> 'a list -> 'b listAs
mapbut ensures the left-to-right order of evaluation.
val map_i : (int -> 'a -> 'b) -> int -> 'a list -> 'b listLike OCaml
List.mapibut tail-recursive. Alternatively, likemapbut with an index
val map2_i : (int -> 'a -> 'b -> 'c) -> int -> 'a list -> 'b list -> 'c listLike
map2but with an index
val map3 : ('a -> 'b -> 'c -> 'd) -> 'a list -> 'b list -> 'c list -> 'd listLike
mapbut for 3 lists.
val map4 : ('a -> 'b -> 'c -> 'd -> 'e) -> 'a list -> 'b list -> 'c list -> 'd list -> 'e listLike
mapbut for 4 lists.
val map_of_array : ('a -> 'b) -> 'a array -> 'b listmap_of_array f abehaves asList.map f (Array.to_list a)
val map_append : ('a -> 'b list) -> 'a list -> 'b listmap_append f [x1; ...; xn]returnsf x1 @ ... @ f xn.
val map_append2 : ('a -> 'b -> 'c list) -> 'a list -> 'b list -> 'c listLike
map_appendbut for two lists; raisesInvalid_argument _if the two lists do not have the same length.
Finding position
val index : 'a CList.eq -> 'a -> 'a list -> intindexreturns the 1st index of an element in a list (counting from 1).
val safe_index : 'a CList.eq -> 'a -> 'a list -> int optionsafe_indexreturns the 1st index of an element in a list (counting from 1) and None otherwise.
val index0 : 'a CList.eq -> 'a -> 'a list -> intindex0behaves asindexexcept that it starts counting at 0.
Folding
val fold_left_until : ('c -> 'a -> 'c CSig.until) -> 'c -> 'a list -> 'cacts like
fold_left f acc swhilefreturnsCont acc'; it stops returningcas soon asfreturnsStop c.
val fold_right_i : (int -> 'a -> 'b -> 'b) -> int -> 'a list -> 'b -> 'bLike
List.fold_rightbut with an index
val fold_left_i : (int -> 'a -> 'b -> 'a) -> int -> 'a -> 'b list -> 'aLike
List.fold_leftbut with an index
val fold_right_and_left : ('b -> 'a -> 'a list -> 'b) -> 'a list -> 'b -> 'bfold_right_and_left f [a1;...;an] hdisf (f (... (f (f hd an [an-1;...;a1]) an-1 [an-2;...;a1]) ...) a2 [a1]) a1 []
val fold_left3 : ('a -> 'b -> 'c -> 'd -> 'a) -> 'a -> 'b list -> 'c list -> 'd list -> 'aLike
List.fold_leftbut for 3 lists; raiseInvalid_argument _if not all lists of the same size
val fold_left2_set : exn -> ('a -> 'b -> 'c -> 'b list -> 'c list -> 'a) -> 'a -> 'b list -> 'c list -> 'aFold sets, i.e. lists up to order; the folding function tells when elements match by returning a value and raising the given exception otherwise; sets should have the same size; raise the given exception if no pairing of the two sets is found;; complexity in O(n^2)
val fold_left_map : ('a -> 'b -> 'a * 'c) -> 'a -> 'b list -> 'a * 'c listfold_left_map f e_0 [a1;...;an]ise_n,[k_1...k_n]where(e_i,k_i)isf e_{i-1} aifor each i<=n
val fold_right_map : ('b -> 'a -> 'c * 'a) -> 'b list -> 'a -> 'c list * 'aSame, folding on the right
val fold_left2_map : ('a -> 'b -> 'c -> 'a * 'd) -> 'a -> 'b list -> 'c list -> 'a * 'd listSame with two lists, folding on the left
val fold_right2_map : ('b -> 'c -> 'a -> 'd * 'a) -> 'b list -> 'c list -> 'a -> 'd list * 'aSame with two lists, folding on the right
Splitting
val except : 'a CList.eq -> 'a -> 'a list -> 'a listexcept eq a lRemove all occurrences ofainl
val remove : 'a CList.eq -> 'a -> 'a list -> 'a listAlias of
except
val remove_first : ('a -> bool) -> 'a list -> 'a listRemove the first element satisfying a predicate, or raise
Not_found
val extract_first : ('a -> bool) -> 'a list -> 'a list * 'aRemove and return the first element satisfying a predicate, or raise
Not_found
val find_map : ('a -> 'b option) -> 'a list -> 'bReturns the first element that is mapped to
Some _. RaiseNot_foundif there is none.
val goto : int -> 'a list -> 'a list * 'a listgoto i lsplitslinto two lists(l1,l2)such that(List.rev l1)++l2=landl1has lengthi. It raisesIndexOutOfRangewheniis negative or greater than the length ofl.
val split_when : ('a -> bool) -> 'a list -> 'a list * 'a listsplit_when p lsplitslinto two lists(l1,a::l2)such thatl1++(a::l2)=l,p a=trueandp b = falsefor every elementbofl1. if there is no sucha, then it returns(l,[])instead.
val sep_last : 'a list -> 'a * 'a listsep_last lreturns(a,l')such thatlisl'@[a]. It raisesFailure _if the list is empty.
val drop_last : 'a list -> 'a listRemove the last element of the list. It raises
Failure _if the list is empty. This is the second part ofsep_last.
val last : 'a list -> 'aReturn the last element of the list. It raises
Failure _if the list is empty. This is the first part ofsep_last.
val lastn : int -> 'a list -> 'a listlastn n lreturns thenlast elements ofl. It raisesFailure _ifnis less than 0 or larger than the length ofl
val chop : int -> 'a list -> 'a list * 'a listchop i lsplitslinto two lists(l1,l2)such thatl1++l2=landl1has lengthi. It raisesFailure _wheniis negative or greater than the length ofl.
val firstn : int -> 'a list -> 'a listfirstn n lReturns thenfirst elements ofl. It raisesFailure _ifnnegative or too large. This is the first part ofchop.
val skipn : int -> 'a list -> 'a listskipn n ldrops thenfirst elements ofl. It raisesFailure _ifnis less than 0 or larger than the length ofl. This is the second part ofchop.
val skipn_at_least : int -> 'a list -> 'a listSame as
skipnbut returnsifnis larger than the length of the list.
val drop_prefix : 'a CList.eq -> 'a list -> 'a list -> 'a listdrop_prefix eq l1 lreturnsl2ifl=l1++l2else returnl.
val insert : 'a CList.eq -> 'a -> 'a list -> 'a listInsert at the (first) position so that if the list is ordered wrt to the total order given as argument, the order is preserved
share_tails l1 l2returns(l1',l2',l)such thatl1isl1'\@landl2isl2'\@landlis maximal amongst all such decompositions
Association lists
val map_assoc : ('a -> 'b) -> ('c * 'a) list -> ('c * 'b) listApplies a function on the codomain of an association list
val assoc_f : 'a CList.eq -> 'a -> ('a * 'b) list -> 'bLike
List.assocbut using the equality given as argument
val remove_assoc_f : 'a CList.eq -> 'a -> ('a * 'b) list -> ('a * 'b) listRemove first matching element; unchanged if no such element
val mem_assoc_f : 'a CList.eq -> 'a -> ('a * 'b) list -> boolLike
List.mem_assocbut using the equality given as argument
val factorize_left : 'a CList.eq -> ('a * 'b) list -> ('a * 'b list) listCreate a list of associations from a list of pairs
Operations on lists of tuples
Operations on lists seen as sets, preserving uniqueness of elements
val add_set : 'a CList.eq -> 'a -> 'a list -> 'a listadd_set x laddsxinlif it is not already there, or returnslotherwise.
val eq_set : 'a CList.eq -> 'a list CList.eqTest equality up to permutation. It respects multiple occurrences and thus works also on multisets.
val subset : 'a list CList.eqTell if a list is a subset of another up to permutation. It expects each element to occur only once.
val merge_set : 'a CList.cmp -> 'a list -> 'a list -> 'a listMerge two sorted lists and preserves the uniqueness property.
val intersect : 'a CList.eq -> 'a list -> 'a list -> 'a listReturn the intersection of two lists, assuming and preserving uniqueness of elements
val union : 'a CList.eq -> 'a list -> 'a list -> 'a listReturn the union of two lists, assuming and preserving uniqueness of elements
val subtract : 'a CList.eq -> 'a list -> 'a list -> 'a listRemove from the first list all elements from the second list.
Uniqueness and duplication
val distinct_f : 'a CList.cmp -> 'a list -> boolLike
distinctbut using the equality given as argument
val duplicates : 'a CList.eq -> 'a list -> 'a listReturn the list of unique elements which appear at least twice. Elements are kept in the order of their first appearance.
val uniquize : 'a list -> 'a listReturn the list of elements without duplicates. This is the list unchanged if there was none.
val sort_uniquize : 'a CList.cmp -> 'a list -> 'a listReturn a sorted version of a list without duplicates according to some comparison function.
val min : 'a CList.cmp -> 'a list -> 'aReturn minimum element according to some comparison function.
- raises Not_found
on an empty list.
Cartesian product
val cartesian : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c listA generic binary cartesian product: for any operator (**),
cartesian (**) [x1;x2] [y1;y2] = [x1**y1; x1**y2; x2**y1; x2**y1], and so on if there are more elements in the lists.
val cartesians : ('a -> 'b -> 'b) -> 'b -> 'a list list -> 'b listcartesians op init lis an n-ary cartesian product: it builds the list of allop a1 .. (op an init) ..fora1, ...,anin the product of the elements of the lists
val combinations : 'a list list -> 'a list listcombinations lreturns the list ofn_1* ... *n_ptuples[a11;...;ap1];...;[a1n_1;...;apn_pd]wheneverlis a list[a11;..;a1n_1];...;[ap1;apn_p]; otherwise said, it iscartesians (::) [] l
val cartesians_filter : ('a -> 'b -> 'b option) -> 'b -> 'a list list -> 'b listLike
cartesians op init lbut keep only the tuples for whichopreturnsSome _on all the elements of the tuple.
module Smart : sig ... endmodule type MonoS = sig ... end