問題5.39 – SICP(計算機プログラムの構造と解釈)その290

問題5.39

(define (lexical-address-frame-number lexical-address)
  (car lexical-address))

(define (lexical-address-displacement-number lexical-address)
  (cadr lexical-address))

(define (make-lexical-address frame-number displacement-number)
  (list frame-number displacement-number))

(define (lexical-address-lookup lexical-address env)
  (let ((frame-num (lexical-address-frame-number lexical-address))
        (displacement-num (lexical-address-displacement-number lexical-address)))
       (let ((frame (list-ref env frame-num)))
             (let ((value (list-ref (frame-values frame) displacement-num)))
                  (if (eq? value '*unassigned*)
                      (error "Unassigned variable")
                      value)))))

(define (lexical-address-set! lexical-address val env)
  (let ((frame-num (lexical-address-frame-number lexical-address))
        (displacement-num (lexical-address-displacement-number lexical-address)))
       (let ((frame (list-ref env frame-num)))
             (define (iter vrs vls count)
               (cond ((null? vrs)
                      (error "Unbound variable - LEXICAL-ADDRESS-SET!"))
                     ((= count 0)
                      (set-car! vls val))
                     (else
                       (iter (cdr vrs) (cdr vls) (- count 1)))))
             (iter (frame-variables frame) (frame-values frame) displacement-num))))
計算機プログラムの構造と解釈
ジェラルド・ジェイ サスマン ジュリー サスマン ハロルド エイブルソン
ピアソンエデュケーション
売り上げランキング: 6542
«
»