State, Transformed
Error handling is just one example of how we can use monad transformers to add capabilities to other our computations. Let’s look at another example by revisiting the State monad that we defined earlier in this chapter. We defined State as a function from an old state to a value and some new state:
| newtype State s a = State {runState :: s -> (a, s)} |
The State monad was useful because it gave us a way to write pure functions that needed access to mutable state. It’s not hard to imagine that we might have other computations that would benefit from the ability to deal with mutable state. We can do that by modeling a state transformer named StateT as a function from an input state to some monadic value that computes a value ...
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.