2016/02/09
SICP 問題 5.41
翻訳時環境に対する変数の文面アドレスを返す手続きfind-variableの実装
(define (find-variable var ct-env)
(define (env-loop frame-address env)
(define (scan variable-address frame)
(cond ((null? frame)
(env-loop (+ frame-address 1) (enclosing-environment env)))
((eq? (car frame) var)
(list frame-address variable-address))
(else
(scan (+ variable-address 1) (cdr frame)))))
(if (null? env)
'not-found
(let ((frame (first-frame env)))
(scan 0 frame))))
(env-loop 0 ct-env))
test
gosh> (find-variable 'c '((y z) (a b c d e) (x y)))
(1 2)
gosh> (find-variable 'x '((y z) (a b c d e) (x y)))
(2 0)
gosh> (find-variable 'w '((y z) (a b c d e) (x y)))
not-found