Type Slowly

Feb 10

Monads, Mo’ Problems.

I don’t think I’ve really got anything new to offer that existing monad tutorials don’t, so I’m going to skip the seemingly compulsory step in the life of a fledgling Haskell programmer of writing a tutorial on monads. However, having recently started to properly ‘get’ what monads are and how they work, I thought I might write a little about how I got to that point - sort of like a literature review of the monad tutorials that are already out there; a ‘meta-tutorial’ of some sort.

So, in a few steps, here’s how I got to a point where I’ve started to feel like I properly understand and am comfortable using monads as an abstraction in my code:

  • Prerequisites: This stuff only properly started to fall into place once I had a good grasp of lambda abstractions and list comprehensions or their distant mathematical cousin, set-builder notation. (the latter for reasons that will become clear in a few steps time). Take some time to properly cover the basics first.
  • Read a monad tutorial in the language you use most day-to-day. This helped me understand the definitions of ‘bind’ and ‘return’, but how monads are actually a useful abstraction (and what on earth they have to do with state, IO, and all the other magic they do in Haskell) still eludes me.
  • Spend a while constantly coming across claims that tools I use everyday are actually Monads in disguise. Remain vaguely confused about why, but develop an intuitive sense of how to spot a Monad in the wild in any case.
  • Read Philip Wadler’s ‘Comprehending Monads’ paper (Did I mention that understanding list comprehensions will help you massively?). 
  • Attain a state of blissful Monad nirvana. Realise that Monads aren’t a technique for implementing stateful imperative programming in a pure functional language, but rather that imperative stateful programming is itself a monad.
  • Write a monad tutorial about your discovery.

…and if spurious real-world metaphors are your sort of thing, I found that Burritos were the most useful analogy (seriously, they are).


blog comments powered by Disqus
Page 1 of 1