3 ms·
Honestly, I don't think your explanation is any easier to grasp than "All about monads". I think RWH and the Typeclassopedia are easier for the uninitiated to
by jrockway 17y ago
Honestly, I don't think your explanation is any easier to grasp than "All about monads". I think RWH and the Typeclassopedia are easier for the uninitiated to understand. ("You could have invented monads" is also very good. Although I think it's sad that I know the exact titles of blog posts about monads...)
What I like about RWH is that it builds up monads as "why you need this". You see code in its non-monadic form for a while, and then monads are introduced, and suddenly you see the same program being significantly smaller. You understand the before and after, and can begin to reason for yourself what a monad is. And it becomes abundantly clear why they are useful in Haskell -- you just saw your program shrink by 2/3s.
And a nitpick unrelated to your article -- I notice a lot of Haskell articles implement things unnaturally just to use a certain feature. This makes it hard to understand why you would actually want to use that feature. The reader skims the article, says "this is just a fold", and dismisses monads forever. (I remember reading LYAH's section on applicative functors once. All the examples do nothing, they are merely functions that technically type-check. Not a good way to learn; the real paper on applicative functors is much more approachable. But I digress.)
- brehaut 17y agoAgreed. Clear, real examples of abstract ideas are really important, and applies to much more than just monads. RWH's monads stuff (in particular its parsec chapter) is a good example of a concept in action, and its easy to see why you would actually use that model. Some 'first principles' things are difficult to present like this because there are usually better ways to do it. The state monad is like this. Even in haskell there are more expressive (and easier to use) tools to do the same thing. In this case i think we can all accept somewhat ad hoc examples as long as they are disclaimed.
- jmillikin 17y agoI agree, generally; it's a bit hard to write something better than RWH over a lunch break. But sometimes, breaking down the same problem from multiple viewpoints helps readers to understand what's going on. My particular purpose in writing this is to demystify monads specifically, especially for people who are casually interested in learning Haskell but unwilling to read a few hundred pages on it. RWH is great, but it doesn't explain monads until chapter 14 -- that's a lot of material a reader has to understand before they can even parse the examples.
- jrockway 17y agoBut sometimes, breaking down the same problem from multiple viewpoints helps readers to understand what's going on. I think 43% of the confusion about monads is due to the sheer number of "monad tutorials", each explaining two or three functions (return/bind, pure/fmap/join) with different, overly-complex analogies. (A monad is like a burrito, a monad is like a space suit, a monad is like a nuclear waste container...) The tutorials start with some analogy, and then inevitably explain how "the list monad" and "the maybe monad" work. At the end you know that monads are like burritos, how to apply a function to every value in a list, and how to return failure from a function. But you still don't know anything about monads, or more importantly why monads. If I were to write a monad tutorial, it would include these key points: 1) A monad is not a thing, it's a property of other things. Maybe is a thing. A list is a thing. Both are monads. (My bike is a thing. My wall is a thing. Both are blue.) 2) The abstract concept of a monad lets you reuse code. If you want to apply a function to every value of a list, you could do that without monads. If you want functions to be able to fail, and you want the failures to be composable, you could do that without monads. But what you can't do without monads is write a function like liftM2. With monads, you can turn a normal function into one that works on lists. Or that works on IO. Or that works on computations that can fail. You get to use the same function for all three types. That's the point of monads. (Then there will be a code example: 2 + 3 --> 5 liftM2 (+) (Just 2) (Just 3) --> Just 5 liftM2 (+) Nothing (Just 3) --> Nothing liftM2 (+) (Just 2) Nothing --> Nothing liftM2 (+) [1,2] [3,4] --> [4,5,5,6] liftM2 (+) readLn readLn --> 1 <ENT> 2 <ENT> --> 3 Then it all makes sense!)
- jmillikin 17y ago(note: HN mangles <*>, so I'm using <∗> instead Isn't #2 a property of functors in general? I know I usually use <$> and <∗> rather than the liftM family because they don't limit the number of parameters. (+) <$> (Just 2) <∗> (Just 3) (+) <$> [1,2] <∗> [3,4] (+) <$> readLn <∗> readLn
- jrockway 17y agoYes.