Version 9 (modified by 3 years ago) ( diff ) | ,
---|

# foldr

`foldr`

and `foldl`

are *enzymes* which generalize traversing and rebuilding of a list structure by taking a *combinator* and an *initial value* as parameters.

map could be defined in terms of folding functions.

see also Folds

(define foldr (lambda (f z xs) (if (null? xs) z (f (car xs) (foldr f z (cdr xs))))))

in terms of constant-space foldl at the cost of extra reverse

(define (foldr f z xs) (foldl (flip f) z (reverse xs))))

foldr f z [] = z foldr f z (x:xs) = f x (foldr f z xs)

foldr k z = go where go [] = z go (y:ys) = y `k` go ys

foldr(F, Z, [H|T]) -> F(H, foldr(F, Z, T)); foldr(F, Z, []) when is_function(F, 2) -> Z.

**Note:**See TracWiki for help on using the wiki.