Determining function argument list in Common Lisp

无人久伴 提交于 2019-12-19 05:22:20

问题


Is it possible to find out the argument list of a function, given a function object (or a function's symbol) in common lisp?


回答1:


This is different for each CL implementation but the Swank package (provides Slime which can show arglists in f.e. Emacs' minibuffer) wraps this up in a single function:

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

This will also work for methods:

* (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))

The easiest way to get Swank is to use Quicklisp.




回答2:


ANSI Common Lisp provides the function FUNCTION-LAMBDA-EXPRESSION, which may return a lambda expression if the implementation supports it and the expression has been recorded. In the lambda expression, the second item is the argument list - as usual.

Otherwise to return an argument list is not defined in the ANSI Common Lisp standard and is part of the specific Lisp implementation. For example in some 'delivered' Lisp applications this information may not be present.

Typically most Common Lisp implementations will have an exported function ARGLIST in some internal package.




回答3:


I don't know of a standard way but in SBCL you can use 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))


来源:https://stackoverflow.com/questions/8353661/determining-function-argument-list-in-common-lisp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!