Is there a function that returns the number of arguments a function has?
In article <3218B954.5...
@student.umass.edu> Bob Green <rgr...
it would behave like this for example:
(args member) => 2
(args atom) => 1
(args 'dog) => 0
Probably you'd want quotes for each of those arguments.
There is no function that does this as part of the standard. Most
implementations do provide something to get you similar info;
specifically, the argument list of the function. Look for a function
called ARGLIST, FUNCTION-ARGLIST, FUNCTION-LAMBDA-LIST, or variations
on that. LispWorks calls it FUNCTION-LAMBDA-LIST. (APROPOS "ARG")
and (APROPOS "FUNCTION") and (APROPOS "LAMBDA") may be of some help if
you run into a wall. Typically, though, these will be functions that
return the argument list, not an integer. This is because the answer
may not reduce easily to a number.
Consider that for (X &OPTIONAL Y) you'd need to return something
indicating "1-2 args". Also, (X &REST Y) is "1 or more".
And for (X &KEY Y), whether you'd want "1-2" or "1
or 3" is a matter of personal philosophy and whether you think the :Y
is an argument. Also, strictly, (X &KEY Y) can take any odd number of
arguments if they are the right ones. e.g., given this definition:
(DEFUN FOO (X &KEY Y) (LIST X Y))
All of the following are valid calls:
(FOO 'A :Y 3) => (A 3)
(FOO 'A :Y 1 :Y 2) => (A 1)
(FOO 'A :Y 1 :Y 2 :Y 3) => (A 1)
(FOO 'A :X 1 :ALLOW-OTHER-KEYS T) => (A NIL)
(FOO 'A :X 1 :Y 2 :Z 3 :ALLOW-OTHER-KEYS T) => (A 2)
(FOO 'A :X 1 :ALLOW-OTHER-KEYS T :ALLOW-OTHER-KEYS NIL) => (A NIL)
(FOO 'A :Y 1 :ALLOW-OTHER-KEYS NIL :ALLOW-OTHER-KEYS NIL) => (A NIL)
So returning something like "1 or 2" or "1 or 3" would be misleading for
this function since there are other possible numbers that "work".
It's better for an implementation to tell you what the argument list is and
for you to infer other information from that. NOTE though: Even
implementations that offer this info may also provide a way to "flush" the
info (which many regard really as "debugging info" and don't want in
delivered application). So it may be useful in debugging, but don't rely on
it for a delivered application without consulting your vendor documentation
and/or vendor support people to be sure you understand when it will hang
around and when not.
This is a much-requested function by the Lisp user community. If I'd had
my way, it would have been specified by the standard.