132 lines
4.5 KiB
HTML
132 lines
4.5 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
|
||
|
<!Originally converted to HTML using LaTeX2HTML 95 (Thu Jan 19 1995) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
|
||
|
<HEAD>
|
||
|
<TITLE>Atoms</TITLE>
|
||
|
</HEAD>
|
||
|
<BODY>
|
||
|
<meta name="description" value="Atoms">
|
||
|
<meta name="keywords" value="lp">
|
||
|
<meta name="resource-type" value="document">
|
||
|
<meta name="distribution" value="global">
|
||
|
<P>
|
||
|
<BR> <HR>
|
||
|
<A HREF="node6.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
|
||
|
<A HREF="node4.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
|
||
|
<A HREF="node4.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <BR>
|
||
|
<A HREF="lp.html"><B>Contents</B></A>
|
||
|
<B> Next:</B>
|
||
|
<A HREF="node6.html"> Lists</A>
|
||
|
<B>Up:</B>
|
||
|
<A HREF="node4.html"> Basic Data Types</A>
|
||
|
<B> Previous:</B>
|
||
|
<A HREF="node4.html"> Basic Data Types</A>
|
||
|
<BR> <HR> <P>
|
||
|
<H2> Atoms</H2>
|
||
|
<P>
|
||
|
The first rule of evaluation is that for any atom the evaluator, known as ``eval,'' attempts to find a value for that atom.
|
||
|
<P>
|
||
|
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:
|
||
|
<BLOCKQUOTE>
|
||
|
<PRE>> (setq my-age 9) ; you assign 9 to the atom my-age
|
||
|
9 ; interpreter responds with value
|
||
|
</PRE>
|
||
|
</BLOCKQUOTE>
|
||
|
Now, you may test what you have done by giving the atom to the
|
||
|
interpreter.
|
||
|
<BLOCKQUOTE>
|
||
|
<PRE>> my-age ; you tell interpreter to eval my-age
|
||
|
9 ; it responds with the set value
|
||
|
</PRE>
|
||
|
</BLOCKQUOTE>
|
||
|
If a birthday has just passed, you can change the value of my-age as
|
||
|
follows:
|
||
|
<BLOCKQUOTE>
|
||
|
<PRE>> (setq my-age 10)
|
||
|
10
|
||
|
> my-age
|
||
|
10
|
||
|
</PRE>
|
||
|
</BLOCKQUOTE>
|
||
|
<P>
|
||
|
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.
|
||
|
<P>
|
||
|
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.
|
||
|
<P>
|
||
|
Try the following sequence:
|
||
|
<P>
|
||
|
<BLOCKQUOTE>
|
||
|
<PRE>> 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.
|
||
|
</PRE>
|
||
|
</BLOCKQUOTE>
|
||
|
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).
|
||
|
<P>
|
||
|
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.
|
||
|
<P>
|
||
|
Notice that it is an error to attempt to set a value for special
|
||
|
atoms: numbers, t, or nil.
|
||
|
<P>
|
||
|
<BLOCKQUOTE>
|
||
|
<PRE>> (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.
|
||
|
</PRE>
|
||
|
</BLOCKQUOTE>
|
||
|
<P>
|
||
|
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.
|
||
|
<P>
|
||
|
<BR> <HR>
|
||
|
<A HREF="node6.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
|
||
|
<A HREF="node4.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
|
||
|
<A HREF="node4.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <BR>
|
||
|
<A HREF="lp.html"><B>Contents</B></A>
|
||
|
<B> Next:</B>
|
||
|
<A HREF="node6.html"> Lists</A>
|
||
|
<B>Up:</B>
|
||
|
<A HREF="node4.html"> Basic Data Types</A>
|
||
|
<B> Previous:</B>
|
||
|
<A HREF="node4.html"> Basic Data Types</A>
|
||
|
<BR> <HR> <P>
|
||
|
<BR> <HR>
|
||
|
<P>
|
||
|
<ADDRESS>
|
||
|
<I>© Colin Allen & Maneesh Dhagat <BR>
|
||
|
March 2007 </I>
|
||
|
</ADDRESS>
|
||
|
</BODY>
|