A single rest-parameter can be given with the keyword &REST
after the required arguments. If such a parameter exists, the function can take a number of arguments, which will be grouped into a list in the rest-parameter. Note that the variable CALL-ARGUMENTS-LIMIT
determines the maximum number of arguments which can be used in a function call, thus the number of arguments is limited to an implementation specific value of minimum 50 or more arguments.
(defun foobar (x y &rest rest)
(format t "X (~s) and Y (~s) are required.~@
The function was also given following arguments: ~s~%"
x y rest))
(foobar 10 20)
; X (10) and Y (20) are required.
; The function was also given following arguments: NIL
;=> NIL
(foobar 10 20 30 40 50 60 70 80)
; X (10) and Y (20) are required.
; The function was also given following arguments: (30 40 50 60 70 80)
;=> NIL
The rest-parameter may be before keyword parameters. In that case it will contain the property list given by the user. The keyword values will still be bound to the corresponding keyword parameter.
(defun foobar (x y &rest rest &key (z 10 zp))
(format t "X (~s) and Y (~s) are required.~@
Z (~s) is a keyword argument. It ~:[wasn't~;was~] given.~@
The function was also given following arguments: ~s~%"
x y z zp rest))
(foobar 10 20)
; X (10) and Y (20) are required.
; Z (10) is a keyword argument. It wasn't given.
; The function was also given following arguments: NIL
;=> NIL
(foobar 10 20 :z 30)
; X (10) and Y (20) are required.
; Z (30) is a keyword argument. It was given.
; The function was also given following arguments: (:Z 30)
;=> NIL
Keyword &ALLOW-OTHER-KEYS
can be added at the end of the lambda-list to allow the user to give keyword arguments not defined as parameters. They will go in the rest-list.
(defun foobar (x y &rest rest &key (z 10 zp) &allow-other-keys)
(format t "X (~s) and Y (~s) are required.~@
Z (~s) is a keyword argument. It ~:[wasn't~;was~] given.~@
The function was also given following arguments: ~s~%"
x y z zp rest))
(foobar 10 20 :z 30 :q 40)
; X (10) and Y (20) are required.
; Z (30) is a keyword argument. It was given.
; The function was also given following arguments: (:Z 30 :Q 40)
;=> NIL