Assignment and blocks

Assignment in Lisp is done using the special operator setq and sequencing is specified by the special operator progn.

setq

A new value can be assigned to an identifier by the special operator called setq, which looks like this:

(setq identifer expression)

The expression is evaluated and identifier is updated with the result. The result of the setq expression is the value of expression. Here are some examples:

user> (setq ticks-per-second 4) 4 user> ticks-per-second 4 user> (setq nil 3) Error---calling default handler: Condition class is # message: "trying to set a constant symbol" value: nil Debug loop. Type help: for help Broken at # DEBUG> top: In the first example, we have changed the value of ticks-per-second to 4 ... not a good idea in general! We can also try to update nil, however, this is not permitted.

progn

If you are writing a program that depends on operations being done in a particular order, then you need to sequence them explicitly. The special operator in Lisp is called progn and is equivalent to Pascal's begin ... end or C's { ... }. It looks like this:

(progn exp-1 ... exp-n)

Each of the expressions exp-1 to exp-n is evaluated in order. The result of progn is exp-n.

The most common occurrence of progn is in the consequent or alternative branches of an if expression. Note that it is not needed in cond expressions, since the consequent in each case, can be a sequence of expressions.


Julian Padget, jap@maths.bath.ac.uk, this version January 12, 1995