0
(defparameter *objects* '(whiskey bucket frog chain))

(defparameter *object-locations* '((whiskey living-room)
                                   (bucket living-room)
                                   (chain garden)
                                   (frog garden)))

(defun objects-at (loc objs obj-locs)
  (labels ((at-loc-p (obj)
             (eq (cadr (assoc obj obj-locs)) loc)))
    (remove-if-not #'at-loc-p objs)))

(objects-at 'living-room *objects* *object-locations*)

(WHISKEY BUCKET)在 REPL 中返回。

是如何obj进入的at-loc-p?的任何参数objects-at都没有命名obj

4

2 回答 2

1

没有一个参数objects-at是命名obj的,但是其中一个参数(实际上是唯一的参数)at-loc-p是。因此,当at-loc-p使用参数 (by remove-if-not) 调用时,该参数将at-loc-p使用 name传递给obj

于 2012-10-18T15:30:14.473 回答
0

labels定义功能。所以

(labels ((square (x) (* x x))) (square 2))

是相同的

(let ((square (lambda (x) (* x x)))) (funcall square 2))

并且x(在您的示例中,obj) 只是square( at-loc-p) 函数的参数名称。

编写函数的另一种方法objects-at

(defun objects-at (loc objs obj-locs)
  (defun at-loc-p (obj)
    (eq (cadr (assoc obj obj-locs)) loc))
  (remove-if-not #'at-loc-p objs)))

除了 this 全局定义函数at-loc-p

于 2012-10-18T15:30:48.247 回答