Mo Moallim
Mo Moallim

Reputation: 33

Haskell foldr with function

I would like to understand how the below code of Haskell foldr is evaluated.

k x y = x

foldr k 1 [0..5]

The result is 0, but I can't understand why it is zero? I would like to think that x is 1 any the elements in the list are y. Can anyone explain it to me, please? I searched it online but couldn't found anything useful.

Upvotes: 3

Views: 203

Answers (1)

lynn
lynn

Reputation: 10814

The Haskell Wiki has some useful info about how to interpret foldr, including this image:

enter image description here

You can see how your expression expands to:

0 `k` (1 `k` ... (5 `k` 1)))))

Upvotes: 10

Related Questions