sicp.io
5.2.1 · State in named slots

Registers make every changing value explicit.

A machine state can be represented as a fixed collection of register values, while one transition computes the next state.

Guiding question

What information must be present to resume a computation?

  • Represent registers as one explicit state value
  • Separate a single transition from repeated execution
  • Read a final state instead of only a final number

The state list holds a, b, and the number of completed transitions. step performs one Euclidean reduction and returns a new list without hiding any changing value.

run does not contain the arithmetic rule itself. It asks whether b is zero and otherwise repeats step, so the transition rule and the controller remain separate.

SICP code316 of 1,048,576 UTF-8 bytes
Examples
Result
Output
Value
Diagnostic
Execution trace0 / 0 events
    Programs run in the browser with their result and execution trace.
    Expected result

    The first program returns the final register state (2 0 4). The gcd is 2 and four transitions completed.

    Trace focus

    Follow each step application and watch the old b become the next a. The state value records the transition count alongside the arithmetic registers.

    Try it yourself

    Change the program and compare the result.

    Run the machine with a equal to 30 and b equal to 18. Predict the complete final state, including the transition count.

    Show hint

    Write one new list after each remainder operation.

    Complete this lesson

    0 of 23 lessons complete in this chapter0%