Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Just give me a cookbook that shows how to, .e.g., print out a haiku while numbering the lines.

We can work some toy examples and then trot out the abstruse pet-a-doggie or whatever.

Resist the urge to avoid practicality.



> print out a haiku while numbering the lines.

Little need for monads to do this:

    main = readFile "haiku.txt" >>= putStrLn . number
      where
        number = lines . zipWith addNumber [0..] . unlines
        addNumber n l = show n ++ " " ++ l
(Untested, but should work.)


> Little need for monads to do this:

What do you think >>= is? It is the piece unique to Monads.

So your “little need for monads” example shows the opposite.


If you’re willing to take input on stdin, you can rewrite it without >>=:

    main = interact number
      where
        number = lines . zipWith addNumber [0..] . unlines
        addNumber n l = show n ++ " " ++ l
My point is that the logic itself does not require the idea of monadic sequencing — it’s relevant only for the minimal amount of surrounding IO.



Sorry, nope. I didn’t bother proving correctness for such a small toy example. (Besides, I wouldn’t know how.)


Not concerned with whether the example as such is a poor use-case.

Just seeing the mechanics is the point.




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

Search: