Why does this Haskell code work successfully with infinite lists?

吃可爱长大的小学妹 提交于 2019-11-28 17:50:51

Let's do a little trace in our heads of how Haskell will evaluate your expression. Substituting equals for equals on each line, the expression pretty quickly evaluates to True:

myAny even [1..]
foldr step False [1..]
step 1 (foldr step False [2..])
even 1 || (foldr step False [2..])
False  || (foldr step False [2..])
foldr step False [2..]
step 2 (foldr step False [3..])
even 2 || (foldr step False [3..])
True   || (foldr step false [3..])
True

This works because acc is passed as an unevaluated thunk (lazy evaluation), but also because the || function is strict in its first argument.

So this terminates:

True || and (repeat True)

But this does not:

and (repeat True) || True

Look at the definition of || to see why this is the case:

True  || _ =  True
False || x =  x

My understanding of foldr is that it will loop through every item in the list (and perhaps that understanding is incomplete).

foldr (unlike foldl) does not have to loop through every item of the list. It is instructive to look at how foldr is defined.

foldr f z []     = z
foldr f z (x:xs) = f x (foldr f z xs)

When a call to foldr is evaluated, it forces the evaluation of a call to the function f. But note how the recursive call to foldr is embedded into an argument to the function f. That recursive call is not evaluated if f does not evaluate its second argument.

The key point here is that Haskell is a non-strict language. "Non-strict" means that it allows non-strict functions, which in turn means that function parameters may not be fully evaluated before they may be used. This obviously allows for lazy evaluation, which is "a technique of delaying a computation until the result is required".

Start from this Wiki article

I do not know Haskell, but I suspect that in your case, it works because of lazy evaluation. Because it allows you to work with list infinitely long, when you access it, it will compute the result as you need it.

See http://en.wikipedia.org/wiki/Lazy_evaluation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!