liveBook · Manning

State-passing

As explained in Functional Design and Architecture: Examples in Haskell

State-passing is a technique in which state is supplied as a function argument and the resulting computation returns updated state.

State-passing treats state as an explicit input and returns the resulting updated state from the computation. It provides a pure, functional imitation of mutation without destroying the previous value. section 7.2.1 section 7.1

The basic design is straightforward: supply the current state, perform a calculation, and return the new state. An interpreter can therefore traverse nested methods while threading Runtime through each call; nested interpreters receive the appropriate outer interpreter as an argument. section 5.3.2

Updating a spaceship model

A rotation script reads the spaceship's mass, calculates a thruster impulse, recalculates the mass, and repeats the process. Each step produces a new model for the next step rather than modifying the previous model directly. section 7.2.1

The approach becomes cumbersome when state is deeply nested or when every operation also returns Either or Maybe for errors and optional values. State monads package the same underlying technique into a monadic form, while mutable references can be more convenient at an implementation boundary or in concurrent systems. section 7.2.1 section 7.3

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Explains input-to-updated-state flow, nested interpreter threading, and boilerplate limits before deferring convenience to state monads.
Examples
Uses spaceship rotation, mass recalculation, and hierarchical DSL interpreters as worked state-threading examples.
What it assumes
Assumes functions, immutable values, and basic functional programming while distinguishing state from ordinary values.
What you can do after
Apply it to model immutable updates and thread runtime state explicitly through functional computations.
How it connects
Connects pure state with state monads, monad transformers, mutable state, concurrency, and software transactional memory.

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.