Stacking Transformers Effectively

Now that we’ve seen how to combine different effects using monad transformers, and defined transformers for both ExceptT and StateT, we find ourselves with a bit of a problem: how should we nest them? Consider the parser example we’ve been using throughout this chapter. Should we define one by nesting a StateT inside of an ExceptT, or vice versa? Let’s take a look at the two options to see what impact the choice we have might make.

Nesting State Inside of Except

Let’s start by continuing the pattern we’ve used so far in this chapter, using State as the inner monad and adding ExceptT to give it the ability to handle errors. First, we’ll create a new module, add some imports, and define a type alias for our parser: ...

Get Effective Haskell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.