A non-empty list. Stores head : α and tail : List α, ensuring at least one element.
Unlike { l : List α // l ≠ [] }, this representation provides $O(1)$ head
access without proof elimination. The canonical form is:
$$\text{NonEmpty}(\alpha) \cong \alpha \times \text{List}(\alpha)$$
- head : α
The first element, accessible in $O(1)$.
- tail : List α
The remaining elements (possibly empty).
Instances For
Equations
Equations
- Data.List.instOrdNonEmpty = { compare := Data.List.instOrdNonEmpty.ord }
Equations
- Data.List.instReprNonEmpty = { reprPrec := Data.List.instReprNonEmpty.repr }
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
Construct a singleton non-empty list. $$\text{singleton}(x) = [x]$$
Instances For
Equations
- Data.List.NonEmpty.last.go x [] = x
- Data.List.NonEmpty.last.go x (y :: ys) = Data.List.NonEmpty.last.go y ys
Instances For
Append a non-empty list to another. Result is non-empty. $$[x_1, \ldots, x_m] \mathbin{+\!\!+} [y_1, \ldots, y_n] = [x_1, \ldots, x_m, y_1, \ldots, y_n]$$
Instances For
Equations
Map a function over every element. $$\text{map}(f, [x_1, \ldots, x_n]) = [f(x_1), \ldots, f(x_n)]$$
Instances For
Right fold over the non-empty list. $$\text{foldr}(f, z, [x_1, \ldots, x_n]) = f(x_1, f(x_2, \ldots f(x_n, z)))$$
Equations
- Data.List.NonEmpty.foldr f init ne = f ne.head (List.foldr f init ne.tail)
Instances For
Right fold without an initial value (uses the last element). $$\text{foldr1}(f, [x_1, \ldots, x_n]) = f(x_1, f(x_2, \ldots f(x_{n-1}, x_n)))$$
Equations
- Data.List.NonEmpty.foldr1 f ne = Data.List.NonEmpty.foldr1.go f ne.head ne.tail
Instances For
Equations
- Data.List.NonEmpty.foldr1.go f x [] = x
- Data.List.NonEmpty.foldr1.go f x (y :: ys) = f x (Data.List.NonEmpty.foldr1.go f y ys)
Instances For
Left fold without an initial value. $$\text{foldl1}(f, [x_1, \ldots, x_n]) = f(\ldots f(f(x_1, x_2), x_3) \ldots, x_n)$$
Equations
- Data.List.NonEmpty.foldl1 f ne = List.foldl f ne.head ne.tail
Instances For
Equations
- Data.List.NonEmpty.instFunctor = { map := fun {α β : Type ?u.10} => Data.List.NonEmpty.map }
Equations
- Data.List.NonEmpty.instPure = { pure := fun {α : Type ?u.6} (x : α) => Data.List.NonEmpty.singleton x }
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.
Equations
- Data.List.NonEmpty.instToString = { toString := fun (ne : Data.List.NonEmpty α) => toString ne.toList }