foldr' is a variant of
foldr that begins list reduction
from the last element and evaluates the accumulator strictly as it
unwinds the stack back to the beginning of the list. The input list
must be finite, otherwise
foldr' runs out of space
(
diverges).
Note that if the function that combines the accumulated value with
each element is strict in the accumulator, other than a possible
improvement in the constant factor, you get the same <math>
space cost as with just
foldr.
If you want a strict right fold in constant space, you need a
structure that supports faster than <math> access to the
right-most element, such as
Seq from the
containers
package.
Use of this function is a hint that the
[] structure may be a
poor fit for the task at hand. If the order in which the elements are
combined is not important, use
foldl' instead.
>>> foldr' (+) [1..4] -- Use foldl' instead!
10
>>> foldr' (&&) [True, False, True, True] -- Use foldr instead!
False
>>> foldr' (||) [False, False, True, True] -- Use foldr instead!
True