(Lispex)sicp.io
3.1 · 대입과 이력

프로시저는 앞서 일어난 일을 기억할 수 있다.

지역 바인딩은 뒤의 호출이 앞의 호출이 남긴 값을 볼 수 있을 때 상태가 됩니다.

생각해 볼 질문

답이 이전 호출에 의존하기 시작하면 무엇이 달라질까요?

  • 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.

리스펙스 · SICP 코드SICP에 필요한 Scheme 호환 문법을 리스펙스 SICP 프로필로 실행합니다.
(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))))))
리스펙스 학습용 런타임리스펙스 SICP 프로필 1.0.0
리스펙스 SICP 런타임 불러오는 중
리스펙스 · SICP 코드UTF-8 384 / 1,048,576바이트
예제
결과
출력
진단
보이는 실행 흐름0 / 0 개의 실행 이벤트
    이 브라우저 결과는 리스펙스 바우치나 권한이 아닙니다.wasm —
    예상 관찰

    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.