Friday, November 9, 2012

Understanding Monads

Monads can be a stumbling block, even for Haskell programmers. While I have gotten little feedback that people need help in understanding, I thought it unlikely they didn't. I thought I would suppose that a lot of people didn't get it and post some resources to help out.

The easiest way to learn is probably to pick up Haskell. It's a weird language, but if you can read LISP, you'll recognize is as syntactically similar, but without everything being in polish prefix notation. Learn You a Haskell helped me get a good start earlier this year.

Learning a new language to grasp monads isn't quite necessary. The difficulty is that very little has been written in this direction in C++. There's FC++ which has been around for at least over a decade. Also, FACT!. And finally, my own library (from which most of these articles come). Unfortunately, there does not seem to have been much interest in functional programming in general in C++. fpcomplete has several articles definitely worth mentioning, such as the one on the continuation monad and a three-part video on the functor pattern.

The easiest way to learn monads is exposure, practice, and experience, which builds an intuition about how and why they work. This is difficult in a language where so few examples exist. In other languages, we have the wonderfully written introduction in Javascript (I don't know Javascript, but I found it very easy to understand.) and You Could Have Invented Monads.

It's also a really good idea to study the foundations. Category theory (wiki) is not about Haskell or monads in particular; it's a highly programming-relevant field of mathematics. Monads (wiki) are mathematical concept within category theory. (See also Category Theory for Computer Scientists (pdf))

I do not expect one to read through each link since that might take weeks! But different people learn different ways, so one source may be more helpful than another. Good luck!

1 comment:

  1. Be careful that Monads in Haskell are modeled after the "Strong Monad" categorical concept thus they have not exactly the same properties.

    Anyway you can read also the paper of Eugenio Moggi : Notions of computations and monads.

    An other thing to do when you pick up Haskell is to not use the do notation but instead force you to use >> and >>= operators with lambas. It helps to understand that Monad is just a framework to execute sequentially function and the result of evaluation might be passed from one function to another. And finally the result is injected (return) into a safe space for retrieve it.

    I think that FP is coming more and more back to C++. (STL is only FP and GP for me)
    The introduction of std::function is a good thing but still C++ is an object first-class citizen language only and not function as in Haskell or Python (well functions are objects), so most programmers are not sensible to this way of thinking.

    Most C++ dev are not aware of all kinds of polymorphism that do exists in the language or maybe do not see the connection/compromise between them.

    my 2 cents :)

    Anyway keep going on I like to read what you do:)


Please feel free to post any comments, concerns, questions, thoughts and ideas. I am perhaps more interested in what you have to say than you are in what I do.