The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

376034 (11) [Avatar] Offline
#1
It's hard to see how can foldr even work with an infinite list as it seems the first application of function f is with z and the last item of the list. It will first need to traverse all the way to the last item which is infinite time in an infinite list.

On the other hand foldl's first application of the function f is with z and the first item of the list. It can already start applying the function and we can traverse down the list lazily.
Mark Elston (133) [Avatar] Offline
#2
I agree. And if you look at the HaskellWiki page on folds it seems to say this as well.

However, I ran into a blog post on Why foldr works for infinite lists and foldl doesn’t that goes a long way in explaining it. It would appear that foldr can work on infinite lists if the operation is lazy in its second parameter. Otherwise, it would appear that foldr would have the same stack behavior as foldl.