sicp.io
3.1.1 · Assignment and history

A procedure can remember what happened.

A local binding becomes state when later calls can observe the value left by earlier calls.

Guiding question

What changes when an answer depends on the calls that came before it?

  • Distinguish a returned value from stored state
  • Follow assignment across successive calls
  • Recognize when call order becomes observable

withdraw closes over the balance binding. Each successful call changes that binding before returning its new value, so the next call begins from a different balance.

Three nested single-binding let expressions make the sequence explicit. Each result therefore reflects the state left by the preceding call instead of starting again from 100.

SICP code384 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 (75 50 insufficient-funds). The failed withdrawal leaves the balance at 50.

    Trace focus

    Find each application of withdraw and the set! expression inside the two successful calls. The final call reaches the other branch and performs no assignment.

    Try it yourself

    Change the program and compare the result.

    Insert a withdrawal of 10 between the two withdrawals of 25. Predict every returned value before running the changed sequence.

    Show hint

    Write down the balance after each successful call. A failed call does not change it.

    Complete this lesson

    0 of 21 lessons complete in this chapter0%