Unsure of how to get the right evaluation order
Posted
by
Matt Fenwick
on Stack Overflow
See other posts from Stack Overflow
or by Matt Fenwick
Published on 2012-10-24T16:47:20Z
Indexed on
2012/10/24
17:00 UTC
Read the original article
Hit count: 335
I'm not sure what the difference between these two pieces of code is (with respect to x
), but the first one completes:
$ foldr (\x y -> if x == 4 then x else x + y) 0 [1,2 .. ]
10
and the second one doesn't (at least in GHCi):
$ foldr (\x (y, n) -> if x == 4 then (x, n) else (x + y, n + 1)) (0, 0) [1,2 .. ]
.......
What am I doing wrong that prevents the second example from completing when it hits x == 4
, as in the first one?
I've tried adding bang-patterns to both the x
and to the x == 4
(inside a let
) but neither seems to make a difference.
© Stack Overflow or respective owner