灯下 登录
计算机科学 / SICP / 5.5.6 Lexical Addressing

Exercise 5.41 · 习题

Exercise 5.41: Write a procedure
find-variable that takes as arguments a variable and a compile-time
environment and returns the lexical address of the variable with respect to
that environment. For example, in the program fragment that is shown above,
the compile-time environment during the compilation of expression ⟨e1⟩ is
((y z) (a b c d e) (x y)). Find-variable should produce

(find-variable
'c '((y z) (a b c d e) (x y)))
(1 2)

(find-variable
'x '((y z) (a b c d e) (x y)))
(2 0)

(find-variable

'w '((y z) (a b c d e) (x y)))

not-found

练习 5.41:编写过程 find-variable,它以一个变量和一个编译时环境为参数,返回该变量相对于该环境的词法地址。例如,在上述程序片段中,编译表达式 ⟨e1⟩ 时的编译时环境为 ((y z) (a b c d e) (x y))。find-variable 应产生如下结果:

(find-variable
'c '((y z) (a b c d e) (x y)))
(1 2)

(find-variable
'x '((y z) (a b c d e) (x y)))
(2 0)

(find-variable
'w '((y z) (a b c d e) (x y)))
not-found

Racket #lang sicp
(find-variable
 'c '((y z) (a b c d e) (x y)))
(1 2)

(find-variable
 'x '((y z) (a b c d e) (x y)))
(2 0)

(find-variable
 'w '((y z) (a b c d e) (x y)))
not-found