3 ms·
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.
by agscala 13y ago
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.
- lmm 13y agoI used Future as well as maybe - http://m50d.github.io/2013/01/16/generic-contexts.html 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.
- FreezerburnV 13y agoWhat kind of example would you like to see other than Maybe?
- jerf 13y agoList 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.
- jerf 13y agoThis conversation is dead, but I've expanded on this at http://www.jerf.org/iri/post/2928 http://www.jerf.org/iri/post/2928 .
- duaneb 13y agoMaybe a LINQ-style monad pattern would be illustrative?