FR
PréludeChargé par défaut — aucun import nécessaire.

List operations

Synopsis
map : (a -> b) -> List a -> List b

Apply a function to every element of a list, keeping the order. map f [1, 2, 3] = [f 1, f 2, f 3]

foldr : (a -> b -> b) -> b -> List a -> b

Right fold: replace :: with f and Nil with z. foldr f z [a, b, c] = f a (f b (f c z))

foldrLazy : (a -> Lazy b -> b) -> Lazy b -> List a -> b

Right fold that can stop: f receives the fold of the rest suspended (Lazy b) and computes it only if it reads it; the seed z is read only if the fold reaches the end. foldrLazy (\x rest -> p x || rest) False xs stops at the first p x.

foldl : (b -> a -> b) -> b -> List a -> b

Left fold: accumulate from the left. foldl f z [a, b, c] = f (f (f z a) b) c Prefer foldl for sums/products (tail-recursive).

reverse : List a -> List a

Reverse a list.

length : List a -> Nat

Number of elements in a list, as a Nat.

isEmpty : List a -> Bool

Is the list empty?

head : List a -> Maybe a

Safely get the first element (Nothing if empty).

tail : List a -> Maybe (List a)

Safely get all but the first element (Nothing if empty).

filter : (a -> Bool) -> List a -> List a

Keep only elements satisfying a predicate. filter even [1, 2, 3, 4] = [2, 4]

splitAt : Nat -> List a -> (List a, List a)

Split a list after its first n elements: the prefix of length n (or the whole list if shorter) paired with the remainder, in a single pass. splitAt 2 [1, 2, 3, 4] = ([1, 2], [3, 4])

length : String -> Nat

Number of characters in a string, as a Nat. A bare length works on both: length s on a String, length xs on a list.