Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell foldr with function

Tags:

haskell

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.

like image 602
Mo Moallim Avatar asked May 01 '26 04:05

Mo Moallim


1 Answers

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)))))
like image 151
Lynn Avatar answered May 02 '26 21:05

Lynn