-
Notifications
You must be signed in to change notification settings - Fork 56
Description
https://learnyouahaskell.github.io/higher-order-functions.html#folds
One big difference is that right folds work on infinite lists, whereas left ones don't!
This is very confusing. It made me doubt whether I understand folds at all. It's like saying that counting from one to infinity won't work, but counting from infinity to one works.
I checked the documentation:
https://hackage.haskell.org/package/base-4.21.0.0/docs/Prelude.html#v:foldr
Note that since the head of the resulting expression is produced by an application of the operator to the first element of the list, given an operator lazy in its right argument, foldr can produce a terminating expression from an unbounded list.
There are some examples at
https://hackage.haskell.org/package/base-4.21.0.0/docs/Prelude.html#v:foldr
I'm not sure if a simple example could be added. I feel it's not necessary. It sounds like a quirk that is rarely needed. The sixths chapter is very long already.
In any case, I believe it's wrong to claim that foldr works on infinite sequences without a detailed explanation. The current "explanation" is not helpful at all:
To put it plainly, if you take an infinite list at some point and you fold it up from the right, you'll eventually reach the beginning of the list.