ListLabels(3)
π ListLabels(3) β OCaml library π
Okay so bestie, welcome to ListLabels(3) from the OCaml library β it's literally the labeled list module, and it's giving "all your fave list ops but make it read like a group chat." π
β¨
πΈ NAME πΈ
ListLabels β list operations. Periodt.
π Module π
Module ListLabels
π Documentation π
Module ListLabels : sig end
List operations. No cap.
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 whole 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. But like, if you're out here with giant lists, pay attention bestie.
The labeled version of this module can be used as described in the StdLabels module.
type 'a t = 'a list =
| []
| (::) of 'a * 'a list
An alias for the type of lists. Iconic behavior.
val length : 'a list -> intβ returns the length (number of elements) of the given list.val compare_lengths : 'a list -> 'b list -> intβ compares the lengths of two lists.compare_lengths l1 l2is equivalent tocompare (length l1) (length l2), except that the computation stops after reaching the end of the shortest list. Since 4.05val compare_length_with : 'a list -> len:int -> intβ compares the length of a list to an integer.compare_length_with l lenis equivalent tocompare (length l) len, except that the computation stops after at mostleniterations on the list. Since 4.05val is_empty : 'a list -> boolβis_empty listrueif and only iflhas no elements. It is equivalent tocompare_length_with l 0 = 0. Since 5.1val cons : 'a -> 'a list -> 'a listβcons x xsisx :: xs. Since 4.05val singleton : 'a -> 'a listβsingleton xreturns the one-element list[x]. Since 5.4val hd : 'a list -> 'aβ returns the first element of the given list. RaisesFailureif the list is empty.val tl : 'a list -> 'a listβ returns the given list without its first element. RaisesFailureif the list is empty.val nth : 'a list -> int -> 'aβ returns then-th element of the given list. The first element (head of the list) is at position0. RaisesFailureif the list is too short. RaisesInvalid_argumentifnis negative.val nth_opt : 'a list -> int -> 'a optionβ returns then-th element of the given list. The first element (head of the list) is at position0. ReturnsNoneif the list is too short. Since 4.05 RaisesInvalid_argumentifnis negative.val rev : 'a list -> 'a listβ list reversal.val init : len:int -> f:(int -> 'a) -> 'a listβinit ~len ~fis[f 0; f 1; ...; f (len-1)], evaluated left to right. Since 4.06 RaisesInvalid_argumentiflen < 0.val append : 'a list -> 'a list -> 'a listβappend l0 l1appendsl1tol0. Same function as the infix operator@. Since 5.1 this function is tail-recursive.val rev_append : 'a list -> 'a list -> 'a listβrev_append l1 l2reversesl1and concatenates it withl2. This is equivalent to( ListLabels.rev l1) @ l2.val concat : 'a list list -> 'a listβ concatenates 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).val flatten : 'a list list -> 'a listβ same asListLabels.concat. Not tail-recursive (length of the argument + length of the longest sub-list).
π Comparison π
val equal : eq:('a -> 'a -> bool) -> 'a list -> 'a list -> boolβequal eq [a1; ...; an] [b1; ..; bm]holds when the two input lists have the same length, and for each pair of elementsai,biat the same position we haveeq ai bi. Note: theeqfunction may be called even if the lists have different length. If you know your equality function is costly, you may want to checkListLabels.compare_lengthsfirst. Since 4.12val compare : cmp:('a -> 'a -> int) -> 'a list -> 'a list -> intβcompare cmp [a1; ...; an] [b1; ...; bm]performs a lexicographic comparison of the two input lists, using the same'a -> 'a -> intinterface ascompare:a1 :: l1is smaller thana2 :: l2(negative result) ifa1is smaller thana2, or if they are equal (0result) andl1is 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
π₯ Iterators π₯
val iter : f:('a -> unit) -> 'a list -> unitβiter ~f [a1; ...; an]applies functionfin turn to[a1; ...; an]. It is equivalent tof a1; f a2; ...; f an.val iteri : f:(int -> 'a -> unit) -> 'a list -> unitβ same asListLabels.iter, but the function is applied to the index of the element as first argument (counting from0), and the element itself as second argument. Since 4.00val map : f:('a -> 'b) -> 'a list -> 'b listβmap ~f [a1; ...; an]applies functionftoa1, ..., an, and builds the list[f a1; ...; f an]with the results returned byf.val mapi : f:(int -> 'a -> 'b) -> 'a list -> 'b listβ same asListLabels.map, but the function is applied to the index of the element as first argument (counting from0), and the element itself as second argument. Since 4.00val rev_map : f:('a -> 'b) -> 'a list -> 'b listβrev_map ~f lgives the same result asListLabels.rev ( ListLabels.map f l), but is more efficient.val filter_map : f:('a -> 'b option) -> 'a list -> 'b listβfilter_map ~f lappliesfto every element ofl, filters out theNoneelements and returns the list of the arguments of theSomeelements. Since 4.08val filter_mapi : f:(int -> 'a -> 'b option) -> 'a list -> 'b listβ same asListLabels.filter_map, but the function is applied to the index of the element as first argument (counting from0), and the element itself as second argument. Since 5.5val concat_map : f:('a -> 'b list) -> 'a list -> 'b listβconcat_map ~f lgives the same result asListLabels.concat ( ListLabels.map f l). Tail-recursive. Since 4.10val fold_left_map : f:('acc -> 'a -> 'acc * 'b) -> init:'acc -> 'a list -> 'acc * 'b listβfold_left_mapis a combination offold_leftandmapthat threads an accumulator through calls tof. Since 4.11val fold_left : f:('acc -> 'a -> 'acc) -> init:'acc -> 'a list -> 'accβfold_left ~f ~init [b1; ...; bn]isf (... (f (f init b1) b2) ...) bn.val fold_right : f:('a -> 'acc -> 'acc) -> 'a list -> init:'acc -> 'accβfold_right ~f [a1; ...; an] ~initisf a1 (f a2 (... (f an init) ...)). Not tail-recursive.
π¦ Iterators on two lists π¦
val iter2 : f:('a -> 'b -> unit) -> 'a list -> 'b list -> unitβiter2 ~f [a1; ...; an] [b1; ...; bn]calls in turnf a1 b1; ...; f an bn. RaisesInvalid_argumentif the two lists are determined to have different lengths.val map2 : f:('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c listβmap2 ~f [a1; ...; an] [b1; ...; bn]is[f a1 b1; ...; f an bn]. RaisesInvalid_argumentif the two lists are determined to have different lengths.val rev_map2 : f:('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c listβrev_map2 ~f l1 l2gives the same result asListLabels.rev ( ListLabels.map2 f l1 l2), but is more efficient.val fold_left2 : f:('acc -> 'a -> 'b -> 'acc) -> init:'acc -> 'a list -> 'b list -> 'accβfold_left2 ~f ~init [a1; ...; an] [b1; ...; bn]isf (... (f (f init a1 b1) a2 b2) ...) an bn. RaisesInvalid_argumentif the two lists are determined to have different lengths.val fold_right2 : f:('a -> 'b -> 'acc -> 'acc) -> 'a list -> 'b list -> init:'acc -> 'accβfold_right2 ~f [a1; ...; an] [b1; ...; bn] ~initisf a1 b1 (f a2 b2 (... (f an bn init) ...)). RaisesInvalid_argumentif the two lists are determined to have different lengths. Not tail-recursive.
π List scanning π
val for_all : f:('a -> bool) -> 'a list -> boolβfor_all ~f [a1; ...; an]checks if all elements of the list satisfy the predicatef. That is, it returns(f a1) && (f a2) && ... && (f an)for a non-empty list andtrueif the list is empty.val exists : f:('a -> bool) -> 'a list -> boolβexists ~f [a1; ...; an]checks if at least one element of the list satisfies the predicatef. That is, it returns(f a1) || (f a2) || ... || (f an)for a non-empty list andfalseif the list is empty.val for_all2 : f:('a -> 'b -> bool) -> 'a list -> 'b list -> boolβ same asListLabels.for_all, but for a two-argument predicate. RaisesInvalid_argumentif the two lists are determined to have different lengths.val exists2 : f:('a -> 'b -> bool) -> 'a list -> 'b list -> boolβ same asListLabels.exists, but for a two-argument predicate. RaisesInvalid_argumentif the two lists are determined to have different lengths.val mem : 'a -> set:'a list -> boolβmem a ~setistrueif and only ifais equal to an element ofset.val memq : 'a -> set:'a list -> boolβ same asListLabels.mem, but uses physical equality instead of structural equality to compare list elements.
π List searching π
val find : f:('a -> bool) -> 'a list -> 'aβfind ~f lreturns the first element of the listlthat satisfies the predicatef. RaisesNot_foundif there is no value that satisfiesfin the listl.val find_opt : f:('a -> bool) -> 'a list -> 'a optionβfind ~f lreturns the first element of the listlthat satisfies the predicatef. ReturnsNoneif there is no value that satisfiesfin the listl. Since 4.05val find_index : f:('a -> bool) -> 'a list -> int optionβfind_index ~f xsreturnsSome i, whereiis the index of the first element of the listxsthat satisfiesf x, if there is such an element. It returnsNoneif there is no such element. Since 5.1val find_map : f:('a -> 'b option) -> 'a list -> 'b optionβfind_map ~f lappliesfto the elements oflin order, and returns the first result of the formSome v, orNoneif none exist. Since 4.10val find_mapi : f:(int -> 'a -> 'b option) -> 'a list -> 'b optionβ same asfind_map, but the predicate is applied to the index of the element as first argument (counting from0), and the element itself as second argument. Since 5.1val filter : f:('a -> bool) -> 'a list -> 'a listβfilter ~f lreturns all the elements of the listlthat satisfy the predicatef. The order of the elements in the input list is preserved.val find_all : f:('a -> bool) -> 'a list -> 'a listβfind_allis another name forListLabels.filter.val filteri : f:(int -> 'a -> bool) -> 'a list -> 'a list
Okay so it's literally Same as ListLabels.filter, but the predicate is applied to the index of the element as first argument (counting from 0, bestie), and the element itself as second argument. β¨
Since 4.11.
π«Ά List manipulation
val take : int -> 'a list -> 'a list
take n l returns the prefix of l of length n, or a copy of l if n > length l. This is the empty list if n is negative. π
Warning. In version5.3only, this function raisesInvalid_argumentfor negativenvalues.
Since 5.3.
val drop : int -> 'a list -> 'a list
drop n l returns the suffix of l after n elements, or [] if n > length l. This is l if n is negative. π
Warning. In version5.3only, this function raisesInvalid_argumentfor negativenvalues.
Since 5.3.
val take_while : f:('a -> bool) -> 'a list -> 'a list
take_while p l is the longest (possibly empty) prefix of l containing only elements that satisfy p. πΈ
Since 5.3.
val drop_while : f:('a -> bool) -> 'a list -> 'a list
drop_while p l is the longest (possibly empty) suffix of l starting at the first element that does not satisfy p. π¦
Since 5.3.
val partition : f:('a -> bool) -> 'a list -> 'a list * 'a list
partition ~f l returns a pair of lists (l1, l2), where l1 is the list of all the elements of l that satisfy the predicate f, and l2 is the list of all the elements of l that do not satisfy f. The order of the elements in the input list is preserved. π₯
val partition_map : f:('a -> ('b, 'c) Either.t) -> 'a list -> 'b list * 'c list
partition_map f l returns a pair of lists (l1, l2) such that, for each element x of the input list l:
-if f x is Left y1 , then y1 is in l1 , and
-if f x is Right y2 , then y2 is in l2 .
The output elements are included in l1 and l2 in the same relative order as the corresponding input elements in l. β¨
In particular, partition_map (fun x -> if f x then Left x else Right x) l is equivalent to partition f l. π
Since 4.12.
π 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_opt : 'a -> ('a * 'b) list -> 'b option
assoc_opt a l returns the value associated with key a in the list of pairs l. That is, assoc_opt a [ ...; (a,b); ...] = Some b if (a,b) is the leftmost binding of a in list l. Returns None if there is no value associated with a in the list l. π«Ά
Since 4.05.
val assq : 'a -> ('a * 'b) list -> 'b
Same as ListLabels.assoc, but uses physical equality instead of structural equality to compare keys. π
val assq_opt : 'a -> ('a * 'b) list -> 'b option
Same as ListLabels.assoc_opt, but uses physical equality instead of structural equality to compare keys. π
Since 4.05.
val mem_assoc : 'a -> map:('a * 'b) list -> bool
Same as ListLabels.assoc, but simply return true if a binding exists, and false if no bindings exist for the given key. π
val mem_assq : 'a -> map:('a * 'b) list -> bool
Same as ListLabels.mem_assoc, but uses physical equality instead of structural equality to compare keys. π§
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. Not tail-recursive. π
val remove_assq : 'a -> ('a * 'b) list -> ('a * 'b) list
Same as ListLabels.remove_assoc, but uses physical equality instead of structural equality to compare keys. Not tail-recursive. π
π¦ Lists of pairs
val split : ('a * 'b) list -> 'a list * 'b list
Transform a list of pairs into a pair of lists: split [(a1,b1); ...; (an,bn)] is ([a1; ...; an], [b1; ...; bn]). Not tail-recursive. πΈ
val split_map : f:('c -> 'a * 'b) -> 'c list -> 'a list * 'b list
split_map ~f l is equivalent to split (map ~f l) but avoids allocating intermediate lists. β¨
Since 5.5.
Not tail-recursive. π
val combine : 'a list -> 'b list -> ('a * 'b) list
Transform a pair of lists into a list of pairs: combine [a1; ...; an] [b1; ...; bn] is [(a1,b1); ...; (an,bn)]. π
Raises Invalid_argument if the two lists have different lengths. Not tail-recursive. π
π₯ Sorting
val sort : cmp:('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, compare is a suitable comparison function. The resulting list is sorted in increasing order. ListLabels.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 : cmp:('a -> 'a -> int) -> 'a list -> 'a list
Same as ListLabels.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 : cmp:('a -> 'a -> int) -> 'a list -> 'a list
Same as ListLabels.sort or ListLabels.stable_sort, whichever is faster on typical input. β‘
val sort_uniq : cmp:('a -> 'a -> int) -> 'a list -> 'a list
Same as ListLabels.sort, but also remove duplicates: if multiple elements compare equal, keep only the first. π
Before5.4 the element kept was not necessarily the first one. π
Since 4.03.
val merge : cmp:('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 containing 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). π
π Lists and Sequences
val to_seq : 'a list -> 'a Seq.t
Iterate on the list. π§
Since 4.07.
val of_seq : 'a Seq.t -> 'a list
Create a list from a sequence. π¦
Since 4.07.
Big kiss to OCamldoc for this one β version/date stamp: 2026-06-22, page ListLabels(3). Slay. π
π¦ where this page comes from
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms