[plt-scheme] keyword arguments (in v4.0, maybe)

From: Richard Cobbe (cobbe at ccs.neu.edu)
Date: Tue Jun 12 21:13:56 EDT 2007

On Wed, Jun 13, 2007 at 07:34:50AM +0800, Matthew Flatt wrote:
> At Tue, 12 Jun 2007 18:23:19 -0500, "Robby Findler" wrote:
> > I guess that only having optional keyword arguments would be strange
> > (and would lead to endless "why don't you have ..." questions on
> > mailing lists like this one ;).
>
> I don't know. That might be a fine point in the design space.

That's how Common Lisp does it.

Given

      (defun f (&key a (b 10) (c 12 c-present-p))
        (list a b c c-present-p))

then F has 3 optional keyword args, A, B, and C.  A defaults to NIL, B
defaults to 10, and C defaults to 12.  Further, C-PRESENT-P is bound to T
within the body of F if the caller provided an explicit value for C and to
NIL otherwise (so if C is 12, you can tell where that 12 came from).

SBCL 1.0.5 gives the following behavior (reformatted slightly for
legibility):

    * (f)
    (NIL 10 12 NIL)

    * (f :a 3 :b 4)
    (3 4 12 NIL)

    * (f :a 3 :b 4 :c 12)
    (3 4 12 T)

    * (f :a 3 :b 4 :c 15)
    (3 4 15 T)

More details at <http://www.lisp.org/HyperSpec/Body/sec_3-4-1.html>.  You
can even override the default symbol that callers use in the argument
list, though I didn't demonstrate that above.

Richard


Posted on the users mailing list.