Previous Next Contents

3. Symbols

A symbol is just a string of characters. There are restrictions on what you can include in a symbol and what the first character can be, but as long as you stick to letters, digits, and hyphens, you'll be safe. (Except that if you use only digits and possibly an initial hyphen, LISP will think you typed an integer rather than a symbol.) Some examples of symbols:

  1. a
  2. b
  3. c1
  4. foo
  5. bar
  6. baaz-quux-garply

Some things you can do with symbols follow. (Things after a ``>'' prompt are what you type to the LISP interpreter, while other things are what the LISP interpreter prints back to you. The ``;'' is LISP's comment character: everything from a ``;'' to the end of line is ignored.)

> (setq a 5)            ;store a number as the value of a symbol
                        ;cmucl will print a warning, ignore it.
5
> a                     ;take the value of a symbol
5
> (let ((a 6)) a)       ;bind the value of a symbol temporarily to 6
6
> a                     ;the value returns to 5 once the let is finished
5
> (+ a 6)               ;use the value of a symbol as an argument to a function
11
> b                     ;try to take the value of a symbol which has no value
Error: Attempt to take the value of the unbound symbol B
                        ;or in CMUCL:
Error in KERNEL::UNBOUND-SYMBOL-ERROR-HANDLER:  the variable B is unbound.

Restarts:
  0: [ABORT] Return to Top-Level.

Debug  (type H for help)

(EVAL B)
0]
                        ; return to top-level by typing "0" or
                        ; "restart 0"               

There are two special symbols, t and nil. The value of t is defined always to be t, and the value of nil is defined always to be nil. LISP uses t and nil to represent true and false. An example of this use is in the if statement, described more fully later:

> (if t 5 6)
5
> (if nil 5 6)
6
> (if 4 5 6)
5

The last example is odd but correct: nil means false, and anything else means true. (Unless we have a reason to do otherwise, we use t to mean true, just for the sake of clarity.)

Symbols like t and nil are called self-evaluating symbols, because they evaluate to themselves. There is a whole class of self-evaluating symbols called keywords; any symbol whose name starts with a colon is a keyword. (See below for some uses for keywords.) Some examples:

> :this-is-a-keyword
:THIS-IS-A-KEYWORD
> :so-is-this
:SO-IS-THIS
> :me-too
:ME-TOO


Previous Next Contents