promise는 한 번 계산하고 그 값을 기억한다.
delay는 일을 나중으로 미루고 force는 그 일을 최대 한 번 수행한 뒤 메모이즈된 결과를 다시 사용합니다.
생각해 볼 질문
force를 두 번 호출해도 계산이 한 번만 일어나는 이유는 무엇일까요?
- 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)))리스펙스 학습용 런타임리스펙스 SICP 프로필 1.0.0
리스펙스 SICP 런타임 불러오는 중
리스펙스 · SICP 코드UTF-8 136 / 1,048,576바이트
예제
결과—
- 출력
- —
- 값
- —
- 진단
- —
보이는 실행 흐름0 / 0 개의 실행 이벤트
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.
힌트를 보기 전에 프로그램을 바꿔 보세요.
Force later three more times and include calls at the end of the result. Predict which values can change.
힌트 하나 보기
Once a promise stores a value, later force operations return it without evaluating the delayed body.