Common Lisp中的函数名称和动态绑定 [英] Function name and dynamic binding in Common Lisp
问题描述
我正在阅读Peter Norvig的 AI范例.在6.2章中,作者使用如下代码(不是原始代码,我挑选了麻烦的部分):
I'm reading Peter Norvig's Paradigms of AI. In chapter 6.2, the author uses code like below (not the original code, I picked out the troubling part):
代码段:
(progv '(op arg) '(1+ 1)
(eval '(op arg)))
作为作者的初衷,此代码应该返回 2 ,但是在sbcl 1.1.1
中,解释器显然没有查找 op 在环境中,丢弃op: undefined function
.
As the author's original intent, this code should return 2, but in sbcl 1.1.1
, the interpreter is apparently not looking up op in the environment, throwing out op: undefined function
.
此实现特定吗?由于该代码必须已经在其他Lisp上进行了测试.
Is this implementation specific? Since the code must have been tested on some other lisp.
p.s 原始代码
推荐答案
您可能是说
(progv '(op arg) '(1+ 1)
(eval '(funcall op arg)))
编辑(2013-08-21):
PAIP是在ANSI-Common-Lisp之前的时代编写的,因此代码很可能 该标准包含一些不合规之处.我们可以做 这些示例适用于以下版本:
Edit(2013-08-21):
PAIP was written in pre-ANSI-Common-Lisp era, so it's possible the code there contains a few noncompliances wrt the standard. We can make the examples work with the following revision:
(defun match-if (pattern input bindings)
"Test an arbitrary expression involving variables.
The pattern looks like ((?if code) . rest)."
(and (eval (reduce (lambda (code binding)
(destructuring-bind (var . val) binding
(subst val var code)))
bindings :initial-value (second (first pattern))))
(pat-match (rest pattern) input bindings)))
;; CL-USER> (pat-match '(?x ?op ?y is ?z (?if (eql (?op ?x ?y) ?z))) '(3 + 4 is 7))
;; ((?Z . 7) (?Y . 4) (?OP . +) (?X . 3) (T . T))
;; CL-USER> (pat-match '(?x ?op ?y (?if (?op ?x ?y))) '(3 > 4))
;; NIL
这篇关于Common Lisp中的函数名称和动态绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!