|
Allegro CL |
ANSI Common Lisp ![]() ![]() ![]() 3.4.1.6 Examples of Ordinary Lambda ListsHere are some examples involving optional parameters and rest parameters:
((lambda (a b) (+ a (* b 3))) 4 5) Here are some examples involving keyword parameters:
((lambda (a b &key c d) (list a b c d)) 1 2) Here are some examples involving optional parameters, rest parameters, and keyword parameters together:
((lambda (a &optional (b 3) &rest x &key c (d a)) (list a b c d x)) 1) As an example of the use of &allow-other-keys and :allow-other-keys, consider a function that takes two named arguments of its own and also accepts additional named arguments to be passed to make-array:
(defun array-of-strings (str dims &rest named-pairs &key (start 0) end &allow-other-keys) (apply #'make-array dims :initial-element (subseq str start end) :allow-other-keys t named-pairs)) This function takes a string and dimensioning information and returns an array of the specified dimensions, each of whose elements is the specified string. However, :start and :end named arguments may be used to specify that a substring of the given string should be used. In addition, the presence of &allow-other-keys in the lambda list indicates that the caller may supply additional named arguments; the rest parameter provides access to them. These additional named arguments are passed to make-array. The function make-array normally does not allow the named arguments :start and :end to be used, and an error should be signaled if such named arguments are supplied to make-array. However, the presence in the call to make-array of the named argument :allow-other-keys with a true value causes any extraneous named arguments, including :start and :end, to be acceptable and ignored. |