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

Optionals, version 1



This should be added to section 4.1.4, "Lambda expressions".

[At the end of the list of forms that the <Formals> may take:]

Some implementations support three more alternatives for the <Formals>:

\item{$\bullet$}{
(<variable_1> ... <variable_k> #!optional <variable_{k+1}> ...):
The variables that precede the special #!optional flag are {\it required}
formal arguments, and the variables that follow the #!optional flag are
{\it optional} formal arguments.  There must be at least as many actual
arguments as there are required arguments, and there must be no more actual
arguments than there are required and optional arguments together.  The actual
arguments are matched against the formal arguments from left to right, and are
stored in the bindings of the corresponding variables.  If optional formal
arguments are left over after the actual arguments have been matched up against
the other formal arguments, then a special default object is stored in the
bindings of those left-over variables.  See the default-object? procedure.
}

\item{$\bullet$}{
(<variable_1> ... <variable_k> #!optional <variable_{k+1}> ...
<variable_{n-1}> . <variable_n>):
The #!optional flag may be used in combination with a space-delimited period
before the last variable.  In this case there must be at least as many actual
arguments as there are required arguments, but there is no upper limit on the
number of actual arguments.  If the number of actual arguments is less than n,
then an empty list will be stored in the binding of <variable_n>.  Otherwise
a newly allocated list of the actual arguments left over after all the other
actual arguments have been matched up against the other formal arguments will
be stored in the binding of <variable_n>.
}

\item{$\bullet$}{
A #!rest flag may be used in place of a space-delimited period before the
last variable.  The #!rest flag is equivalent to a space-delimited period
in such a context.
}

  (let ((f (lambda (x #!optional base)
             (expt (if (default-object? base) 2 base) x))))
    (list (f 10) (f 4 3)))                                    ==>  (1024 81)



[The following probably goes in section 6.9, "Control features".]

(default-object? obj)                                           procedure

Returns #t if obj is a default object such as is supplied for optional
arguments that have no corresponding actual argument.  (See lambda, section
4.1.4.)  Otherwise returns #f.

  ((lambda (#!optional x y z) (default-object? z)) 1 4)               ==>  #t
  ((lambda (#!optional x y z) (default-object? z)) 1 4 9)             ==>  #f