liveBook · Manning

StateT

As explained in Learn Haskell by Example

StateT is a Haskell monad transformer that threads and updates state through computations in an underlying monad.

StateT is a Haskell monad transformer that adds readable and updatable state to computations in an underlying monad. StateT s m a records the state type, underlying monad, and computation result. section 15.1.2

Use the core operations to manage state during a computation:

Counting directories during recursive traversal

Define type App a = StateT Int IO a, perform filesystem operations with liftIO, increment the counter with modify, and run the computation using execStateT (example "foo") 0. A richer application state can track recursion depth and transferred files with a Stats record, helper modification functions, and a Monoid identity for an empty state. section 15.1.2

How Learn Haskell by Example covers it

How deep it goes
Defines the type parameters and three essential operations, then demonstrates recursive state updates while deferring less-used functions.
Examples
Uses recursive directory traversal, counters, a Stats record, modify helpers, execStateT, and GHCi execution.
What you can do after
Apply it via Control.Monad.Trans.State.Strict to build effectful computations that read, replace, and update threaded state.
How it connects
Connects StateT with ReaderT, WriterT, RWST, monad transformers, MonadIO, and the underlying IO monad.

Also explained in

Compare how 2 books explain StateT →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.