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!