State monad
A state monad represents a computation as a function from an input state to a result and an updated state, allowing stateful computations to be composed.
Type-Driven Development with Idris
The state monad represents computations that read and update state while producing a result.
- How it is used
- Encapsulates local state management in pure algorithms and separates stateful descriptions from execution.
- How deep it goes
- Develops library usage and a custom interpreter with Functor, Applicative, and Monad implementations, while deferring MonadState interface details.
- Examples
- Uses a natural-number increment, depth-first tree labeling, and a running total of positive integers.
- What you can do after
- Build a custom State interpreter and apply Idris State operations to recursive algorithms.
Read how Type-Driven Development with Idris explains state monad →
Functional Design and Architecture: Examples in Haskell
A state monad is a monadic abstraction that threads changeable state through computations while presenting state transitions as composable values.
- How it is used
- Organizes stateful functional computations without repeatedly passing state as an ordinary argument.
- How deep it goes
- Explains get, put, bind, runners, transformer stacks, and lifting through worked Haskell examples.
- Examples
- Uses an age-increment script, a Minesweeper board and score, and a spaceship rotation script.
- What you can do after
- Apply it via Haskell’s State and StateT to sequence updates, combine state with IO, and check final state in tests.
Read how Functional Design and Architecture: Examples in Haskell explains state monad →
Functional Programming in Scala, Second Edition
A state monad represents a stateful computation as a function from an input state to a result paired with an output state.
- How it is used
- Represents stateful computations as composable values instead of directly mutating shared state.
- How deep it goes
- Defines unit, flatMap, get, and set, then demonstrates state threading; exercises defer laws and combinator behavior.
- Examples
- Uses zipWithIndex to number list elements by incrementing an Int state through a for-comprehension.
- What you can do after
- Build it from scratch by implementing State, unit, flatMap, get, set, and state-threading programs.
Read how Functional Programming in Scala, Second Edition explains state monad →
What to know first
- HaskellHaskell is a statically typed, purely functional general-purpose programming language with non-strict, typically lazy evaluation.
- monadA monad is a programming abstraction that provides operations for sequencing computations while encapsulating and preserving their computational context.
Related concepts
- applicative functorimplements · An applicative functor is a functor with operations for applying a function in a computational context to a value in that context and combining the computations.
- functorimplements · A functor is an abstraction that maps values or objects while preserving the structure and relationships of their surrounding context.
- monadimplements · A monad is a programming abstraction that provides operations for sequencing computations while encapsulating and preserving their computational context.
- putuses · put is a state operation that replaces the current state with a supplied value.
Questions about state monad
What is state monad?
A state monad represents a computation as a function from an input state to a result and an updated state, allowing stateful computations to be composed.
Which Manning books explain state monad?
Type-Driven Development with Idris, Functional Design and Architecture: Examples in Haskell and Functional Programming in Scala, Second Edition.
What should I know before learning state monad?
Manning books that teach state monad usually introduce Haskell and monad first.
What is state monad part of?
State monad is part of Control.Monad.State. Control.Monad.State is a module providing stateful computation abstractions for Haskell and Idris.
What are the parts of state monad?
State monad includes Get.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


