next up previous
Contents Next: Lists Up: Basic Data Types Previous: Basic Data Types

Atoms

The first rule of evaluation is that for any atom the evaluator, known as ``eval,'' attempts to find a value for that atom.

For most atoms, eval will return an error unless you have previously assigned a value to it. To assign a value to an atom use setq (or you can use its more sophisticated cousin, setf; more on setf in later chapters). So, for instance, to assign the value 9 to the atom ``my-age'' type the following to the interpreter:

> (setq my-age 9)       ; you assign 9 to the atom my-age
9                      ; interpreter responds with value
Now, you may test what you have done by giving the atom to the interpreter.
> my-age                ; you tell interpreter to eval my-age
9                      ; it responds with the set value
If a birthday has just passed, you can change the value of my-age as follows:
> (setq my-age 10)
10
> my-age
10

9, 10, 1.234 and all the other numbers are special atoms in Lisp -- they are pre-defined to evaluate to themselves. So, you may give any number to the interpreter, and it will respond by repeating the number.

In addition to numbers, two other special atoms are predefined, t and nil (think of them as true and false respectively). The interpreter considers nil to be identical to the empty list. Typing () directly to the interpreter will cause it to respond with nil.

Try the following sequence:

> 9
9
> 10
10
> my-age
10
> t       
T
> nil
NIL
> ()
NIL
> your-age
Error: The variable YOUR-AGE is unbound.
Error signalled by EVAL.
The last item illustrates what happens if you try to evaluate an atom that has not been set to a value. (The exact error message will vary between versions of Lisp, but all will say something about an unbound variable).

Most Lisp systems throw you into a debugger mode when an error occurs. From the debugger you can find out lots of useful things about the state of the interpreter when the problem occurred. Unfortunately, Lisp debuggers are not at all standardized so it is impossible to give a description here. Even in debugger mode, although the prompt usually is different, the Lisp interpreter continues to evaluate Lisp expressions normally. So we will ignore what is going on when an error occurs and assume that you can just carry on giving expressions to the interpreter for evaluation.

Notice that it is an error to attempt to set a value for special atoms: numbers, t, or nil.

> (setq 1 2)
Error: 1 is not a symbol.
Error signalled by SETQ.
> (setq t nil)
Error: Cannot assign to the constant T.
Error signalled by SETQ.

From these error messages you can see that the interpreter distinguishes between symbols, numbers, constants. Numbers and symbols are mutually exclusive subcategories of atoms. Constants (such as t and nil) are a subcategory of symbol. Only symbols which are not constants may be assigned a value with setq.



next up previous
Contents Next: Lists Up: Basic Data Types Previous: Basic Data Types



© Colin Allen & Maneesh Dhagat
March 2007