1.9.1 Using setq

To set the value of the symbol flowers to the list (rose violet daisy buttercup), evaluate the following expression by positioning the cursor after the expression and typing C-x C-e.

(setq flowers '(rose violet daisy buttercup))

The list (rose violet daisy buttercup) will appear in the echo area. This is what is returned by the setq special form. As a side effect, the symbol flowers is bound to the list; that is, the symbol flowers, which can be viewed as a variable, is given the list as its value. (This process, by the way, illustrates how a side effect to the Lisp interpreter, setting the value, can be the primary effect that we humans are interested in. This is because every Lisp function must return a value if it does not get an error, but it will only have a side effect if it is designed to have one.)

After evaluating the setq expression, you can evaluate the symbol flowers and it will return the value you just set. Here is the symbol. Place your cursor after it and type C-x C-e.

flowers

When you evaluate flowers, the list (rose violet daisy buttercup) appears in the echo area.

Incidentally, if you evaluate 'flowers, the variable with a quote in front of it, what you will see in the echo area is the symbol itself, flowers. Here is the quoted symbol, so you can try this:

'flowers

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))

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 setq; and that is to say that setq makes 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.