Next: Variable Scoping, Previous: Accessing Variables, Up: Variables

The usual way to change the value of a variable is with the special
form `setq`

. When you need to compute the choice of variable at
run time, use the function `set`

.

— Special Form: **setq** [`symbol form`]`...`

This special form is the most common method of changing a variable's value. Each

symbolis given a new value, which is the result of evaluating the correspondingform. The current binding of the symbol is changed.

`setq`

does not evaluatesymbol; it sets the symbol that you write. We say that this argument is automatically quoted. The ‘q’ in`setq`

stands for “quoted”.The value of the

`setq`

form is the value of the lastform.(setq x (1+ 2)) ⇒ 3 x ;`x`

now has a global value. ⇒ 3 (let ((x 5)) (setq x 6) ; The local binding of`x`

is set. x) ⇒ 6 x ; The global value is unchanged. ⇒ 3Note that the first

formis evaluated, then the firstsymbolis set, then the secondformis evaluated, then the secondsymbolis set, and so on:(setq x 10 ; Notice that`x`

is set before y (1+ x)) ; the value of`y`

is computed. ⇒ 11

— Function: **set**` symbol value`

This function puts

valuein the value cell ofsymbol. Since it is a function rather than a special form, the expression written forsymbolis evaluated to obtain the symbol to set. The return value isvalue.When dynamic variable binding is in effect (the default),

`set`

has the same effect as`setq`

, apart from the fact that`set`

evaluates itssymbolargument whereas`setq`

does not. But when a variable is lexically bound,`set`

affects itsdynamicvalue, whereas`setq`

affects its current (lexical) value. See Variable Scoping.(set one 1) error--> Symbol's value as variable is void: one (set 'one 1) ⇒ 1 (set 'two 'one) ⇒ one (set two 2) ;`two`

evaluates to symbol`one`

. ⇒ 2 one ; So it is`one`

that was set. ⇒ 2 (let ((one 1)) ; This binding of`one`

is set, (set 'one 3) ; not the global value. one) ⇒ 3 one ⇒ 2If

symbolis not actually a symbol, a`wrong-type-argument`

error is signaled.(set '(x y) 'z) error--> Wrong type argument: symbolp, (x y)