4 ms·
> print out a haiku while numbering the lines. Little need for monads to do this: main = readFile "haiku.txt" >>= putStrLn . number where nu
by bradrn 5y ago
> 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.)
- smnrchrds 5y ago> Untested But proven correct? https://www-cs-faculty.stanford.edu/~knuth/faq.html https://www-cs-faculty.stanford.edu/~knuth/faq.html
- bradrn 5y agoSorry, nope. I didn’t bother proving correctness for such a small toy example. (Besides, I wouldn’t know how.)
- smitty1e 5y agoNot concerned with whether the example as such is a poor use-case. Just seeing the mechanics is the point.
- dragonwriter 5y ago> 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.
- bradrn 5y agoIf 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.