[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
case-lambda syntax in Chez Scheme
-
Some good person replied to my example in Re: Limitation with Lambda.
This person pointed out that the "case-lambda" syntax is not standard
Scheme. My apologies, case-lambda is from Chez Scheme Version 2.0,
and I have found it so useful (I use it a *lot*) that I just forgot
that users of other Scheme systems might not know what it meant.
An excerpt from the Chez Scheme Version 2.0 release notes follows. It's
a simple idea that not only makes code easier to write and to understand,
but it also runs faster than the old way. Maybe Kent Dybvig could
be encouraged to post a little more info about case-lambda?
-- Brad Pierce
------------------------------------------------------------------------
(below Copyright Kent Dybvig)
Optional Arguments
Procedures with a variable number of arguments could only be defined
in earlier versions using the "lambda dot" interface, which requires
allocation of a list to hold all but the required parameters.
Version 2.0 supports a variant of "lambda", called "case-lambda",
that allows procedures with variable numbers of arguments to be
defined efficiently. "case-lambda" has the form:
(case-lambda [idspec-a exp1-a exp2-a ...]
[idspec-b exp1-b exp2-b ...] ...)
where the idspecs are normal "lambda" parameter lists. In essence,
each bracketted item (parens may be used in place of the brackets)
represents a different "lambda" expression; which one is evaluated
depends upon the number of arguments. If the number of arguments is
a correct number for the first idspec, evaluation proceeds with the
first body within the bindings implied by the first idspec. If not,
then if the number of arguments is a correct number for the second
idspec, evaluation proceeds with the second body, etc.