Re:A Gentle Intro to Monads … Maybe?
by jerf on Friday January 17, 2014 @11:56AM
(#7076797)
List is probably the minimal intro you can do without trapping yourself. It's too easy to implement something that can do the 1-or-0 elements in Maybe, but still isn't a Monad. You should also do a conditional, which I believe this code fails; that is, a monadic computation can examine the intermediate results and make flow-control decisions.
The minimal example is probably something like:
minimal :: Int -> [(Int, Int)]
minimal i = do
x <- [i, i + 1]
y <- [i, 10 * i]
if i `mod` 2 == 0
then return (x, y)
else return (y, x)
The evenness or oddness of the parameter controls which side of the tuple gets which element.
It may still be possible to implement this without implementing a full monadic computation, but if you can pass this test using something like bind or join, you're probably at least on the right track.
The minimal example is probably something like:
Running this in GHCi, I get: The evenness or oddness of the parameter controls which side of the tuple gets which element.It may still be possible to implement this without implementing a full monadic computation, but if you can pass this test using something like bind or join, you're probably at least on the right track.