Common Lisp中的函数名和动态绑定

Jia*_*aji 2 lisp eval common-lisp dynamic-binding

我正在阅读Peter Norvig的AI范式.在第6.2章中,作者使用如下代码(不是原始代码,我选择了令人不安的部分):

代码片段:

(progv '(op arg) '(1+ 1)
(eval '(op arg)))
Run Code Online (Sandbox Code Playgroud)

作为作者的原始意图,这段代码应该返回2,但是sbcl 1.1.1,在解释器显然没有在环境中查找操作,抛弃op: undefined function.

这个实现是否具体?由于代码必须已在其他一些lisp上进行过测试.

ps 原始代码

hua*_*uan 5

你可能意味着

(progv '(op arg) '(1+ 1)
  (eval '(funcall op arg)))
Run Code Online (Sandbox Code Playgroud)

编辑(2013年8月21日):

PAIP是在ANSI-Common-Lisp之前的时代编写的,因此其中的代码可能包含一些符合标准的非一致性.我们可以使示例使用以下修订:

(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
Run Code Online (Sandbox Code Playgroud)