Node:lambda* Reference, Next:define* Reference, Previous:let-keywords Reference, Up:Optional Arguments
When using optional and keyword argument lists, using lambda
for
creating procedures and using let-optional
or let-keywords
is a bit lengthy. Therefore, lambda*
is provided, which combines
the features of those macros into a single convenient syntax.
For quick reference, here is the syntax of the formal argument list for
lambda*
(brackets are used to indicate grouping only):
ext-param-list ::= [identifier]* [#:optional [ext-var-decl]+]? [#:key [ext-var-decl]+ [#:allow-other-keys]?]? [[#:rest identifier]|[. identifier]]? ext-var-decl ::= identifier | ( identifier expression )
The characters `*', `+' and `?' are not to be taken literally; they mean respectively, zero or more occurrences, one or more occurrences, and one or zero occurrences.
lambda* formals body | library syntax |
lambda* creates a procedure that takes optional arguments. These
are specified by putting them inside brackets at the end of the
parameter list, but before any dotted rest argument. For example,
(lambda* (a b #:optional c d . e) '()) creates a procedure with fixed arguments a and b, optional
arguments c and d, and rest argument e. If the
optional arguments are omitted in a call, the variables for them are
bound to
(lambda* (#:key xyzzy larch) '()) can be called with any of the argument lists Optional and keyword arguments can also be given default values
which they take on when they are not present in a call, by giving a
two-item list in place of an optional argument, for example in:
(lambda* (foo #:optional (bar 42) #:key (baz 73)) (list foo bar baz)) foo is a fixed argument, bar is an optional argument with default value 42, and baz is a keyword argument with default value 73. Default value expressions are not evaluated unless they are needed and until the procedure is called.
((lambda* (#:key (heads 0) (tails 0)) (display (list heads tails))) #:heads 37 #:tails 42 #:heads 99) would result in (99 47) being displayed.
|