Page
Library
Module
Module type
Parameter
Class
Class type
Source
NelSourceDescribes a non-empty list (usually expressed as nel for Non Empty List) (i.e. one containing at least one element). Most of the documentation was mostly done by OCaml Standard Library.
The type isn’t very original and overloads :: (cons) to represent a pair consisting of an element and a (regular) list. As the sum does not contain Nil, it is impossible to construct a non-empty list.
A set of functions for constructing non-empty lists.
init len f is [f 0; f 1; ...; f (len-1)], evaluated left to right. Return None if the given len is < 1.
Same of init but raises Invalid_argument "Nel.init_exn" if the given len is < 1.
from_list tl attempts to convert a list into a non-empty list. Return None if the given list tl is empty.
from_list_exn tl attempts to convert a list into a non-empty list (like from_list but raises Invalid_argument "Nel.from_list_exn" if the given list tl is empty).
length nel return the length (number of elements) of the given nel. Since it is a non-empty list, length nel always returns a number >= 1.
is_singleton nel return true if the non-empty list holds only one element. false otherwise.
hd nel return the first element of the given list nel. Since the list cannot be empty, the function is total.
tl nel return the given list nel without the first element. Since the list can be a singleton, the tail is returned as a list (and not a non-empty list).
last nel retun the latest element of the given nel. Since the list cannot be empty, the function is total.
Return the n-th element of the given list. The first element (head of the list) is at position 0. If the result does not exits, it return None.
Same of nth but raises Invalid_argument "Nel.nth_exn" if the given n is < 1 and Failure if the index does not exists.
rev_append nel1 nel2 reverses nel1 and concatenates it with nel2. This is equivalent to append (rev nel1) nel2.
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).
Same as 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.
map f nel produce a new non-empty list applying f on every element of nel.
See 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.
See rev_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.
bind nel f is concat_map f nel. See concat_map.
See concat_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.
fold_left f init [b1; ...; bn] is f (... (f (f init b1) b2) ...) bn.
fold_right f [a1; ...; an] init is f a1 (f a2 (... (f an init) ...)). Not tail-recursive.
reduce f nel is the semigroup reduction over a non-empty list.
for_all pred nel return true if all element of the given nel satisfy pred. false otherwise.
for_some pred nel return true if at least one element of the given nel satisfy pred. false otherwise.
find pred nel returns the first element of the given nel that satisfy the given predicate pred.
See find but the function is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.
find_index pred nel returns Some i, where i is the index of the first element of the non-empty list nel that satisfies pred x, if there is such an element. It returns None if there is no such element.
find_map pred nel applies pred to the elements of nel in order, and returns the first result of the form Some v, or None if none exist.
See find_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.
Comparison between non-empty lists. Use the same lexicographic heuristics than Stdlib.List.compare.
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.
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.