프로시저는 앞서 일어난 일을 기억할 수 있다.
지역 바인딩은 뒤의 호출이 앞의 호출이 남긴 값을 볼 수 있을 때 상태가 됩니다.
답이 이전 호출에 의존하기 시작하면 무엇이 달라질까요?
- Distinguish a returned value from stored state
- Follow assignment across successive calls
- Recognize when call order becomes observable
withdraw closes over the balance binding. Each successful call changes that binding before returning its new value, so the next call begins from a different balance.
Three nested single-binding let expressions make the sequence explicit. Each result therefore reflects the state left by the preceding call instead of starting again from 100.
(let ((balance 100))
(let ((withdraw
(lambda (amount)
(if (>= balance amount)
(begin
(set! balance (- balance amount))
balance)
'insufficient-funds))))
(let ((first (withdraw 25)))
(let ((second (withdraw 25)))
(let ((third (withdraw 60)))
(list first second third))))))- 출력
- —
- 값
- —
- 진단
- —
The first program returns (75 50 insufficient-funds). The failed withdrawal leaves the balance at 50.
Find each application of withdraw and the set! expression inside the two successful calls. The final call reaches the other branch and performs no assignment.
힌트를 보기 전에 프로그램을 바꿔 보세요.
Insert a withdrawal of 10 between the two withdrawals of 25. Predict every returned value before running the changed sequence.
힌트 하나 보기
Write down the balance after each successful call. A failed call does not change it.