StdLabels.List(3)
okay so bestie, this is StdLabels.List(3) from the OCaml library β yes, the StdLabels.List manual page, and it's literally all about lists. π
Part 1 energy only, so let's go. π¦
π NAME π
StdLabels.List β no description. That's it, that's the name section, no cap. β¨
π§ Module π§
Module StdLabels.List
β¨ Documentation β¨
Module List : (module ListLabels)
type 'a t = 'a list =
| []
| (::) of 'a * 'a list
An alias for the type of lists. π
val length : 'a list -> int β Return the length (number of elements) of the given list. πΈ
val compare_lengths : 'a list -> 'b list -> int β Compare the lengths of two lists. compare_lengths l1 l2 is equivalent to compare (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 β Compare the length of a list to an integer. compare_length_with l len is equivalent to compare (length l) len, except that the computation stops after at most len iterations on the list. Since 4.05. π
val is_empty : 'a list -> bool β is_empty l is true if and only if l has no elements. It is equivalent to compare_length_with l 0 = 0. Since 5.1. π
val cons : 'a -> 'a list -> 'a list β cons x xs is x :: xs. Since 4.05. β
val singleton : 'a -> 'a list β singleton x returns the one-element list [x]. Since 5.4. π
val hd : 'a list -> 'a β Return the first element of the given list. 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 nth : 'a list -> int -> 'a β Return the n-th element of the given list. The first element (head of the list) is at position 0. Raises Failure if the list is too short. Raises Invalid_argument if n is negative. π
val nth_opt : 'a list -> int -> 'a option β Return the n-th element of the given list. The first element (head of the list) is at position 0. Return None if the list is too short. Since 4.05. Raises Invalid_argument if n is negative. π
val rev : 'a list -> 'a list β List reversal. π
val init : len:int -> f:(int -> 'a) -> 'a list β init ~len ~f is [f 0; f 1; ...; f (len-1)], evaluated left to right. Since 4.06. Raises Invalid_argument if len < 0. β¨
val append : 'a list -> 'a list -> 'a list β append l0 l1 appends l1 to l0. 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 l2 reverses l1 and concatenates it with l2. This is equivalent to ( ListLabels.rev l1) @ 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. Not tail-recursive (length of the argument + length of the longest sub-list). π§΅
val flatten : 'a list list -> 'a list β Same as ListLabels.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 elements ai, bi at the same position we have eq ai bi. Note: the eq function may be called even if the lists have different length. If you know your equality function is costly, you may want to check ListLabels.compare_lengths first. 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 -> int interface as compare:
a1 :: l1is smaller thana2 :: l2(negative result) ifa1is smaller thana2, or if they are equal (0 result) 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 function f in turn to [a1; ...; an]. It is equivalent to f a1; f a2; ...; f an. β¨
val iteri : f:(int -> 'a -> unit) -> 'a list -> unit β Same as ListLabels.iter, but the function is applied to the index of the element as first argument (counting from 0), and the element itself as second argument. Since 4.00. π
val map : f:('a -> 'b) -> 'a list -> 'b list β map ~f [a1; ...; an] applies function f to a1, ..., an, and builds the list [f a1; ...; f an] with the results returned by f. π
val mapi : f:(int -> 'a -> 'b) -> 'a list -> 'b list β Same as ListLabels.map, but the function is applied to the index of the element as first argument (counting from 0), and the element itself as second argument. Since 4.00. π
val rev_map : f:('a -> 'b) -> 'a list -> 'b list β rev_map ~f l gives the same result as ListLabels.rev ( ListLabels.map f l), but is more efficient. β‘
val filter_map : f:('a -> 'b option) -> 'a list -> 'b list β filter_map ~f l applies f to every element of l, filters out the None elements and returns the list of the arguments of the Some elements. Since 4.08. π¦
val filter_mapi : f:(int -> 'a -> 'b option) -> 'a list -> 'b list β Same as ListLabels.filter_map, but the function is applied to the index of the element as first argument (counting from 0), and the element itself as second argument. Since 5.5. π¦
val concat_map : f:('a -> 'b list) -> 'a list -> 'b list β concat_map ~f l gives the same result as ListLabels.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_map is a combination of fold_left and map that threads an accumulator through calls to f. Since 4.11. π§
val fold_left : f:('acc -> 'a -> 'acc) -> init:'acc -> 'a list -> 'acc β fold_left ~f ~init [b1; ...; bn] is f (... (f (f init b1) b2) ...) bn. π
val fold_right : f:('a -> 'acc -> 'acc) -> 'a list -> init:'acc -> 'acc β fold_right ~f [a1; ...; an] ~init is f 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 turn f a1 b1; ...; f an bn. Raises Invalid_argument if 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]. Raises Invalid_argument if 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 l2 gives the same result as ListLabels.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] is f (... (f (f init a1 b1) a2 b2) ...) an bn. Raises Invalid_argument if 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] ~init is f a1 b1 (f a2 b2 (... (f an bn init) ...)). Raises Invalid_argument if 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 predicate f. That is, it returns (f a1) && (f a2) && ... && (f an) for a non-empty list and true if 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 predicate f. That is, it returns (f a1) || (f a2) || ... || (f an) for a non-empty list and false if the list is empty. β
val for_all2 : f:('a -> 'b -> bool) -> 'a list -> 'b list -> bool β Same as ListLabels.for_all, but for a two-argument predicate. Raises Invalid_argument if the two lists are determined to have different lengths. β
val exists2 : f:('a -> 'b -> bool) -> 'a list -> 'b list -> bool β Same as ListLabels.exists, but for a two-argument predicate. Raises Invalid_argument if the two lists are determined to have different lengths. β
val mem : 'a -> set:'a list -> bool β mem a ~set is true if and only if a is equal to an element of set. π
val memq : 'a -> set:'a list -> bool β Same as ListLabels.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 l returns the first element of the list l that satisfies the predicate f. Raises Not_found if there is no value that satisfies f in the list l. π
val find_opt : f:('a -> bool) -> 'a list -> 'a option β find_opt ~f l returns the first element of the list l that satisfies the predicate f. Returns None if there is no value that satisfies f in the list l. Since 4.05. π«Ά
val find_index : f:('a -> bool) -> 'a list -> int option β find_index ~f xs returns Some i, where i is the index of the first element of the list xs that satisfies f x, if there is such an element. It returns None if there is no such element. Since 5.1. π
val find_map : f:('a -> 'b option) -> 'a list -> 'b option β find_map ~f l applies f to the elements of l in order, and returns the first result of the form Some v, or None if none exist. Since 4.10. π
val find_mapi : f:(int -> 'a -> 'b option) -> 'a list -> 'b option β Same as find_map, but the predicate is applied to the index of the element as first argument (counting from 0), and the element itself as second argument. Since 5.1. π
val filter : f:('a -> bool) -> 'a list -> 'a list β filter ~f l returns all the elements of the list l that satisfy the predicate f. The order of the elements in the input list is preserved. π
val find_all : f:('a -> bool) -> 'a list -> 'a list β find_all is another name for ListLabels.filter. π
val filteri : f:(int -> 'a -> bool) -> 'a list -> 'a list β Same as ListLabels.filter, but the predicate is applied to the index of the element as first argument (counting from 0), 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 version 5.3 only, 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 version 5.3 only, this function raisesInvalid_argumentfor negativenvalues.
Since 5.3. βοΈ
val take_while : f:('a -> bool) -> 'a list -> 'a listβ okay sotake_while p lis the longest (possibly empty) prefix oflcontaining only elements that satisfyp .Since 5.3 β¨val drop_while : f:('a -> bool) -> 'a list -> 'a listβ anddrop_while p lis the longest (possibly empty) suffix oflstarting at the first element that does not satisfyp .Since 5.3 π¦val partition : f:('a -> bool) -> 'a list -> 'a list * 'a listβpartition ~f lreturns a pair of lists(l1, l2) ,wherel1is the list of all the elements oflthat satisfy the predicatef ,andl2is the list of all the elements oflthat do not satisfyf .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 lreturns a pair of lists(l1, l2)such that, for each elementxof the input listl :
-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 lreturns the value associated with keyain the list of pairsl .That is,assoc a [ ...; (a,b); ...] = bif(a,b)is the leftmost binding ofain listl .
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 lreturns the value associated with keyain the list of pairsl .That is,assoc_opt a [ ...; (a,b); ...] = Some bif(a,b)is the leftmost binding ofain listl .ReturnsNoneif there is no value associated withain the listl .Since 4.05 πΈval assq : 'a -> ('a * 'b) list -> 'bβ Same asListLabels.assoc ,but uses physical equality instead of structural equality to compare keys. π₯val assq_opt : 'a -> ('a * 'b) list -> 'b optionβ Same asListLabels.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 asListLabels.assoc ,but simply returntrueif a binding exists, andfalseif no bindings exist for the given key. πval mem_assq : 'a -> map:('a * 'b) list -> boolβ Same asListLabels.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 lreturns the list of pairslwithout the first pair with keya ,if any. Not tail-recursive. πval remove_assq : 'a -> ('a * 'b) list -> ('a * 'b) listβ Same asListLabels.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 lis equivalent tosplit (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 return0if its arguments compare as equal, a positive integer if the first is greater, and a negative integer if the first is smaller (seeArray.sortfor a complete specification). For example,compareis a suitable comparison function. The resulting list is sorted in increasing order.ListLabels.sortis 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 asListLabels.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 asListLabels.sortorListLabels.stable_sort ,whichever is faster on typical input. π«Άval sort_uniq : cmp:('a -> 'a -> int) -> 'a list -> 'a listβ Same asListLabels.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 thatl1andl2are sorted according to the comparison functioncmp ,merge ~cmp l1 l2will return a sorted list containing all the elements ofl1andl2 .If several elements compare equal, the elements ofl1will be before the elements ofl2 .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 β
This little page was served up by OCamldoc on 2026-06-22, for StdLabels.List(3) β mwah, bestie πβ¨
π¦ 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