User Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cl:special_operators:setq [2017/05/01 21:00] (current)
Line 1: Line 1:
 +====== Special Operator SETQ ======
 +
 +====Syntax====
 +  * **setq** //​{pair}''​*''//​ → //result//
 +  ​
 +<​blockquote>​
 +//pair// ::= //var form//
 +</​blockquote>​
 +
 +====Arguments and Values====
 +  * //var// - a //​[[CL:​Glossary:​symbol]]//​ naming a //​[[CL:​Glossary:​variable]]//​ other than a //​[[CL:​Glossary:​constant variable]]//​.
 +  * //form// - a //​[[CL:​Glossary:​form]]//​.
 +  * //result// - the //​[[CL:​Glossary:​primary value]]// of the last //form//, or **[[CL:​Constant Variables:​nil]]** if no //pairs// were supplied.
 +
 +====Description====
 +Assigns values to //​[[CL:​Glossary:​variables]]//​.
 +
 +''​(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. **setq** may be used for assignment of both lexical and dynamic variables.
 +
 +If any //var// refers to a //​[[CL:​Glossary:​binding]]//​ made by **[[CL:​Special Operators:​symbol-macrolet]]**,​ then that //var// is treated as if **[[CL:​Macros:​setf]]** (not **setq**) had been used.
 +
 +====Examples====
 +<​blockquote>​
 +;;; TODO condemn toplevel SETQ
 +
 +;;; A simple use of SETQ to establish values for variables. ​
 +(setq a 1 b 2 c 3) <​r>​3</​r>​
 +a <r>1 </r>
 +b <​r>​2</​r>​
 +c <​r>​3</​r>​
 +
 +;;; Use of SETQ to update values by sequential assignment. ​
 +(setq a (1+ b) b (1+ a) c (+ a b)) <r>7 a → 3 b → 4 c → 7</r>
 +
 +;;; This illustrates the use of SETQ on a symbol macro. ​
 +([[CL:​Special Operators:​let]] ((x ([[CL:​Functions:​list]] 10 20 30)))
 +  ([[CL:​Special Operators:​symbol-macrolet]] ((y ([[CL:​Functions:​car]] x)) (z ([[CL:​Functions:​cadr]] x))) 
 +    (setq y ([[CL:​Functions:​math-one-plus|1+]] z) z ([[CL:​Functions:​math-one-plus|1+]] y))
 +    ([[CL:​Functions:​list]] x y z))) <​r>​((21 22 30) 21 22)</​r>​
 +</​blockquote>​
 +
 +====Side Effects====
 +The //​[[CL:​Glossary:​primary value]]// of each //form// is assigned to the corresponding //var//.
 +
 +====Affected By====
 +None.
 +
 +====Exceptional Situations====
 +None.
 +
 +====See Also====
 +**[[CL:​Macros:​psetq|Macro PSETQ]]**, **[[CL:​Functions:​set|Function SET]]**, **[[CL:​Macros:​setf|Macro SETF]]**
 +
 +====Notes====
 +None.
 +
 +\issue{SYMBOL-MACROLET-SEMANTICS:​SPECIAL-FORM}