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.
Your second example has two related problems.
Recall that
foldr
produces a right-associative nesting, i.e.,foldr f z [a, b, c]
=f a (f b (f c z))
. Thus, the second argument to the function you givefoldr
represents the final value of folding the entire remaining list. In the case of an infinite list, this is only possible if you either generate another lazy infinite data structure or, at some point, ignore the second parameter entirely, as your first example does.Your second example always uses the second item in the input tuple to compute the second item of the result tuple, so when applied to an infinite list you end up with an infinite regress. Specifying
0
as the "starting" value doesn't help, because infoldr
the starting value is placed at the end of the list, which an infinite list clearly doesn't have--it seems you want to pass the value along unchanged, but that requires having a value to begin (or perhaps "end") with!That much only causes the second item in the result to be non-terminating. The first item of the result is well-defined, or at least could be. The problem here is that pattern matching forces evaluation, which prevents any result from being produced until the entire fold is finished. This can be avoided in several ways, but simply using
fst
andsnd
is easy enough:\x yn -> if x == 4 then (x, snd yn) else (x + fst yn, snd yn + 1)
should give you a result consisting of a tuple whose first item is what you expect (but whose second item is undefined, as explained above).