Stdlib.ListLabels(3)
๐ Stdlib.ListLabels(3) โ the OCaml library manual, bestie edition โจ
okay so this is the OCaml library page for Stdlib.ListLabels(3), and it's honestly a whole mood: it's the labeled-arguments glow-up of the classic list module. no description in the NAME section, which is iconic of it, honestly. anyway, let's get into it ๐
โจ NAME โจ
Stdlib.ListLabels โ no description ๐ซถ
๐ฆ Module ๐ฆ
Module Stdlib.ListLabels
๐ Documentation ๐
Module ListLabels : (module Stdlib__ListLabels)
type 'a t = 'a list =
| []
| (::) of 'a * 'a list
so 'a t is literally just an alias for the type of lists. same list energy, just with labels on the functions. slay.
๐ธ the actual functions ๐ธ
val length : 'a list -> intโ returns the length (number of elements) of the given list. simple, clean, ate.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.05 โจval 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.05 โญval is_empty : 'a list -> boolโis_empty listrueif and only iflhas no elements. it's equivalent tocompare_length_with l 0 = 0. since 5.1 ๐ฆval cons : 'a -> 'a list -> 'a listโcons x xsisx :: xs. since 4.05 ๐val singleton : 'a -> 'a listโsingleton xreturns the one-element list[x]. since 5.4 ๐val hd : 'a list -> 'aโ returns the first element of the given list. RaisesFailureif the list is empty. no cap, empty list means no head to give you ๐ญval tl : 'a list -> 'a listโ returns the given list without its first element. RaisesFailureif the list is empty. same deal, bestie.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. so don't come at it with negative index energy ๐ซถ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. lowkey the safer nth, honestly.val rev : 'a list -> 'a listโ list reversal. that's it. that's the whole vibe. ๐ฅ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. we love a glow-up ๐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). so don't be dramatic with gigantic nested lists ๐ญval flatten : 'a list list -> 'a listโ same asListLabels.concat. Not tail-recursive (length of the argument + length of the longest sub-list). yes it's literally the same energy ๐ซถ
๐ซถ 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.12 โญval 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. periodt.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.00 ๐val 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. main character transformation energy โจ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.00 ๐val 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. efficient queen ๐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.08 ๐ธval 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.5 ๐ฆval 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.10 ๐ฅval 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.11 ๐val 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. so bestie, mind your stack ๐ญ
๐ง 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. matching lengths or it's a no ๐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. everyone has to pass the vibe check ๐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. just one icon is enough โจ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. physical equality said "i know her" ๐
๐ 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.05. the no-drama version, honestly.val 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.1 โญval 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.10 ๐ธval 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.1 ๐val 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. we keep the order, always ๐val find_all : f:('a -> bool) -> 'a list -> 'a listโfind_allis another name forListLabels.filter. same function, different wig.val filteri : f:(int -> 'a -> bool) -> 'a list -> 'a listโ same asListLabels.filter, but the predicate is applied to the index of the element as first argument (counting from0), and the element itself as second argument. since 4.11 โจ
๐ List manipulation ๐
val take : int -> 'a list -> 'a listโtake n lreturns the prefix oflof lengthn, or a copy oflifn > length l. this is the empty list ifnis negative. Warning. In version 5.3 only, this function raisesInvalid_argumentfor negativenvalues. since 5.3. so watch out for that version-specific drama ๐ซถval drop : int -> 'a list -> 'a listโdrop n lreturns the suffix oflafternelements, or[]ifn > length l. this islifnis negative. Warning. In version 5.3 only, this function raisesInvalid_argumentfor negativenvalues. since 5.3. same warning, same energy ๐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 ๐ฆ no cap, it just chucks the front part.
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 ๐
periodt.
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 xisLeft y1, theny1is inl1, and - -if
f xisRight y2, theny2is inl2.
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 ๐
RaisesNot_foundif there is no value associated withain the listl๐ญ
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 ๐
iconic.
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 ๐
Before 5.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
and that's the whole vibe bestie ๐ This beauty comes to you from the OCamldoc crew, version-dated 2026-06-22, and it's the Stdlib.ListLabels(3) manual page โ go forth and slay those lists ๐๐ซถ
๐ฆ 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