Why does the following Haskell code not terminate:
foldr (||) True $ repeat False -- never terminates
when something like this does:
foldr (||) False $ repeat True -- => True
To me, it’s the second expression that looks to be in more trouble of not terminating. What’s wrong with my view of Haskell’s lazy evaluation?
I think your understanding of lazy is correct, but not for
foldr. Let’s look at its “specification“Then look at your expression
As you see the
True(z) which we are “looking for” to get||to terminate won’t be reached until the whole list is consumed. And that won’t happen as the list is infinite.This should also explain for the actually terminating example.