(Lispex)sicp.io
3.3 · 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.

Lispex · SICP sourceScheme-compatible SICP syntax executed by the Lispex SICP profile.
(let ((calls 0))
  (let ((later
         (delay (begin (set! calls (+ calls 1)) calls))))
    (list (force later) (force later) calls)))
Lispex learning runtimeLispex SICP profile 1.0.0
Loading Lispex SICP runtime
Lispex · SICP source136 / 1,048,576 UTF-8 bytes
Examples
Result
Output
Value
Diagnostic
Visible execution0 / 0 trace events
    This browser result is not a Lispex Vouch record or authority.wasm —
    Expected observation

    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 before you read the hint.

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

    Show one hint

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