Re:A Gentle Intro to Monads … Maybe?
by agscala on Friday January 17, 2014 @11:05AM
(#7076447)
It seems like a good intro, but one of these days I would really like to see a monad explanation that doesn't use Maybe.
[ Parent
| on HN ]
I used Future as well as maybe - http://m50d.github.io/2013/01/16/generic-contexts.html . I think it's important to give at least two or three examples, because the whole point of the idea of a monad is that it's an abstraction that several different datatypes conform to.
Re:A Gentle Intro to Monads … Maybe?
by FreezerburnV on Friday January 17, 2014 @11:16AM
(#7076519)
What kind of example would you like to see other than Maybe?
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)
Running this in GHCi, I get: > minimal 2
[(2,2),(2,20),(3,2),(3,20)]
> minimal 3
[(3,3),(30,3),(3,4),(30,4)]
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.
This conversation is dead, but I've expanded on this at http://www.jerf.org/iri/post/2928 .