sicp.io
3.1.4 · The costs of introducing assignment

The same expression can stop meaning the same computation.

Expose history dependence, order dependence, and alias-sensitive observations that appear when procedure calls can change remembered locations.

Guiding question

Which substitution and reordering arguments become invalid once an expression can mutate state?

  • Compare repeated stateful calls with repeated pure calls
  • Recognize that equal source expressions can produce different values over time
  • Make two evaluation schedules explicit instead of relying on operand order
  • Observe how one earlier call changes the meaning of a later call
  • Record the exact finite schedule and its evaluation order

Two calls to the same withdraw procedure with the same argument return 90 and then 80 because the first call changes the captured balance. Replacing both calls with one previously computed value would therefore change the program. The pure withdraw-value procedure has no hidden history, so two calls from the same explicit balance both return 90.

The order probe makes schedule dependence explicit with let bindings. Calling the zero message first changes state before the one message reads it. Calling one first observes the old state. No conclusion here depends on the evaluator choosing an operand order; the two sequences are written separately and compared as data.

SICP code333 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 repetition program returns (90 80 90 90). The explicit schedules return ((0 1 1) (0 0 0)).

    Trace focus

    Find the balance assignment between the two identical stateful calls, then confirm that the pure calls allocate no persistent location. In the schedule comparison, follow the first call before the second and identify the exact read whose value changes.

    Try it yourself

    Change the program and compare the result.

    Add a reset message to the order probe and compare zero–reset–one with zero–one–reset. Explain which algebraic replacement or call reordering would change each observation.

    Show hint

    Write every call in a nested let so the schedule is part of the source rather than an assumption about argument evaluation.

    Complete this lesson

    0 of 21 lessons complete in this chapter0%