foldr
foldr is a higher-order function used to reduce a list from the right side using a binary function and a base value. In Haskell its type is foldr :: (a -> b -> b) -> b -> [a] -> b. It takes a function f, a base value z, and a list xs, and computes foldr f z xs by applying f to each element and the result of folding the tail: foldr f z [] = z; foldr f z (x:xs) = f x (foldr f z xs).
Conceptually, foldr performs a right-associative fold. For a list [x1, x2, ..., xn], foldr f z [x1, x2,
Common uses include defining alternative list operations. For example, sum xs can be defined as foldr (+)
Laziness is a defining trait of foldr in a non-strict language like Haskell. It can work with