Hacker News new | past | comments | ask | show | jobs | submit login
Adventures in Looping (drewolson.org)
17 points by lbarrow on Oct 1, 2021 | hide | past | favorite | 7 comments



The article is kind of overcomplicating things in favor of really trying preserve `forever`.

Here's an easy one with `whileM` (the one parameter version that is effectively a do-while loop, where the while condition is the expression's return value, there is a two parameter version that takes an independent boolean parameter).

  main :: IO ()
  main = do
    wsUrl <- fetchConnectionUrl
    conn <- connectWebSocket wsUrl

    whileM $ do
      message <- readMessage conn

      case message of
        MessageA val -> putStrLn "Message A" $> True
        MessageB val -> putStrLn "Message B" $> True
        Disconnect -> pure False
EDIT: ah I read a little too quickly. The author wants to have arbitrary breaks in the while loop. In that case you can use https://hackage.haskell.org/package/loop-while-1.0.0/docs/Co... which is morally the same thing without the `mzero` jargon (which really is just a generalized `break` here). The other alternative is to just stick with whileM and just normal structured programming if-else blocks that eventually end up returning true or false.


As much a I enjoy writing FP code in some specific situations, the problem described in this article is exactly why I usually do not reach for it when I need to solve quickly practical, production-oriented problems.

In an imperative setup, this problem simply does not exist.

In an FP setup you need to do brain contortions to get a solution to a trivial problem going.


This is the sort of technique you learn once and use a couple times, and then it’s just sort of automatic when you find yourself in this situation. Like learning a new language, the hard part is when the way you think about programming hasn’t fully made the paradigm shift.

FP’s power lies providing tools that allow you to decouple processes like “loop forever” or “transform each item of a list” from your application’s own business logic.


Man I love Haskell, but this shows how... I'd say immature the language is when it comes to creating software for a specific use.

Business logic is already separated into other parts of the code, and now I have one concept I need to implement: "loop until exit". I would expect a mature programming language to assist me in mapping this to code as directly as possible, but this is not the case here.

The single concept has to be implemented by combining multiple new concepts: monad combination, lifting, mzero as a terminator... which would be fine if any of these concepts assisted in separating logic, but they do not. They only serve to build the concept of "loop until exit".


The author is for some reason not using the most direct loop until exit construct. See https://news.ycombinator.com/item?id=28703896


This kind of articles which explain something which looks simple, but involve a few concepts beginners don't get to use very often; is something Haskell community needs more of. Article does a great job touching different topics to sufficient depth, keep good pace, and isn't introducing something too challenging. A great tea-time reading for a Haskell/FP beginner!


so.. the whole point of the article is "I miss the break keyword"?




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: