想要在CLISP中使用EVAL访问词法定义的函数

reb*_*oob 5 clisp

为什么这段代码不起作用?

(setf x '(foo bar (baz)))

(labels 
    ((baz () (print "baz here"))) 
    (baz) ;works
    (eval (third x))) ;fails with the below message

*** - EVAL: undefined function BAZ
Run Code Online (Sandbox Code Playgroud)

我正在使用GNU CLISP.

dan*_*lei 3

在 Common Lisp 中,eval在空词法环境中计算其参数,因此baz无法找到词法绑定函数。

虽然 Common Lisp 标准没有提供访问词法环境并调用 eval 的可移植方法,但您的实现可能具有此功能。例如,在 CLISP 中:

cs-user> (setf x '(foo bar (baz)))

(foo bar (baz))
cs-user> (labels ((baz () (print "baz here"))) 
           (eval-env (third x) (the-environment)))

"baz here" 
"baz here"
cs-user> 
Run Code Online (Sandbox Code Playgroud)

有关其他方法,请参阅 geocar 的答案。