Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: How to calculate the number of arguments a function takes. (its arity).

105 views
Skip to first unread message

WJ

unread,
Apr 9, 2012, 9:22:01 PM4/9/12
to
> > Is there a portable way to calculate a function's arity in CL? I've
> > skimmed the CLtL2 book without success. I suppose it can't be done
> > because a function can take optional arguments... I'm I missing
> > something obvious?
>
> No, no portable way, but you could look at the source code of SLIME
> and there at the definition of the ARGLIST function for the various
> Lisps supported by SLIME. All of them provide one way or another to
> access the lambda list of a function.

Since CL is not a Lisp, there is no portable way to do that.

Let's try it in a Lisp (NewLisp):

: (define (foo a b c) (* a b c))
(lambda (a b c) (* a b c))

: (nth 0 foo)
(a b c)

: (length (nth 0 foo))
3



Paul Graham, May 2001:

A hacker's language is terse and hackable. Common Lisp is not.

The good news is, it's not Lisp that sucks, but Common Lisp.

Historically, Lisp has been good at letting hackers have their
way. The political correctness of Common Lisp is an aberration.
Early Lisps let you get your hands on everything.

WJ

unread,
Apr 10, 2012, 4:41:30 AM4/10/12
to
Clojure:

user=> (defn f [a b c] (list c b a))
#'user/f
user=> (:arglists (meta #'f))
([a b c])
user=> (count (first (:arglists (meta #'f))))
3

kod...@eurogaran.com

unread,
Apr 10, 2012, 5:34:38 AM4/10/12
to
El martes, 10 de abril de 2012 03:22:01 UTC+2, WJ escribió:
> > > Is there a portable way to calculate a function's arity in CL? I've
> > > skimmed the CLtL2 book without success. I suppose it can't be done
> > > because a function can take optional arguments... I'm I missing
> > > something obvious?

Yes :
(defun cube (x) (* x x x)) -> cube
(second (function-lambda-expression (function cube))) -> (x)
(length (second (function-lambda-expression (function cube)))) -> 1

ugih li

unread,
Apr 10, 2012, 7:09:21 AM4/10/12
to
Cool!
CL is the most COMMON lisp!

Elias Mårtenson

unread,
Apr 11, 2012, 10:56:07 AM4/11/12
to
Except of course that it's not portable, as an implementation has the right to let FUNCTION-LAMBDA-EXPRESSION always return NIL,T,NIL for any function.

Pascal J. Bourguignon

unread,
Apr 11, 2012, 12:58:10 PM4/11/12
to
Have a look at:
http://www.informatimago.com/develop/lisp/small-cl-pgms/ibcl/index.html

--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.

WJ

unread,
Apr 22, 2012, 6:33:42 AM4/22/12
to
Racket:

> (procedure-arity modulo)
2

WJ

unread,
Apr 16, 2013, 4:20:51 PM4/16/13
to
Instead of CL, tCL.

% proc foo {a b c d} {puts ok}
% foo 2 3 4 5
ok
% info args foo
a b c d
0 new messages