Skip to Content
Effective Haskell
book

Effective Haskell

by Rebecca Skinner
July 2023
Intermediate to advanced
670 pages
17h 13m
English
Pragmatic Bookshelf
Content preview from Effective Haskell

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Grokking Functional Programming

Grokking Functional Programming

Michal Plachta
Grokking Algorithms

Grokking Algorithms

Aditya Bhargava
The Rust Programming Language, 2nd Edition

The Rust Programming Language, 2nd Edition

Steve Klabnik, Carol Nichols

Publisher Resources

ISBN: 9798888650400Errata Page