emacs.d/clones/lisp/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node30.html

82 lines
4.5 KiB
HTML
Raw Normal View History

2022-08-26 19:11:35 +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.5.1. Vectors</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Vectors">
<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=tex2html1881 HREF="node31.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html1879 HREF="node29.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html1873 HREF="node29.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html1883 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html1884 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html1882 HREF="node31.html"> Strings</A>
<B>Up:</B> <A NAME=tex2html1880 HREF="node29.html"> Arrays</A>
<B> Previous:</B> <A NAME=tex2html1874 HREF="node29.html"> Arrays</A>
<HR> <P>
<H2><A NAME=SECTION00651000000000000000>2.5.1. Vectors</A></H2>
<P>
One-dimensional arrays are called <i>vectors</i> in Common Lisp
and constitute the type <tt>vector</tt> (which is therefore a subtype of <tt>array</tt>).
Vectors and lists are collectively considered to be
<i>sequences</i>. They differ in that any component of a one-dimensional array
can be accessed in constant time,
whereas the average component access time for a
list is linear in the length of the list; on the other hand, adding a new
element to the front of a list takes constant time, whereas the same
operation on an array takes time linear in the length of the array.
<P>
A general vector (a one-dimensional array
that can have any data object as an element but that has
no additional paraphernalia) can be notated by notating the
components in order, separated by whitespace and surrounded by <tt>#(</tt>
and <tt>)</tt>.
For example:
<P><pre>
#(a b c) ;A vector of length 3
#() ;An empty vector
#(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47)
;A vector containing the primes below 50
</pre><P>
Note that when the function <tt>read</tt> parses this syntax, it always constructs
a <i>simple</i> general vector.
<P>
<hr>
<b>Rationale:</b> Many people have suggested that brackets be used
to notate vectors, as <tt>[a b c]</tt>
instead of <tt>#(a b c)</tt>. This notation
would be shorter, perhaps more readable, and certainly in accord with
cultural conventions in other parts of computer science and mathematics.
However, to preserve the usefulness of the user-definable macro-character
feature of the function <tt>read</tt>, it is necessary to leave some
characters to the user for this purpose. Experience in MacLisp has
shown that users, especially implementors of languages for use
in artificial intelligence research, often want
to define special kinds of brackets. Therefore Common Lisp avoids using
brackets and braces for any syntactic purpose.
<hr>
<P>
Implementations may provide certain specialized representations of
arrays for efficiency in the case where all the components are of
the same specialized (typically numeric) type. All implementations
provide specialized arrays for the cases when the components
are characters (or rather, a special subset of the characters);
the one-dimensional instances of
this specialization are called <i>strings</i>.
All implementations are also required to provide specialized arrays
of bits, that is, arrays of type <tt>(array bit)</tt>;
the one-dimensional instances of
this specialization are called <i>bit-vectors</i>.
<P>
<BR> <HR><A NAME=tex2html1881 HREF="node31.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html1879 HREF="node29.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html1873 HREF="node29.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html1883 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html1884 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html1882 HREF="node31.html"> Strings</A>
<B>Up:</B> <A NAME=tex2html1880 HREF="node29.html"> Arrays</A>
<B> Previous:</B> <A NAME=tex2html1874 HREF="node29.html"> Arrays</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>