Next: , Previous: , Up: Getting Started   [Index]


1.5 Symbols

We have not seen any way to store the results of computations. We need something to fill the role played by variables in other programming languages. LISP provides a similar service through the use symbols. Symbols look like variables, although symbols have other uses as well. Just as one can assign a value toa variable in most programming languages, one can assign a value to a symbol in LISP.

Most programming languages provide a special notation for assignment, e.g., an assignment arrow or an equal sign. However, LISP does not such thing. Instead, LISP uses its routine workhorse, the function. LISP has a special function which assigns the value of one of its arguments to the other argument. This function goes under the rather peculiar name of ‘setq’. To assign ‘5’ to ‘x’ in LISP, we do the following:

-> (setq x 5)
5
->

We can now query LISP about the value of ‘x’:

-> x
5
->

The use of ‘setq’ has caused LISP to remember a value for ‘x’.

Another way of saying this is that symbols are also s-expressions that LISP tries to evaluate when we type them in. The value of a symbol in LISP is the value assigned to that symbol by some previous assignment operation.

We can use a symbol wherever we expect an s-expression to appear. Note that useing a symbol as an argument to a function does not ordinarily change its value, unless that function is something specially designed for that purpose, like ‘setq’.

What would the value of a symbol be if it were not explicitly assigned a value by a call to a previous assignment function? Attempting to evaluate the symbol ‘money’ caused an error. In particular, LISP complained that ‘money’ is an unassigned symbol, i.e., that it was never assigned a value. LISP considers this to be an error.


Next: Leaving LISP, Previous: Arguments to Functions, Up: Getting Started   [Index]