Subject: Re: Scheme 102
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 30 Nov 2001 10:13:54 GMT
Newsgroups: comp.lang.scheme
Message-ID: <9u7m52$cb2bj$1@fido.engr.sgi.com>
David Feuer  <David_Feuer@brown.edu> wrote:
+---------------
| bciceron@rsn.hp.com wrote:
| > is it possible to dynamically discover the number of parameters to a
| > function?
| 
| If you need to find out the number of parameters a function takes,
| PLT Scheme does offer such a feature.  (don't remember what it's called)
+---------------

<URL:http://www.cs.rice.edu/CS/PLT/packages/doc/mzscheme/node34.htm>
describes MzScheme's "arity" and "procedure-arity-includes?" primitives
and the "arity-at-least" structure type, e.g.:

	> (define (my-arity x)
	    (let ((ax (arity x)))
	      (if (or (number? ax) (pair? ax))
		ax					; BUG! [See note.]
		(list (arity-at-least-value ax) '*))))
	> (map my-arity
	       (list version pair? cons list modulo + - * / read write))
	(0 1 2 (0 *) 2 (0 *) (1 *) (0 *) (1 *) (0 1) (1 2))
	> 

[Note: If handed a procedure defined by MzScheme's "case-lambda"
<URL:http://www.cs.rice.edu/CS/PLT/packages/doc/mzscheme/node22.htm>,
"arity" can return a more complex list than `(,x ,(+ x 1)). Extending
"my-arity" for this case is left as an exercise for the reader.]


-Rob

-----
Rob Warnock, 30-3-510		<rpw3@sgi.com>
SGI Network Engineering		<http://www.meer.net/~rpw3/>
1600 Amphitheatre Pkwy.		Phone: 650-933-1673
Mountain View, CA  94043	PP-ASEL-IA

[Note: aaanalyst@sgi.com and zedwatch@sgi.com aren't for humans ]