:: (a -> a -> a) -> t a -> a -package:copilot-language

A variant of foldr that has no base case, and thus may only be applied to non-empty structures. This function is non-total and will raise a runtime exception if the structure happens to be empty.

Examples

Basic usage:
>>> foldr1 (+) [1..4]
10
>>> foldr1 (+) []
Exception: Prelude.foldr1: empty list
>>> foldr1 (+) Nothing
*** Exception: foldr1: empty structure
>>> foldr1 (-) [1..4]
-2
>>> foldr1 (&&) [True, False, True, True]
False
>>> foldr1 (||) [False, False, True, True]
True
>>> foldr1 (+) [1..]
* Hangs forever *
A variant of foldl that has no base case, and thus may only be applied to non-empty structures. This function is non-total and will raise a runtime exception if the structure happens to be empty.
foldl1 f = foldl1 f . toList

Examples

Basic usage:
>>> foldl1 (+) [1..4]
10
>>> foldl1 (+) []
*** Exception: Prelude.foldl1: empty list
>>> foldl1 (+) Nothing
*** Exception: foldl1: empty structure
>>> foldl1 (-) [1..4]
-8
>>> foldl1 (&&) [True, False, True, True]
False
>>> foldl1 (||) [False, False, True, True]
True
>>> foldl1 (+) [1..]
* Hangs forever *
A variant of foldl that has no base case, and thus may only be applied to non-empty structures.
foldl1 f = foldl1 f . toList
A variant of foldr that has no base case, and thus may only be applied to non-empty structures.
foldr1 f = foldr1 f . toList
O(n) Left fold on non-empty vectors
O(n) Left fold on non-empty vectors with strict accumulator
O(n) Right fold on non-empty vectors
O(n) Right fold on non-empty vectors with strict accumulator
Left fold over vector
Deprecated: Use foldl f mempty instead.
Deprecated: Use foldr f mempty instead.