foldr
lib.lists.foldr
Docs pulled from | This Revision | 14 minutes ago
“right fold” a binary function op
between successive elements of
list
with nul
as the starting value, i.e.,
foldr op nul [x_1 x_2 ... x_n] == op x_1 (op x_2 ... (op x_n nul))
.
Inputs
op
-
1. Function argument
nul
-
2. Function argument
list
-
3. Function argument
Type
foldr :: (a -> b -> b) -> b -> [a] -> b
Examples
lib.lists.foldr
usage example
concat = foldr (a: b: a + b) "z"
concat [ "a" "b" "c" ]
=> "abcz"
# different types
strange = foldr (int: str: toString (int + 1) + str) "a"
strange [ 1 2 3 4 ]
=> "2345a"
Noogle detected
Implementation
The following is the current implementation of this function.
foldr = op: nul: list:
let
len = length list;
fold' = n:
if n == len
then nul
else op (elemAt list n) (fold' (n + 1));
in fold' 0;