liveBook · Manning

runState

As explained in Functional Design and Architecture: Examples in Haskell

runState is a Haskell state-monad function that executes a stateful computation from an initial state and returns its result together with the final state.

runState executes a stateful computation from an initial state and returns both the computation’s result and its final state. For a Haskell State value, provide the script and starting state; the function runs the state transitions rather than requiring explicit state arguments. section 7.2.2

runState
 :: State state returnValue
 -> state
 -> (state, returnValue)

The type signature shows runState consuming a State computation and an initial state, then producing the final state together with the return value. section 7.2.2

Running an age script

Running bornAndGettingOlderTwice with initial age 111 yields two messages and a final age of 2. A test checks both the returned messages and the resulting state. section 7.2.2

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Defines the execution signature and demonstrates a test, while deferring detailed treatment of monad stacks and transformers.
Examples
Uses an age-changing script, initial age 111, returned messages, and assertions about the final age.
What it assumes
Assumes familiarity with Haskell state-monad values and leaves monad transformers for separate study.
What you can do after
Apply it via Haskell’s State API to execute stateful computations and inspect their outputs and final state.
How it connects
Connects State computations with state-monad scripts, StateT, free monads, and STM.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.