A right fold reduces a list by recursively folding its tail before applying the combining function to the current element. Its list type is `(a -> b -> b) -> b -> [a] -> b`: the function receives an element and the accumulated result, while the base value handles the empty list. section 8.2.1
Building an expression from the right
Folding `[1..9]` with an initial value of `"0"` produces `"(1+(2+(3+(4+(5+(6+(7+(8+(9+0)))))))))"`. The parentheses make the right association visible: the last element combines with the base first, then each preceding element wraps that result. section 8.2.1
Listing 8.4 A right fold function on lists
foldRight :: (a -> b -> b) -> b -> [a] -> b foldRight _ z [] = z #1 foldRight f z (x : xs) = f x $ foldRight f z xs #2
Use this implementation to trace the empty-list case and the recursive step that applies `f` after folding the tail. section 8.2.1
Right folds also support broader data-structure abstractions:
- `Foldable` provides `foldr` and derives convenience operations such as `null`, `length`, `elem`, `sum`, `maximum`, and `minimum` from it. section 8.2.1
- Practice implementing those operations with `foldr`, then define `Foldable` for a binary tree and relate left and right folds to its structure. section 8.2.1
