(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*)
在REPL中返回(WHISKEY BUCKET)
。
obj
如何传递到at-loc-p
? objects-at
的所有参数均未命名为obj
。
答案 0 :(得分:1)
objects-at
的所有参数均未命名为obj
,但at-loc-p
的参数之一(实际上是唯一参数)是。{1}}。因此,当使用参数(at-loc-p
)调用remove-if-not
时,该参数将传递给名为at-loc-p
的{{1}}。
答案 1 :(得分: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)))
除了全局定义函数at-loc-p
。