chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Is there a way I'm missing to use optional parameter


From: felix winkelmann
Subject: Re: [Chicken-users] Is there a way I'm missing to use optional parameters?
Date: Tue, 5 Apr 2005 08:48:52 +0200

On Apr 5, 2005 8:08 AM, Reed Sheridan <address@hidden> wrote:
> I'm moving from Common Lisp and the one thing I really miss so far is
> optional and/or keyword parameters, which are very helpful when you
> have no idea what you're doing or which parameters you'll really need.
>  I see that there's a way to have just one optional argument, and the
> horrific case-lambda construct is available, but is there some scheme
> way to do the job that keyword and optional args do in CL that I'm
> missing?
> 

There is no real portable way in Scheme to do this, but many Schemes
(including Chcken, Bigloo and Gambit) allow DSSSL style keywords,
which are basically the same as in CL:

(define (foo #!optional (x 42) y #!key (abc 'def))
  (list x y abc) )

(foo)  ==>  (42 #f def)
(foo 1) ==> (1 #f def)
(foo 1 2 abc: 99) ==> (1 2 99)

There are also some macros, which are better than case-lambda,
originally by Olin Shivers. A portable implementation of one of them
looks like this:

(define-syntax let-optionals*
  (syntax-rules ()
    [(_ rest () body ...) (let () body ...)]
    [(_ rest ((var default) . more) body ...)
     (let* ((tmp rest)
            (var (if (null? tmp) default (car tmp)))
            (rest2 (if (null? tmp) '() (cdr tmp))) )
       (let-optionals* rest2 more body ...) ) ]
    [(_ rest (var) body ...) (let ((var rest)) body ...)] ) )


cheers,
felix




reply via email to

[Prev in Thread] Current Thread [Next in Thread]