A finite-state machine models behavior with immutable state and input-driven transitions. In a purely functional design, functions compute each next state from the previous one, while State removes sequencing boilerplate. section 6.6
A candy dispenser illustrates the model:
- Accept a coin only when locked and candy remains; unlock the machine and increase its coins. section 6.6
- Dispense candy only when unlocked and then lock the machine; ignore invalid inputs and every input when empty. section 6.6
Simulation interface
A simulation consumes a list of inputs and returns the final coin and candy counts through State[Machine, (Int, Int)]. section 6.6








