Does anybody know the steps of haskell ‘foldr’ use of function?
GHCI Command Window:
foldr (\x y -> 2*x + y) 4 [5,6,7]
The result after evaluation:
40
Steps on this,
Prelude> foldr (\x y -> 2*x + y) 4 [5,6,7]
6 * 2 + (7 * 2 + 4)
12 + 18 = 30
5 * 2 + 30 = 40 v
One definition of foldr is:
The wikibook on Haskell has a nice graph on foldr (and on other folds, too):
I.e.
a : b : c : [](which is just[a, b, c]) becomesf a (f b (f c acc))(again, taken from wikibook).So your example is evaluated as
let f = (\x y -> 2*x + y) in f 5 (f 6 (f 7 4))(let-binding only for brevity).