确定Common Lisp中的函数参数列表

sab*_*bof 11 lisp common-lisp

在常见的lisp中给定函数对象(或函数的符号)是否有可能找到函数的参数列表?

aer*_*que 13

这对于每个CL实现都是不同的,但是Swank包(提供可以在fe Emacs的迷你缓冲区中显示arglists的Slime)将它包装在一个函数中:

* (defun testfn (arg1 arg2 &key (arg3 :a)) (declare (ignore arg1 arg2 arg3)))
TESTFN
* (swank-backend:arglist #'testfn)
(ARG1 ARG2 &KEY (ARG3 :A))
Run Code Online (Sandbox Code Playgroud)

这也适用于方法:

* (defmethod testmethod ((arg1 t) arg2 &key (arg3 :a)) (declare (ignore arg1 arg2 arg3)))
STYLE-WARNING: Implicitly creating new generic function TESTMETHOD.
#<STANDARD-METHOD TESTMETHOD (T T) {1005670231}>
* (swank-backend:arglist #'testmethod)
(ARG1 ARG2 &KEY (ARG3 :A))
Run Code Online (Sandbox Code Playgroud)

获得Swank的最简单方法是使用Quicklisp.


nar*_*ryl 7

我不知道标准方式,但在SBCL中你可以使用sb-introspect:function-lambda-list.

(defun test (a &rest rest &key (b 42)) nil)
(sb-introspect:function-lambda-list #'test)
=> (A &REST REST &KEY (B 42))
Run Code Online (Sandbox Code Playgroud)


Rai*_*wig 7

ANSI Common Lisp提供函数FUNCTION-LAMBDA-EXPRESSION,如果实现支持它并且已经记录了表达式,它可以返回lambda表达式.在lambda表达式中,第二项是参数列表 - 像往常一样.

否则返回参数列表未在ANSI Common Lisp标准中定义,并且是特定Lisp实现的一部分.例如,在某些"交付的"Lisp应用程序中,此信息可能不存在.

通常,大多数Common Lisp实现在某些内部包中都会有一个导出函数ARGLIST.