by tirumaraiselvan on 3/19/20, 9:47 AM with 130 comments
by chc on 3/20/20, 11:52 PM
by Chinjut on 3/21/20, 1:16 AM
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.
by ISO-morphism on 3/21/20, 5:27 AM
by candeira on 3/21/20, 3:49 AM
The linked comment recommends using strict/eager fold-left instead (`foldl'` in Haskell) of using lazy fold-left (`foldl`, with no trailing apostrophe). It's not saying that you shouldn't use fold-left in any language.
Reading the HN title alone, in its front-page context, it looks like it's saying that it's the fold-left generic operation that's wrong for lists, and not just `foldl`, Haskell's default lazy implementation of fold-left. Paradoxically, following the guidelines and re-using a boiled-down version of the linked comment's first line results in the line being misleading and/or link-bait (I guessed what it was about, but I still had to go and check).
For some time I've thought that the HN policy of disallowing editing of post titles, while correct, can be taken too far, and that many titles would be better with some annotation.
In this case, "It never makes sense to use [lazy] foldl on lists [in Haskell, use foldl' instead]" would be a better title. A bit awkward and maybe inelegant, but it says what the article means. Out of its original context, the HN title doesn't say what the posted article means.
Maybe "Lazy foldl versus strict foldl' in Haskell" would have been an even better title, if the guidelines allow for using one's discretion on when to disregard them.
Other common case is that of headlines in the first person: "My X ..." would often be improved by editing it to "[Name's] X ..." or "[Name of whatever X is] ..." while leaving it otherwise unchanged.
--
I don't know if @sama will get summoned to this comment like he would if this were Twitter, but it doesn't hurt to try.
by hawkice on 3/20/20, 11:29 PM
by jolmg on 3/20/20, 11:39 PM
This whole post is great, but this part would be less awkward and easier to understand with a simpler example of more practical use like:
and = foldr (&&) True
or all f = foldr (&&) True . map f
x && y doesn't evaluate y if x is False.by MichaelMoser123 on 3/21/20, 6:43 AM
by thayne on 3/21/20, 4:01 AM
by im3w1l on 3/20/20, 11:47 PM
by microtherion on 3/20/20, 11:19 PM
by ionforce on 3/21/20, 4:08 AM
by dependenttypes on 3/21/20, 2:58 AM
by abhayb on 3/21/20, 4:23 AM
It's just like Haskell for the crucial difference between two functions to be denoted by prime. For half of the essay I thought that this was a deep philosophical treatise about the degenerate case equality of lazy and strict languages. But, in reality, fold and fold-prime (can you inline code in HN?) are totally different functions.
And like with all pairs of things in Haskell once you learn the fundamental difference between them, the fact of that difference becomes obvious. So obvious that you don't know how anyone could possibly confuse the two. And so you call the method everyone should use foldl-prime even though a casual programmer doesn't even know that "single-quotes" are an acceptable value name. When you could have just called it foldl and moved the old one to the deprecated module.