Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm sorry, I know you put a lot of effort into this but I find it rather convoluted.

I honestly think the best way to explain the Monad typeclass is by just showing the code. It really isn't that complex:

    instance Monad (Either e) where
      return = Right
      Left err >>= _ = Left err
      Right a >>= f = f a
And then show how you can use `>>=` to chain operations on `Either` values. Then you show a second instance, such as:

    instance Monad (State s) where
      return a = State $ \s -> (s, a)
      ma >>= f = State $ \s -> let (s', a) = runState ma s in runState (f a) s'
Then explain how you can use `>>=` here to do something seemingly completely different.

Damnit, now I've accidentally written a monad tutorial :(



> I'm sorry, I know you put a lot of effort into this but I find it rather convoluted.

Don't worry about it! I'm very happy to receive criticism.

I tried to do it in this way because the task was to explain it to a 5-year old. I haven't (yet?) met a person that young who could understand an abstract language like Haskell. It's just not happening.

My explanation is probably indeed way too convoluted, but I still believe monads can be intuitively explained to a 5-year old somehow.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: