News for hackers. Stuff that matters.
Re:Why study functional programming? (2012) by solomonb on Wednesday August 03, 2022 @02:12PM (#32335081)
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 :(

Re:Why study functional programming? (2012) by unsafecast on Thursday August 04, 2022 @12:15PM (#32344829)
> 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.