common-lisp Functions Auxiliary Variables


Example

The &AUX keyword can be used to define local variables for the function. They are not parameters; the user cannot supply them.

&AUX variables are seldomly used. You can always use LET instead, or some other way of defining local variables in the function body.

&AUX variables have the advantages that local variables of the whole function body move to the top and it makes one indentation level (for example introduced by a LET) unnecessary.

(defun foobar (x y &aux (z (+ x y)))
  (format t "X (~d) and Y (~d) are required.~@
             Their sum is ~d."
          x y z))

(foobar 10 20)
; X (10) and Y (20) are required.
; Their sum is 30.
;=> NIL

One typical usage may be resolving "designator" parameters. Again, you need not do it this way; using let is just as idiomatic.

(defun foo (a b &aux (as (string a)))
  "Combines A and B in a funny way.  A is a string designator, B a string."
  (concatenate 'string as " is funnier than " b))