Eval错误 - 错误的类型参数:listp

时间:2015-01-31 16:52:55

标签: emacs elisp

以下代码

(setq func 'concat)
(apply func "a" "b")

抛出以下错误

***Eval error*** Wrong type argument: listp, "b"

为什么apply将来自第三个位置的所有参数作为参数用于' func'?

3 个答案:

答案 0 :(得分:4)

apply将列表作为其最后一个参数,因此这些调用是正确的:

(apply func "a" '("b"))
(apply func '("a" "b"))

要传递普通参数,您可以改为使用funcall

(funcall func "a" "b")

最后,您还可以使用apply,如下所示

(apply func "a" "b" nil)

(apply func "a" "b" ())

这是因为nil()在Emacs Lisp中被视为空列表。

答案 1 :(得分:2)

apply的典型用法是将一个函数应用于参数列表,然后在参数上“扩展”该列表。另一方面,仅需要funcall,因为elisp分离了函数和变量绑定。

(defun wrapped-fun (fun a b)
   "Wrapped-fun takes a function and two arguments. It first does something,
    then calls fun with the two arguments, then finishes off doing 
    something else."
   (do-something)
   (funcall fun a b)  ;; Had function and variable namespaces been the same
                      ;; this could've been just (fun a b)
   (do-something-else))

答案 2 :(得分:1)

apply需要一个函数和一个列表,所以请使用

(apply func '("a" "b"))

或者只是

(func "a" "b")