7 ms·
It perhaps rarely makes sense, but not 100% never. You might at some point in your life have a situation where you want to fold an operator with short-circuitin
by Chinjut 7y ago
It perhaps rarely makes sense, but not 100% never. You might at some point in your life have a situation where you want to fold an operator with short-circuiting behavior left-associatedly over a list (thus, with later elements of the list controlling whether short-circuiting happens skipping evaluation of-and-concerning earlier elements of the list). In this case, foldl is the way to go. foldl' can't execute the desired short-circuiting, and foldr can but only in the other direction, of right-associated bundling with earlier elements of the list controlling the short-circuiting of-and-concerning later elements of the list.
If there's ever a situation where you want to reverse a list and then foldr over it, taking advantage of the short-circuiting foldr allows, well, that reverse and then foldr is essentially the definition of foldl, have at it.
- erikpukinskis 7y agoAnd you may find yourself Living in a shotgun shack And you may find yourself In another part of the world And you may find yourself folding an operator with short-circuiting behavior left-associatedly over a list Let the data go by....
- DoreenMichele 7y agoLetting the data go by... ;)
- idclip 7y agoMakes me want to listen to letting the cables sleep by bush https://youtu.be/d8TrkCObypE https://youtu.be/d8TrkCObypE
- deleted 7y ago[deleted]
- taejo 7y agoProbably you were just adding a reference and didn't miss the first one, but for anyone who did, it's "Once in a Lifetime" by Talking Heads: https://www.youtube.com/watch?v=5IsSpAOD6K8 https://www.youtube.com/watch?v=5IsSpAOD6K8
- bryanrasmussen 7y agoAren't you supposed to ask yourself something before letting anything go by?!?
- bonzini 7y ago"How did I get here" seems appropriate.
- idclip 7y agoIf the list was a list of game states, would foldl makes sense as a map that teats for winning or death conditions?
- lmm 7y agoNo, you want foldr for that because you want the fold to finish as soon as you reach one of those conditions.
- raverbashing 7y agoNice. Do you know of an example where this short circuiting happens in Haskell? Would it be folding a list of Nothing/Just X with an "or" operator
- 08-15 7y ago> want to fold an operator with short-circuiting behavior left-associatedly over a list And then you'd find out that it doesn't work the way you hoped it would. foldl always traverses the whole list, because there is no way for it to see that arbitrarily nested applications of the operator would still return the absorbing first argument. If you need short-circuiting and left-associated traversal, you have to implement an improved fold. Oleg did, and called it iteratee.