In article <sfwg124x9d1.fsf@world.std.com>,
Kent M Pitman <pitman@world.std.com> wrote:
+---------------
| Tim Bradshaw <tfb@tfeb.org> writes:
| > Also I have no idea if supplied-p parameters are supported in other
| > Lisps, though that may not be a big issue.
|
| Well, in scheme for example:
| (define (foo arg1 . maybe-arg2)
| (if (cdr maybe-arg2) ;supplied-p
| ... (car maybe-arg2) ... ;i.e., definitely-arg2
| ))
+---------------
I think you probably meant something like this:
(define (foo arg1 . maybe-arg2)
(if (not (null? maybe-arg2)) ;supplied-p
... (car maybe-arg2) ... ;i.e., definitely-arg2
))
+---------------
| It can often be glossed, in other words,. But it's ugly.
+---------------
Indeed. I often find myself writing destructuring macros to help out.
By the way, some Schemes have a construct called "case-lambda":
<URL:http://www.cs.rice.edu/CS/PLT/packages/doc/mzscheme/node22.htm>
The case-lambda form creates a procedure that dispatches to a
particular body of expressions based on the number of arguments
it receives. This provides a mechanism for creating variable-arity
procedures with more control and efficiency than using a ``rest arg''.
So using case-lambda, the above example [extended slightly] becomes:
(define foo
(case-lambda
((arg1) ...what to do if only one arg given...)
((arg1 arg2) ...what to do if exactly two args...)
((arg1 arg2 arg3 . the-remainder) ...three or more...)))
-Rob
-----
Rob Warnock, 8L-855 rpw3@sgi.com
Applied Networking http://reality.sgi.com/rpw3/
Silicon Graphics, Inc. Phone: 650-933-1673
1600 Amphitheatre Pkwy. FAX: 650-933-0511
Mountain View, CA 94043 PP-ASEL-IA