In Python, I can do this:
>>> def foo(x,y,z=1):
return x+y*z
>>> foo.func_code.co_varnames
('x', 'y', 'z')
>>> 开发者_开发技巧foo.func_defaults
(1,)
And from it, know how many parameters I must have in order to call foo(). How can I do this in Common Lisp?
Most implementations provide a way of doing this, but none is standardized. If you absolutely need it, Swank (the Common Lisp part of SLIME) has a function called swank-backend:arglist
that, as far as I can see, does what you want:
CCL> (swank-backend:arglist 'if)
(TEST TRUE &OPTIONAL FALSE)
CCL> (swank-backend:arglist 'cons)
(X Y)
CCL> (swank-backend:arglist (lambda (a b c &rest args)))
(A B C &REST ARGS)
I'm not sure you can rely on it remaining available in the future, though.
Usually most Lisps have a function called ARGLIST in some package. LispWorks calls it FUNCTION-LAMBDA-LIST.
For information purposes in LispWorks, if one has the cursor on a function symbol, then control-shift-a displays the arglist. In LispWorks there is also an 'arglist-on-space' functionality that can be loaded. After typing a symbol and a space, the IDE displays the arglist.
There is also the CL:DESCRIBE function. It describes various objects. In most CL implementations it also should display the arglist of a function.
The following example is for Clozure Common Lisp:
Welcome to Clozure Common Lisp Version 1.6-r14468M (DarwinX8664)!
? (defun foo (x y &optional (z 1)) (+ x (* y z)))
FOO
? (arglist #'foo)
(X Y &OPTIONAL Z)
:ANALYSIS
? (describe #'foo)
#<Compiled-function FOO #x302000550F8F>
Name: FOO
Arglist (analysis): (X Y &OPTIONAL Z)
Bits: 8405508
...
If you want to know this just when editing, SLIME+emacs will take care of that for you.
e.g. In emacs lisp-mode + slime, typing
(format
will display the arguments of format in the minibuffer on the bottom.
精彩评论