Common Lisp the Language, 2nd Edition


next up previous contents index
Next: Generalized Variables Up: Constants and Variables Previous: Reference

7.1.2. Assignment

The following facilities allow the value of a variable (more specifically, the value associated with the current binding of the variable) to be altered. Such alteration is different from establishing a new binding. Constructs for establishing new bindings of variables are described in section 7.5.


[Special Form]
setq {var form}*

The special form (setq var1 form1 var2 form2 ...) is the ``simple variable assignment statement'' of Lisp. First form1 is evaluated and the result is stored in the variable var1, then form2 is evaluated and the result stored in var2, and so forth. The variables are represented as symbols, of course, and are interpreted as referring to static or dynamic instances according to the usual rules. Therefore setq may be used for assignment of both lexical and special variables.

setq returns the last value assigned, that is, the result of the evaluation of its last argument. As a boundary case, the form (setq) is legal and returns nil. There must be an even number of argument forms. For example, in

(setq x (+ 3 2 1) y (cons x nil))

x is set to 6, y is set to (6), and the setq returns (6). Note that the first assignment is performed before the second form is evaluated, allowing that form to use the new value of x.

See also the description of setf, the Common Lisp ``general assignment statement'' that is capable of assigning to variables, array elements, and other locations.

change_begin
Some programmers choose to avoid setq as a matter of style, always using setf for any kind of structure modification. Others use setq with simple variable names and setf with all other generalized variables.

X3J13 voted in March 1989 (SYMBOL-MACROLET-SEMANTICS)   to specify that if any var refers not to an ordinary variable but to a binding made by symbol-macrolet, then that var is handled as if setf had been used instead of setq.
change_end


[Macro]
psetq {var form}*

A psetq form is just like a setq form, except that the assignments happen in parallel. First all of the forms are evaluated, and then the variables are set to the resulting values. The value of the psetq form is nil. For example:

(setq a 1) 
(setq b 2) 
(psetq a b  b a) 
a => 2 
b => 1

In this example, the values of a and b are exchanged by using parallel assignment. (If several variables are to be assigned in parallel in the context of a loop, the do construct may be appropriate.)

See also the description of psetf, the Common Lisp ``general parallel assignment statement'' that is capable of assigning to variables, array elements, and other locations.

change_begin
X3J13 voted in March 1989 (SYMBOL-MACROLET-SEMANTICS)   to specify that if any var refers not to an ordinary variable but to a binding made by symbol-macrolet, then that var is handled as if psetf had been used instead of psetq.
change_end


[Function]
set symbol value

set allows alteration of the value of a dynamic (special) variable. set causes the dynamic variable named by symbol to take on value as its value.

change_begin
X3J13 voted in January 1989 (ARGUMENTS-UNDERSPECIFIED)   to clarify that the value may be any Lisp datum whatsoever.
change_end

Only the value of the current dynamic binding is altered; if there are no bindings in effect, the most global value is altered. For example,

(set (if (eq a b) 'c 'd) 'foo)

will either set c to foo or set d to foo, depending on the outcome of the test (eq a b).

set returns value as its result.

set cannot alter the value of a local (lexically bound) variable. The special form setq is usually used for altering the values of variables (lexical or dynamic) in programs. set is particularly useful for implementing interpreters for languages embedded in Lisp. See also progv, a construct that performs binding rather than assignment of dynamic variables.


[Function]
makunbound symbol
fmakunbound symbol

makunbound causes the dynamic (special) variable named by symbol to become unbound (have no value). fmakunbound does the analogous thing for the global function definition named by symbol. For example:

(setq a 1) 
a => 1 
(makunbound 'a) 
a => causes an error 

(defun foo (x) (+ x 1)) 
(foo 4) => 5 
(fmakunbound 'foo) 
(foo 4) => causes an error

Both functions return symbol as the result value.

change_begin
X3J13 voted in March 1989 (FUNCTION-NAME)   to extend fmakunbound to accept any function-name (a symbol or a list whose car is setf - see section 7.1). Thus one may write (fmakunbound '(setf cadr)) to remove any global definition of a setf expansion function for cadr.
change_end



next up previous contents index
Next: Generalized Variables Up: Constants and Variables Previous: Reference


AI.Repository@cs.cmu.edu