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.
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.
(let ((calls 0))
(let ((later
(delay (begin (set! calls (+ calls 1)) calls))))
(list (force later) (force later) calls)))- Output
- —
- Value
- —
- Diagnostic
- —
The first program returns (1 1 1).
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.
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.