Next: Function Documentation Prev: Simple Lambda Up: Lambda Expressions
Advanced Features of Argument Lists
Our simple sample function, `(lambda (a b c) (+ a b c))', specifies
three argument variables, so it must be called with three arguments: if
you try to call it with only two arguments or four arguments, you get a
It is often convenient to write a function that allows certain
arguments to be omitted. For example, the function `substring' accepts
three arguments--a string, the start index and the end index--but the
third argument defaults to the end of the string if you omit it. It is
also convenient for certain functions to accept an indefinite number of
arguments, as the functions `and' and `+' do.
To specify optional arguments that may be omitted when a function is
called, simply include the keyword `&optional' before the optional
arguments. To specify a list of zero or more extra arguments, include
the keyword `&rest' before one final argument.
Thus, the complete syntax for an argument list is as follows:
The square brackets indicate that the `&optional' and `&rest' clauses,
and the variables that follow them, are optional.
A call to the function requires one actual argument for each of the
REQUIRED-VARS. There may be actual arguments for zero or more of the
OPTIONAL-VARS, and there cannot be any more actual arguments than these
unless `&rest' exists. In that case, there may be any number of extra
If actual arguments for the optional and rest variables are omitted,
then they always default to `nil'. However, the body of the function
is free to consider `nil' an abbreviation for some other meaningful
value. This is what `substring' does; `nil' as the third argument
means to use the length of the string supplied. There is no way for the
function to distinguish between an explicit argument of `nil' and an
Common Lisp note: Common Lisp allows the function to specify what
default value to use when an optional argument is omitted; GNU
Emacs Lisp always uses `nil'.
For example, an argument list that looks like this:
(a b &optional c d &rest e)
binds `a' and `b' to the first two actual arguments, which are
required. If one or two more arguments are provided, `c' and `d' are
bound to them respectively; any arguments after the first four are
collected into a list and `e' is bound to that list. If there are only
two arguments, `c' is `nil'; if two or three arguments, `d' is `nil';
if four arguments or fewer, `e' is `nil'.
There is no way to have required arguments following optional
ones--it would not make sense. To see why this must be so, suppose
that `c' in the example were optional and `d' were required. If three
actual arguments are given; then which variable would the third
argument be for? Similarly, it makes no sense to have any more
arguments (either required or optional) after a `&rest' argument.
Here are some examples of argument lists and proper calls:
((lambda (n) (1+ n)) ; One required:
1) ; requires exactly one argument.
((lambda (n &optional n1) ; One required and one optional:
(if n1 (+ n n1) (1+ n))) ; 1 or 2 arguments.
((lambda (n &rest ns) ; One required and one rest:
(+ n (apply '+ ns))) ; 1 or more arguments.
1 2 3 4 5)
automatically generated by info2www