How to specify optional and/or named arguments
Original Scheme's handling of variable number arguments via dotted-list in formals has its own beauty---it is naturally understood that argument binding in terms of (limited) pattern matching. And you can add optional/named arguments on top of that, by parsing 'rest' arguments by yourself (see let-optionals* and let-keywords*, for example). However, in production code where you write lots of functions with such arguments, it tends to be clumsy.
There are several extensions that allow you to directly express optional/named arguments in the 'formals' position of the lambda form.
Using lambda*, optional positional arguments, required named arguments and optional named arguments can be specified as followings:
It appears that there's no way to allow arbitrary keyword arguments while specifying some of them (a la &allow-other-keys semantics in CL).
Using lambda*, optional positional arguments and optional named arguments can be specified in CL way, except Guile uses #:optional, #:key, and #:rest to delimit these parameters in the formals.
It can also accept #:allow-other-keys.
The lambda syntax is extended to accept optional positional and named arguments in CL way, except using #!optional, #!key, and #!rest to delimit these parameters.
It doesn't appear to have &allow-other-keys equivalent.
The lambda syntax is extended to accept optional positional and named arguments in CL way, except using :optional, :key, and :rest to delimit these parameters.
In optional and named arguments you can specify guard predicate as well, in the form:
(<var> <expression> <guard>)
When the procedure is called, the value of the argument that is a candidate of matching this parameter <var> is first passed to <guard>, and bound to <var> only iff <guard> returns a true value; otherwise, the value of <expression> is used as if the argument is not supplied.
About This Site
Concept:CaseSensitivity Concept:DocumentationFormat Concept:ExtendedLambdaList Concept:FileSystem Concept:HashTable Concept:Module Concept:Networking Concept:ObjectSystem Concept:Process Concept:RegularExpression Concept:UserGroup