sicp.io
3.2.2 · Applying simple procedures

Each application adds one frame to a specific parent.

Record parameter frames and parent links while a compound procedure calls another procedure or returns a closure for a later application.

Guiding question

How do equal parameter names remain distinct across nested and repeated procedure applications?

  • Create one parameter frame per compound procedure application
  • Attach each frame to the environment stored in the procedure
  • Distinguish repeated square frames with different x values
  • Follow a returned closure from creation frame to later call frame
  • Treat the frame transcript as an explanatory model rather than runtime internals

modeled-sum-of-squares records one frame containing x = 3 and y = 4. Each modeled-square call then records a separate child frame whose own x binding is 3 or 4. The repeated parameter name does not overwrite another call because every application owns a different frame.

make-adder records the frame where x = 5 and returns a closure that keeps access to it. Calling add-five later creates a y = 7 frame linked to that captured environment. The transcript uses pedagogical frame names to make those links explicit.

SICP code558 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 (25 ((sum-of-squares global ((x . 3) (y . 4))) (square sum-of-squares ((x . 3))) (square sum-of-squares ((x . 4))))). The second returns (12 ((make-adder global ((x . 5))) (returned-procedure make-adder ((y . 7))))).

    Trace focus

    Compare the modeled frame records with the actual procedure applications. Find two distinct square calls and the parent application that requested them. In the closure run, separate the make-adder return from the later add-five application and locate the lookup of captured x.

    Try it yourself

    Change the program and compare the result.

    Add modeled-average and call it twice from one modeled procedure. Give every call frame a distinct numeric id and record both its lexical parent and the caller that requested it.

    Show hint

    Lexical parent and dynamic caller answer different questions. Keep both fields explicit in the explanatory record.

    Complete this lesson

    0 of 21 lessons complete in this chapter0%