File: LISP-tutorial-3.html

package info (click to toggle)
cmucl 20c-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 42,524 kB
  • sloc: lisp: 358,331; ansic: 28,385; asm: 3,777; sh: 1,236; makefile: 366; csh: 31
file content (104 lines) | stat: -rw-r--r-- 3,525 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<HTML>
<HEAD>
<TITLE>Common LISP Hints: Symbols</TITLE>
</HEAD>
<BODY>
<A HREF="LISP-tutorial-2.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="LISP-tutorial-4.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="LISP-tutorial.html#toc3"><IMG SRC="toc.gif" ALT="Contents"></A>
<HR>
<H2><A NAME="s3">3. Symbols</A></H2>

<P>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:</P>
<P>
<OL>
<LI>    a</LI>
<LI>        b</LI>
<LI>        c1</LI>
<LI>        foo</LI>
<LI>        bar</LI>
<LI>        baaz-quux-garply</LI>
</OL>
</P>
<P>Some things you can do with symbols follow. (Things after a <CODE>``&gt;''</CODE> prompt
are what you type to the LISP interpreter, while other things are what
the LISP interpreter prints back to you. The <CODE>``;''</CODE> is LISP's comment
character: everything from a <CODE>``;''</CODE> to the end of line is ignored.)</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
&gt; (setq a 5)            ;store a number as the value of a symbol
                        ;cmucl will print a warning, ignore it.
5
&gt; a                     ;take the value of a symbol
5
&gt; (let ((a 6)) a)       ;bind the value of a symbol temporarily to 6
6
&gt; a                     ;the value returns to 5 once the let is finished
5
&gt; (+ a 6)               ;use the value of a symbol as an argument to a function
11
&gt; 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 &quot;0&quot; or
                        ; &quot;restart 0&quot;               
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>There are two special symbols, <EM>t</EM> and <EM>nil</EM>. The value of <CODE>t</CODE> is defined
always to be <CODE>t</CODE>, and the value of <CODE>nil</CODE> is defined always to be <CODE>nil</CODE>. LISP
uses <CODE>t</CODE> and <CODE>nil</CODE> to represent true and false. An example of this use is
in the if statement, described more fully later:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
&gt; (if t 5 6)
5
&gt; (if nil 5 6)
6
&gt; (if 4 5 6)
5
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>The last example is odd but correct: <CODE>nil</CODE> means false, and anything else
means true. (Unless we have a reason to do otherwise, we use <CODE>t</CODE> to mean
true, just for the sake of clarity.)</P>
<P>Symbols like <CODE>t</CODE> and <CODE>nil</CODE> 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:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
&gt; :this-is-a-keyword
:THIS-IS-A-KEYWORD
&gt; :so-is-this
:SO-IS-THIS
&gt; :me-too
:ME-TOO
</PRE>
</CODE></BLOCKQUOTE>
</P>

<HR>
<A HREF="LISP-tutorial-2.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="LISP-tutorial-4.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="LISP-tutorial.html#toc3"><IMG SRC="toc.gif" ALT="Contents"></A>
</BODY>
</HTML>