sicp.io
3.5.1 · Delayed evaluation

A promise computes once and remembers the value.

delay packages work for later, while force performs that work at most once and reuses the memoized result.

Guiding question

How can two force calls cause only one evaluation?

  • Separate promise construction from promise forcing
  • Observe the first computed force and a memoized hit
  • Use an effect counter to reveal hidden evaluation

Creating later does not run its body. The first force changes calls from 0 to 1 and stores the resulting value in the promise.

The second force returns the stored value without running the body again. The final calls value remains 1, making memoization visible in the result.

SICP code136 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 (1 1 1).

    Trace focus

    Locate one force event with a computed outcome and the later force event with a memoized-hit outcome. They refer to the same promise allocation.

    Try it yourself

    Change the program and compare the result.

    Force later three more times and include calls at the end of the result. Predict which values can change.

    Show hint

    Once a promise stores a value, later force operations return it without evaluating the delayed body.

    Complete this lesson

    0 of 21 lessons complete in this chapter0%