Monads
This chapter introduces the king of the type constructor hierarchy: the monad. I see two main reasons for the prominent position and importance of monads:
- The first reason is an objective and pragmatic one—we can write more expressive effectful programs with monads than we can with applicative functors. In particular, the control flow of applicative functor programs is fixed upfront before it is run. Each step in an applicative program produces a result, and these results are combined to form the final result. In contrast, the control flow of monadic programs is more flexible. The result of a monadic step can determine what the next steps to take are. In essence, we have the same flexibility in monadic programs as we do in pure programs.
- This flexibility makes monads the first abstraction that Haskell programmers reach when writing effectful programs. Even when the same code can be written using either the monad or the applicative function abstraction, monad...