在lisp中应用和关键字参数

在lisp中应用和关键字参数,lisp,keyword,apply,Lisp,Keyword,Apply,我可以在LISP中使用关键字参数 (member 'a '(a b c) :test #'eq) 但是,当我尝试使用apply调用成员方法时 (apply #'member 'a '(a b c) :test #'eq) 我收到如下错误消息: MEMBER: keyword arguments in (:TEST) should occur pairwise [Condition of type SYSTEM::SIMPLE-PROGRAM-ERROR] 解决办法是 (apply #'m

我可以在LISP中使用关键字参数

(member 'a '(a b c) :test #'eq)
但是,当我尝试使用apply调用成员方法时

(apply #'member 'a '(a b c) :test #'eq)
我收到如下错误消息:

MEMBER: keyword arguments in (:TEST) should occur pairwise
 [Condition of type SYSTEM::SIMPLE-PROGRAM-ERROR]
解决办法是

(apply #'member 'a '(a b c) '(:test eq))
而没有关键字参数

(apply #'member 'a '((a b c)))
这背后的逻辑是什么?为什么“(:test#”eq)会引发错误

补充 这就是我问这个问题的原因。 我有ANSI公共Lispbook第103页的代码

(defun our-adjoin (obj lst &rest args)
       (if (apply #'member obj lst args)
           lst
           (cons obj lst)))
当我尝试
(我们的邻接词'a'(abc))
时,它返回结果
(abc)
,但我们的邻接词不能翻译为
(apply#'member'a'(abc))
,因为它会引发错误(如中所述)


我所能想到的是,
&rest args
中的值是用来生成类似于
(apply#member'a'(abc)())
的值,以避免引发错误

apply
希望它的最终参数是一个列表。就是

(apply #'foo (list 1 2 3)) == (foo 1 2 3)
(apply #'member 'a '(a b c) :test #'eq) == ??? ; is an error - #'eq isn't a list
我不知道
apply
是如何将
#eq
(一个函数)组合成一个列表的,但这就是问题所在

您可能正在查找
funcall
,而不是
apply

(funcall #'foo 1 2 3) == (foo 1 2 3)
(funcall #'member 'a '(a b c) :test #'eq) == (member 'a '(a b c) :test #'eq)
编辑:
(应用#“成员”a'(a b c))
这和

(member 'a 'a 'b 'c)
这当然是胡说八道。将
apply
视为“扩展”其最后一个参数

编辑2:我们的邻接处的
code
所以你的假设(等价物是
(应用#'成员'a'(abc)')
)是正确的。(仅供参考,
nil
'nil
()
'()
(list)

)之间没有区别,因此我们可以使用计算参数列表调用函数

APPLY
具有以下语法:

apply function &rest args+ => result*
  • 第一个参数是一个函数

  • 然后是几个参数,至少一个,其中最后一个参数需要是一个列表


使用
funcall
可能比使用
apply
的扭曲更清晰。
apply function &rest args+ => result*