我认为由于 Emacs Lisp 和 Common Lisp 在语法上看起来非常相似,所以我可以只需按照我在 RosettaCode 上找到的示例代码进行编写。但事实证明我错了。
相关代码如下:
(defun print-name (&key first (last "?"))
(princ last)
(when first
(princ ", ")
(princ first))
(values))
根据RosettaCode的说法,它应该执行以下操作:
> (print-name)
?
> (print-name :first "John")
?, John
> (print-name :last "Doe")
Doe
> (print-name :first "John" :last "Doe")
Doe, John
现在,问题来了:每当我尝试在我的ELisp解释器中运行该函数时,我都会收到以下错误提示:
Now, here's the thing; whenever I try to run that function in my ELisp interpreter, I get the following error:
*** Eval error *** Wrong number of arguments: (lambda (&key first (last "?")) (princ la\
st) (if first (progn (princ ", ") (princ first))) (values)), 0
我对Lisp不够熟悉,不知道这是什么意思,而且无论怎么搜索也找不到答案。
那么在Emacs Lisp中正确的做法是什么?
values
函数是另一个 Common Lisp 的特色。在cl-lib
中它被称为cl-values
。 - legoscia