1
0
Fork 0
cl-sites/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node27.html
2023-10-25 11:23:21 +02:00

164 lines
9 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE>2.3. Symbols</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Symbols">
<meta name="keywords" value="clm">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<b>Common Lisp the Language, 2nd Edition</b>
<BR> <HR><A NAME=tex2html1842 HREF="node28.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html1840 HREF="node15.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html1834 HREF="node26.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html1844 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html1845 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html1843 HREF="node28.html"> Lists and Conses</A>
<B>Up:</B> <A NAME=tex2html1841 HREF="node15.html"> Data Types</A>
<B> Previous:</B> <A NAME=tex2html1835 HREF="node26.html"> String Characters</A>
<HR> <P>
<H1><A NAME=SECTION00630000000000000000>2.3. Symbols</A></H1>
<P>
Symbols are Lisp data objects that serve several purposes
and have several interesting characteristics. Every object of
type <tt>symbol</tt> has a name,
called its <i>print name</i>. Given a symbol, one can
obtain its name in the form of a string. Conversely,
given the name of a symbol as a string, one can obtain the
symbol itself. (More precisely, symbols are organized into
<i>packages</i>, and all the symbols in a package are uniquely
identified by name. See chapter <A HREF="node111.html#XPACK">11</A>.)
<P>
Symbols have a component called the <i>property list</i>, or <i>plist</i>.
By convention this is always a list whose even-numbered
components (calling the first component zero) are symbols,
here functioning as property names, and whose odd-numbered components
are associated property values. Functions are provided for manipulating
this property list; in effect, these allow a symbol to be treated as an
extensible record structure.
<P>
Symbols are also used to represent certain kinds of variables in Lisp
programs, and there are functions for dealing with the values associated
with symbols in this role.
<P>
A symbol can be notated simply by writing its name.
If its name is not empty, and if the name consists only of
uppercase alphabetic, numeric, or certain pseudo-alphabetic
special characters (but not
delimiter characters such as parentheses or space), and if
the name of the symbol cannot be mistaken for a number, then
the symbol can be notated by the sequence of characters in its name.
Any uppercase letters that appear in the (internal) name may
be written in either case in the external notation (more on this below).
For example:
<P><pre>
FROBBOZ ;The symbol whose name is <tt>FROBBOZ</tt>
frobboz ;Another way to notate the same symbol
fRObBoz ;Yet another way to notate it
unwind-protect ;A symbol with a <tt>-</tt> in its name
+$ ;The symbol named <tt>+$</tt>
1+ ;The symbol named <tt>1+</tt>
+1 ;This is the integer 1, not a symbol
pascal_style ;This symbol has an underscore in its name
b^2-4*a*c ;This is a single symbol!
; It has several special characters in its name
file.rel.43 ;This symbol has periods in its name
/usr/games/zork ;This symbol has slashes in its name
</pre><P>
In addition to letters and numbers, the following characters are normally
considered to be alphabetic for the purposes of notating
symbols:
<P><pre>
+ - * / @ $ % ^ &amp; _ = &lt; &gt; ~ .
</pre><P>
Some of these characters have conventional purposes for naming things;
for example, symbols that name special variables
generally have names beginning and ending with
<tt>*</tt>. The last character listed above, the period, is considered alphabetic
<i>provided</i> that a token does not consist entirely of periods.
A single period standing by itself is used in the notation
of conses and dotted lists; a token consisting of two or more periods
is syntactically illegal. (The period also serves as the decimal point
in the notation of numbers.)
<P>
The following characters are also alphabetic by default but are explicitly
reserved to the user for definition as reader macro characters
(see section <A HREF="node190.html#MACROCHARACTERSSECTION">22.1.3</A>) or any other desired purpose
and therefore should not be used routinely in names of symbols:
<P><pre>
? ! [ ] { }
</pre><P>
<P>
A symbol may have uppercase letters, lowercase letters, or both
in its print name.
However, the Lisp reader normally converts lowercase letters to
the corresponding uppercase letters when reading symbols.
The net effect is that most of the time case makes no
difference when <i>notating</i> symbols. Case <i>does</i> make
a difference internally and when printing a symbol.
Internally the symbols that name all standard Common Lisp functions,
variables, and keywords have uppercase names; their names appear
in lowercase in this book for readability. Typing such names
with lowercase letters works because the function <tt>read</tt> will convert
lowercase letters to the equivalent uppercase letters.
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in June 1989 (READ-CASE-SENSITIVITY) <A NAME=1080>&#160;</A> to introduce
<tt>readtable-case</tt>, which controls whether <tt>read</tt> will alter the case
of letters read as part of the name of a symbol.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
If a symbol cannot be simply notated by the characters of its name
because the (internal) name contains special characters or lowercase letters,
then there are two ``escape'' conventions for notating them.
Writing a <tt> </tt> character before any character causes the character
to be treated itself as an ordinary character for use in a symbol name;
in particular, it suppresses internal conversion of lowercase letters
to their uppercase equivalents.
If any character in a notation is preceded by <tt> </tt>, then that
notation can never be interpreted as a number.
For example:
<P><pre>
\( ;The symbol whose name is <tt>(</tt>
\+1 ;The symbol whose name is <tt>+1</tt>
+\1 ;Also the symbol whose name is <tt>+1</tt>
\frobboz ;The symbol whose name is <tt>fROBBOZ</tt>
3.14159265\s0 ;The symbol whose name is <tt>3.14159265s0</tt>
3.14159265\S0 ;A different symbol, whose name is <tt>3.14159265S0</tt>
3.14159265s0 ;A short-format floating-point approximation to <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap40211.gif">
APL\\360 ;The symbol whose name is <tt>APL 360</tt>
apl\\360 ;Also the symbol whose name is <tt>APL 360</tt>
\(b^2\)\ -\ 4*a*c ;The name is <tt>(B^2) - 4*A*C</tt>;
; it has parentheses and two spaces in it
\(\b^2\)\ -\ 4*\a*\c ;The name is <tt>(b^2) - 4*a*c</tt>;
; the letters are explicitly lowercase
</pre><P>
It may be tedious to insert a <tt>\</tt> before <i>every</i> delimiter
character in the name of a symbol if there are many of them.
An alternative convention is to surround the name of a symbol
with vertical bars; these cause every character between them to
be taken as part of the symbol's name, as if <tt>\</tt> had been written
before each one, excepting only
<tt>|</tt> itself and <tt>\</tt>, which must nevertheless be preceded by <tt>\</tt>.
For example:
<P><pre>
|&quot;| ;The same as writing <tt>\&quot;</tt>
|(b^2) - 4*a*c| ;The name is <tt>(b^2) - 4*a*c</tt>
|frobboz| ;The name is <tt>frobboz</tt>, not <tt>FROBBOZ</tt>
|APL\360| ;The name is <tt>APL360</tt>, because the <tt>\</tt> quotes the <tt>3</tt>
|APL\\360| ;The name is <tt>APL\360</tt>
|apl\\360| ;The name is <tt>apl\360</tt>
|\|\|| ;Same as <tt>\|\|</tt>: the name is <tt>||</tt>
|(B^2) - 4*A*C| ;The name is <tt>(B^2) - 4*A*C</tt>;
; it has parentheses and two spaces in it
|(b^2) - 4*a*c| ;The name is <tt>(b^2) - 4*a*c</tt>
</pre>
<P>
<BR> <HR><A NAME=tex2html1842 HREF="node28.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html1840 HREF="node15.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html1834 HREF="node26.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html1844 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html1845 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html1843 HREF="node28.html"> Lists and Conses</A>
<B>Up:</B> <A NAME=tex2html1841 HREF="node15.html"> Data Types</A>
<B> Previous:</B> <A NAME=tex2html1835 HREF="node26.html"> String Characters</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>