1
0
Fork 0
cl-sites/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node15.html

281 lines
14 KiB
HTML
Raw Normal View History

2023-10-25 11:23:21 +02:00
<!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. Data Types</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Data Types">
<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=tex2html1666 HREF="node16.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html1664 HREF="clm.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html1658 HREF="node14.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html1668 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html1669 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html1667 HREF="node16.html"> Numbers</A>
<B>Up:</B> <A NAME=tex2html1665 HREF="clm.html">Common Lisp the Language</A>
<B> Previous:</B> <A NAME=tex2html1659 HREF="node14.html"> Overview of Syntax</A>
<HR> <P>
<H1><A NAME=SECTION00600000000000000000>2. Data Types</A></H1>
<P>
<A NAME=DTYPES>Common Lisp</A> provides a variety of types of data objects. It is important to
note that in Lisp it is data objects that are typed, not variables.
Any variable can have any Lisp object as its value.
(It is possible to make an explicit declaration that a variable will
in fact take on one of only a limited set of values. However, such
a declaration may always be omitted, and the program will still run correctly.
Such a declaration merely constitutes advice from the user
that may be useful in gaining efficiency. See <tt>declare</tt>.)
<P>
In Common Lisp, a data type is a (possibly infinite) set of
Lisp objects. Many Lisp objects belong to more than one
such set, and so it doesn't always make sense to ask what is <i>the</i> type
of an object; instead, one usually asks only whether an object belongs
to a given type. The predicate <tt>typep</tt> may be used to ask
whether an object belongs to a given type,
and the function <tt>type-of</tt> returns <i>a</i> type
to which a given object belongs.
<P>
The data types defined in Common Lisp are arranged into a hierarchy (actually
a partial order) defined by the subset relationship.
Certain sets of objects, such as the set of numbers or the
set of strings, are interesting enough to deserve labels.
Symbols are used for most
such labels (here, and throughout this book, the word ``symbol''
refers to atomic symbols, one kind of Lisp object,
elsewhere known as literal atoms). See chapter <A HREF="node44.html#DTSPEC">4</A>
for a complete description of type specifiers.
<P>
The set of all objects is specified
by the symbol <tt>t</tt>. The empty data type, which contains no objects, is
denoted by <tt>nil</tt>.
<P>
<img align=bottom alt="old_change_begin" src="gif/old_change_begin.gif"><br>
A type called <tt>common</tt> encompasses all the data
objects required by the Common Lisp language. A Common Lisp implementation
is free to provide other data types that are not subtypes of <tt>common</tt>.
<br><img align=bottom alt="old_change_end" src="gif/old_change_end.gif">
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in March 1989
(COMMON-TYPE) <A NAME=419>&#160;</A>
to remove the type <tt>common</tt> (and the predicate <tt>commonp</tt>)
from the language, on the grounds that it has
not proved to be useful in practice and that it could be difficult to redefine in the
face of other changes to the Common Lisp type system (such as the introduction
of CLOS classes).
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
The following categories of Common Lisp objects are of particular interest:
numbers, characters, symbols, lists, arrays, structures, and functions.
There are others as well.
Some of these categories
have many subdivisions. There are also standard types defined to
be the union
of two or more of these categories. The categories listed above, while they
are data types, are neither more nor less ``real'' than other data types;
they simply constitute a particularly useful slice across
the type hierarchy for expository purposes.
<P>
Here are brief descriptions of various Common Lisp data types.
The remaining sections of this chapter go into more detail
and also describe notations for objects
of each type. Descriptions of Lisp functions that operate
on data objects of each type appear in later chapters.
<P>
<UL><LI>
<i>Numbers</i> are provided in various forms and representations.
Common Lisp provides a true integer data type: any integer,
positive or negative, has in principle a representation as a
Common Lisp data object, subject only to total memory limitations (rather than
machine word width).
A true rational data type is provided: the quotient of two integers,
if not an integer, is a ratio.
Floating-point numbers of various ranges and precisions are also
provided, as well as
Cartesian complex numbers.
<P>
<LI>
<i>Characters</i> represent printed glyphs such as letters
or text formatting operations. Strings are one-dimensional
arrays of characters.
Common Lisp provides for a rich character set, including ways to
represent characters of various type styles.
<P>
<LI>
<i>Symbols</i> (sometimes called <i>atomic symbols</i> for emphasis
or clarity) are named data objects. Lisp provides machinery
for locating a symbol object, given its name (in the form
of a string). Symbols have <i>property lists</i>, which in effect
allow symbols to be treated as record structures with an extensible
set of named components, each of which may be any Lisp object.
Symbols also serve to name functions and variables within programs.
<P>
<LI>
<i>Lists</i> are sequences represented in the form of linked cells
called <i>conses</i>. There is a special object (the symbol <tt>nil</tt>)
that is the empty list. All other lists are built recursively by adding a new
element to the front of an existing list. This is done by
creating a new <i>cons</i>, which is an object having two components
called the <i>car</i> and the <i>cdr</i>. The <i>car</i> may hold anything,
and the <i>cdr</i> is made to point to the previously existing list.
(Conses may actually be used completely generally as two-element
record structures, but their most important use is to represent
lists.)
<P>
<LI>
<i>Arrays</i> are dimensioned collections of objects.
An array can have any non-negative number of dimensions and is indexed
by a sequence of integers. A general array can have any Lisp object as
a component; other types of arrays are specialized for efficiency
and can hold only certain types of Lisp objects.
It is possible for two arrays, possibly with differing dimension information,
to share the same set of elements (such that modifying one array modifies
the other also) by causing one to be <i>displaced</i> to the other.
One-dimensional arrays of any kind are called <i>vectors</i>.
One-dimensional arrays of characters are called <i>strings</i>.
One-dimensional arrays of bits (that is, of integers whose values are 0 or 1)
are called <i>bit-vectors</i>.
<P>
<LI>
<i>Hash tables</i> provide an efficient way of mapping any
Lisp object (a <i>key</i>) to an associated object.
<P>
<LI>
<i>Readtables</i> are used to control the built-in expression parser
<tt>read</tt>.
<P>
<LI>
<i>Packages</i> are collections of symbols that serve as name spaces.
The parser recognizes symbols by looking up character sequences
in the current package.
<P>
<LI>
<i>Pathnames</i> represent names of files in a fairly implementation-independent
manner. They are used to interface to the external file system.
<P>
<LI>
<i>Streams</i> represent sources or sinks of data, typically characters
or bytes. They are used to perform I/O, as well as for internal
purposes such as parsing strings.
<P>
<LI>
<i>Random-states</i> are data structures used to encapsulate the state
of the built-in random-number generator.
<P>
<LI>
<i>Structures</i> are user-defined record structures, objects that
have named components. The <tt>defstruct</tt> facility is used
to define new structure types. Some Common Lisp implementations may
choose to implement certain system-supplied data types,
such as <i>bignums</i>, <i>readtables</i>, <i>streams</i>,
<i>hash tables</i>, and <i>pathnames</i>, as structures,
but this fact will be invisible to the user.
</UL>
<P>
<img align=bottom alt="old_change_begin" src="gif/old_change_begin.gif"><br>
<UL>
<LI><i>Functions</i> are objects that can be invoked as procedures;
these may take arguments and return values. (All Lisp procedures
can be construed to return values and therefore every procedure is
a function.)
Such objects include <i>compiled-functions</i> (compiled code objects).
Some functions are represented as a list whose <i>car</i> is a particular
symbol such as <tt>lambda</tt>.
Symbols may also be used as functions.
</UL>
<br><img align=bottom alt="old_change_end" src="gif/old_change_end.gif">
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in June 1988 (FUNCTION-TYPE) <A NAME=467>&#160;</A>
to specify that symbols are not of type <tt>function</tt>,
but are automatically coerced to functions
in certain situations (see section <A HREF="node40.html#FUNCTIONTYPESECTION">2.13</A>).
<P>
X3J13 voted in June 1988
(CONDITION-SYSTEM) <A NAME=472>&#160;</A>
to adopt the Common Lisp Condition System,
thereby introducing a new category of data objects:
<UL><LI>
<i>Conditions</i> are objects used to affect control flow in certain
conventional ways by means of signals and handlers that intercept those signals.
In particular, errors are signaled by raising particular conditions,
and errors may be trapped by establishing handlers for those conditions.
</UL>
<P>
X3J13 voted in June 1988
(CLOS) <A NAME=478>&#160;</A>
to adopt the Common Lisp Object System,
thereby introducing additional categories of data objects:
<UL><LI>
<i>Classes</i> determine the structure and behavior of other
objects, their <i>instances</i>. Every Common Lisp data object
belongs to some class. (In some ways the CLOS class system is
a generalization of the system of type specifiers of the first edition of this book,
but the class system augments the type system rather than supplanting it.)
<P>
<LI>
<i>Methods</i> are chunks of code that operate on arguments
satisfying a particular pattern of classes. Methods are
not functions; they are not invoked directly on arguments
but instead are bundled into generic functions.
<P>
<LI>
<i>Generic functions</i> are functions that contain, among other
information, a set of methods. When invoked, a generic function
executes a subset of its methods. The subset chosen for execution
depends in a specific way on the classes or identities of the arguments
to which it is applied.
</UL>
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
These categories are not always mutually exclusive.
The required relationships among the various data types are
explained in more detail in section <A HREF="node42.html#DATATYPERELATIONSHIPS">2.15</A>.
<P>
<HR>
<UL>
<LI> <A NAME=tex2html1670 HREF="node16.html#SECTION00610000000000000000"> Numbers</A>
<UL>
<LI> <A NAME=tex2html1671 HREF="node17.html#SECTION00611000000000000000"> Integers</A>
<LI> <A NAME=tex2html1672 HREF="node18.html#SECTION00612000000000000000"> Ratios</A>
<LI> <A NAME=tex2html1673 HREF="node19.html#SECTION00613000000000000000"> Floating-Point Numbers</A>
<LI> <A NAME=tex2html1674 HREF="node20.html#SECTION00614000000000000000"> Complex Numbers</A>
</UL>
<LI> <A NAME=tex2html1675 HREF="node21.html#SECTION00620000000000000000"> Characters</A>
<UL>
<LI> <A NAME=tex2html1676 HREF="node22.html#SECTION00621000000000000000"> Standard Characters</A>
<LI> <A NAME=tex2html1677 HREF="node23.html#SECTION00622000000000000000"> Line Divisions</A>
<LI> <A NAME=tex2html1678 HREF="node24.html#SECTION00623000000000000000"> Non-standard Characters</A>
<LI> <A NAME=tex2html1679 HREF="node25.html#SECTION00624000000000000000"> Character Attributes</A>
<LI> <A NAME=tex2html1680 HREF="node26.html#SECTION00625000000000000000"> String Characters</A>
</UL>
<LI> <A NAME=tex2html1681 HREF="node27.html#SECTION00630000000000000000"> Symbols</A>
<LI> <A NAME=tex2html1682 HREF="node28.html#SECTION00640000000000000000"> Lists and Conses</A>
<LI> <A NAME=tex2html1683 HREF="node29.html#SECTION00650000000000000000"> Arrays</A>
<UL>
<LI> <A NAME=tex2html1684 HREF="node30.html#SECTION00651000000000000000"> Vectors</A>
<LI> <A NAME=tex2html1685 HREF="node31.html#SECTION00652000000000000000"> Strings</A>
<LI> <A NAME=tex2html1686 HREF="node32.html#SECTION00653000000000000000"> Bit-Vectors</A>
</UL>
<LI> <A NAME=tex2html1687 HREF="node33.html#SECTION00660000000000000000"> Hash Tables</A>
<LI> <A NAME=tex2html1688 HREF="node34.html#SECTION00670000000000000000"> Readtables</A>
<LI> <A NAME=tex2html1689 HREF="node35.html#SECTION00680000000000000000"> Packages</A>
<LI> <A NAME=tex2html1690 HREF="node36.html#SECTION00690000000000000000"> Pathnames</A>
<LI> <A NAME=tex2html1691 HREF="node37.html#SECTION006100000000000000000"> Streams</A>
<LI> <A NAME=tex2html1692 HREF="node38.html#SECTION006110000000000000000"> Random-States</A>
<LI> <A NAME=tex2html1693 HREF="node39.html#SECTION006120000000000000000"> Structures</A>
<LI> <A NAME=tex2html1694 HREF="node40.html#SECTION006130000000000000000"> Functions</A>
<LI> <A NAME=tex2html1695 HREF="node41.html#SECTION006140000000000000000"> Unreadable Data Objects</A>
<LI> <A NAME=tex2html1696 HREF="node42.html#SECTION006150000000000000000"> Overlap, Inclusion, and Disjointness of Types</A>
</UL>
<BR> <HR><A NAME=tex2html1666 HREF="node16.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html1664 HREF="clm.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html1658 HREF="node14.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html1668 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html1669 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html1667 HREF="node16.html"> Numbers</A>
<B>Up:</B> <A NAME=tex2html1665 HREF="clm.html">Common Lisp the Language</A>
<B> Previous:</B> <A NAME=tex2html1659 HREF="node14.html"> Overview of Syntax</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>