from Hacker News

It never makes sense to use foldl on lists in Haskell (2019)

by tirumaraiselvan on 3/19/20, 9:47 AM with 130 comments

  • by chc on 3/20/20, 11:52 PM

    It's worth noting, if you either just read the title or are skimming the post, that "never" is used here specifically in the context of a language like Haskell. If your language uses strict evaluation (like most languages), it probably makes sense to use a left fold on lists.
  • by Chinjut on 3/21/20, 1:16 AM

    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.

  • by ISO-morphism on 3/21/20, 5:27 AM

    I'd like to take the opportunity to thank lexi-lambda for multiple thoughtful contributions to the programming community. In the world of open source and software in general there are names that are recognizable - some feel more like "brands," intentionally cultivated, but I don't feel like I'm really being sold something when I see a post/article/wall of text from lexi-lambda, rather through repeated examples I've come to feel a bit of excitement that someone who has gone much deeper down an interesting technical path than I has taken the time to meaningfully share their knowledge and experience for the benefit of others. Thank you.
  • by candeira on 3/21/20, 3:49 AM

    The linked comment is great. However, I think the HN heading is confusing, particularly for people who may not know about the existence of Haskell and its lazy-by-default semantics.

    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

    What does this random minor github discussion have to do with the submitted title? I'm on mobile, does 'foldl' appear on the page on desktop?
  • by jolmg on 3/20/20, 11:39 PM

    > Now, in this case, this is a silly operation, since foldr (:) [] is just a complicated identity function on lists, but we could imagine a slightly more complicated function, such as one that doubles each element in a list.

    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

    Haskell seems to be used with projects that are heavy on parsing (shellcheck, pandoc, graphql). I think that's partly because there just isn't a parser generator that feels right for general purpose languages, with haskell you seem to have less of a mismatch between the parser itself and handling of the parse tree. Is that a correct impression?
  • by thayne on 3/21/20, 4:01 AM

    What is the connection between the title and the linked PR. Where is the explanation of why it never makes sense?
  • by im3w1l on 3/20/20, 11:47 PM

    So if I got that right, the future best practice will be to use foldMap/foldMap' on all data structures unless you require a particular associativity.
  • by microtherion on 3/20/20, 11:19 PM

    For a second there, I thought "foldl" was the opposite of "hodl" in terms of cryptocurrency speculation.
  • by ionforce on 3/21/20, 4:08 AM

    Poorly written headline.
  • by dependenttypes on 3/21/20, 2:58 AM

    The issue is basically that haskell is a lazy language which ends up leaking memory left and right. Most people that I have talked to about it seem to agree that haskell would be a trillion times better if it was a strict language. At least we now have Idris.
  • by abhayb on 3/21/20, 4:23 AM

    I'm going to take inspiration from @lexi-lambda and respond in mini-composition form. Note that great writers write essays. I, write compositions.

    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.