Accumulator in foldr

haskell

Solution

Consider the evaluation of a simple `foldr` expression based on the (correct) definition you provided:

  foldr (+) 0 [1,2,3,4]
= 1 + foldr (+) 0 [2,3,4]
= 1 + 2 + foldr (+) 0 [3,4]
= 1 + 2 + 3 + foldr (+) 0 [4]
= 1 + 2 + 3 + 4 + foldr (+) 0 []
= 1 + 2 + 3 + 4 + 0
= 10

So you are right: `acc` doesn't really "accumulate" anything. It never takes on a value other than `0`.

Why is it called "acc" if it isn't an accumulator? Similarity to `foldl`? Hysterical raisins? A lie to children? I'm not sure.

Edit: I'll also point out that the GHC implementation of `foldr` uses `z` (presumably for zero) rather than `acc`.

Problem

In the Haskell Wikibook, foldr is implemented as follows: ``` foldr :: (a -> b -> b) -> b -> [a] -> b foldr f acc [] = acc foldr f acc (x:xs) = f x (foldr f acc xs) ``` It is stated that the initial value of the accumulator is set as an argument. But as I understand it, acc is the identity value for the operation (e.g. 0 for sum or 1 for product) and its value does not change during the execution of the function. Why then is it referred to here and in other texts as an accumulator, implying that it changes or accumulates a value step by step? I can see that an accumulator is relevant in a left fold, such as foldl, but is the wikibook explanation incorrect, and only for symmetry, in which case it is wrong?

Original source