sicp.io
3.2.4 · Internal definitions and local procedure groups

Internal names can describe one cooperating local program.

A procedure invocation can create a private environment for several named helpers, including mutually recursive helpers installed by the local definitions.

Guiding question

How can several local procedures refer to one another without leaking their names into the surrounding environment?

  • Distinguish a local definition from a global binding
  • Follow mutually recursive helpers through one invocation environment
  • Explain why helper references are resolved when the closures are applied
  • Model scan-out as bindings created before assignments install procedure values
  • Keep an explicit unassigned marker separate from a valid procedure value

classify-parity creates even-step and odd-step inside each call. Both closures share the invocation environment, so even-step can call odd-step and odd-step can call even-step even though neither name becomes global. The calls happen only after both local definitions have been processed. A fresh call to classify-parity creates another private group of bindings.

The second program treats definitions as quoted data and builds the scan-out shape used to reason about simultaneous local scope: create every name with an explicit unassigned marker first, then install each procedure with set!, then evaluate the remaining body. This run constructs the transformation as data and makes its unassigned and set! structure explicit.

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 first program returns ((#f #t) (#t #f)). The second returns a let form with even-step and odd-step bound to quoted *unassigned* markers, followed by two set! forms and the original body.

    Trace focus

    In the first run, find the environment created by classify-parity and follow alternating applications without a global even-step or odd-step lookup. In the second, separate reading quoted definition data from constructing bindings, lambda values, assignments, and the final body. The generated form is an explanatory model of this finite input and makes every transformation step visible.

    Try it yourself

    Change the program and compare the result.

    Add a local divisible-by-three? helper group that cycles through three mutually recursive procedures. Keep all helper names inside one public classifier and predict the results for 8 and 9.

    Show hint

    Each helper represents one remainder class and calls the next helper after subtracting 1. Only the remainder-zero helper returns true at zero.

    Complete this lesson

    0 of 21 lessons complete in this chapter0%