# Obligatory Monad Post

I'm taking a break from blogging for a while, but before I go, here is a (now cliché) blog post on monads.

There are far more precise, formal descriptions out there; however, I'll give a practical but imprecise description of monads that I believe makes them easier to think about:

A monad is a datatype

^{1}that allows us to: represent computations^{2}that have some structured^{3}result, and chain such computations together^{4}

- The datatype defines two operations, which (in Haskell parlance) are called
*bind*(»=) and*return*.*return*places a value in the monad, and*bind*is used to pass this monadic value to another function. Implementations of these operations must satisfy certain laws. - Some expression to be evaluated.
- The result has some additional context. For example, that context could be that the computation may fail (Haskell’s Maybe monad), or that it is non-deterministic (List), or that some interaction with the outside-world occurs (IO).
- Sequential composition. We can combine monadic values by passing them between functions that operate on them (using the bind operator)

Here’s an example in Haskell:

As a bonus, here’s an example in Scala making use of the Writer monad from the Scalaz library. The Writer monad is useful for computations that accumulate some additional output alongside the result (e.g. logging).

I hope this has been useful 🙂