Richard Crowley’s blog

Monads without pretension

The Strange Loop conference this week took a slight detour from its usual celebration of programming languages no one uses to really highlight Lisp in all its abstract glory.  Suddenly, Scala seemed like the most mainstream topic of discussion and monads were everywhere except within the understanding of most attendees.

I read The Essence of Functional Programming for the second or third time on the plane and am not a professor or language author so I'd like to take a stab at explaining monads to folks accustomed to imperative programming languages.

This is not an exhaustive discussion of monads.  This is what I consider to be the prerequisite material that no one bothered to cover in their talks.  These are the three monad laws implemented and tested in Python.

Such contrived code has no place in a real Python application but now with monads solved your only problem is homoiconic syntax.  To take monads to the next level, reimplement M, unitM, and bindM to encapsulate side effects (such as the primitive logging above), propagate error messages and statistics (both as demonstrated in the paper), or perhaps automatically memoize k.