Next: , Previous: , Up: Welcome to Lisp   [Contents][Index]


16.4.11 Assignment

In Common Lisp, the most general assignment operator is setf. We can use it to do assigments to either kind of variable:

> (setf *glob* 98)
98
> (let ((n 10))
    (setf n 2)
    n)
2

When the first argument to setf is a symbol tht is not the name of a local variable, it is taken to be a global variable:

> (setf x (list ’a ’b ’c))
(A B C)

That is, you can create global variables implicitly, just by assigning them values. In source files, it is better style to use explicit defparameters.

You can do more than just assign values to variables. The first argument to setf can be an expression as well as a variable name. In such cases, the value of the second argument is inserted in the place referred to be the first:

> (setf (car x) ’n)
N
> x
(N B C)

The first argument to setf can be almost any expression that refers to a particular place. All such operators are marked as “settable” in Appendix D.

You can give any (even) number of arguments to setf. An expression of the form:

(setf a b
      c d
      e f)

is equivalent to three separate calls to setf in sequence.