Node:Using setq, Next:, Previous:Using set, Up:set & setq



1.9.2 Using setq

As a practical matter, you almost always quote the first argument to set. The combination of set and a quoted first argument is so common that it has its own name: the special form setq. This special form is just like set except that the first argument is quoted automatically, so you don't need to type the quote mark yourself. Also, as an added convenience, setq permits you to set several different variables to different values, all in one expression.

To set the value of the variable carnivores to the list '(lion tiger leopard) using setq, the following expression is used:

(setq carnivores '(lion tiger leopard))

This is exactly the same as using set except the first argument is automatically quoted by setq. (The q in setq means quote.)

With set, the expression would look like this:

(set 'carnivores '(lion tiger leopard))

Also, setq can be used to assign different values to different variables. The first argument is bound to the value of the second argument, the third argument is bound to the value of the fourth argument, and so on. For example, you could use the following to assign a list of trees to the symbol trees and a list of herbivores to the symbol herbivores:

(setq trees '(pine fir oak maple)
      herbivores '(gazelle antelope zebra))

(The expression could just as well have been on one line, but it might not have fit on a page; and humans find it easier to read nicely formatted lists.)

Although I have been using the term `assign', there is another way of thinking about the workings of set and setq; and that is to say that set and setq make the symbol point to the list. This latter way of thinking is very common and in forthcoming chapters we shall come upon at least one symbol that has `pointer' as part of its name. The name is chosen because the symbol has a value, specifically a list, attached to it; or, expressed another way, the symbol is set to "point" to the list.