Here an example to illustrate what I mean
Prelude> foldr (-) 0 [0,1]
-1
I thought here it would basically do 0 - 0 = 0; 1 - 0 = 1;
but the actual result is -1
I tried it with other examples
Prelude> foldr (-) 1 [1,3]
-1
Prelude> foldr (-) 1 [1,9]
-7
I probably misunderstood how foldr works so I would be happy about an explanation :)
Try foldr (-) 0 [1, 2, 3, 4, 5]
. You should get 3
. That's because the fold is equivalent to (1 - (2 - (3 - (4 - (5 - 0)))))
-- it's starting from the right. If you try foldl
, you should get -15
. That's because it's starting from the left and is equivalent to (((((0 - 1) - 2) - 3) - 4) - 5)
.
Your shorter example, foldr (-) 0 [0, 1]
, is equivalent to 0 - (1 - 0)
, which reduces to -1
.