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

87 lines
4.7 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>17.2. Array Access</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Array Access">
<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=tex2html3513 HREF="node160.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3511 HREF="node157.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3505 HREF="node158.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3515 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3516 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html3514 HREF="node160.html"> Array Information</A>
<B>Up:</B> <A NAME=tex2html3512 HREF="node157.html"> Arrays</A>
<B> Previous:</B> <A NAME=tex2html3506 HREF="node158.html"> Array Creation</A>
<HR> <P>
<H1><A NAME=SECTION002120000000000000000>17.2. Array Access</A></H1>
<P>
The function <tt>aref</tt> is normally
used for accessing an element of an array.
Other access functions, such as <tt>svref</tt>, <tt>char</tt>, and <tt>bit</tt>,
may be more efficient in specialized circumstances.
<P>
<BR><b>[Function]</b><BR>
<tt>aref <i>array</i> &amp;rest <i>subscripts</i></tt><P>This accesses and returns the element of <i>array</i> specified
by the <i>subscripts</i>. The number of subscripts must
equal the rank of the array, and each subscript must be
a non-negative integer less than the corresponding array dimension.
<P>
<tt>aref</tt> is unusual among the functions that operate on arrays
in that it completely ignores fill pointers. <tt>aref</tt> can access
without error any array element, whether active or not. The generic
sequence function <tt>elt</tt>, however, observes the fill pointer;
accessing an element beyond the fill pointer with <tt>elt</tt> is an error.
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
Note that this remark, predating the design of the Common Lisp Object System,
uses the term ``generic'' in a generic sense and not necessarily
in the technical sense used by CLOS
(see chapter <A HREF="node15.html#DTYPES">2</A>).
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<tt>setf</tt> may be used with <tt>aref</tt> to destructively replace
an array element with a new value.
<P>
Under some circumstances it is desirable to write code that
will extract an element from an array <tt>a</tt> given a list <tt>z</tt> of the indices,
in such a way that the code works regardless of the rank of the
array. This is easy using <tt>apply</tt>:
<P><pre>
(apply #'aref a z)
</pre><P>
(The length of the list must of course equal the rank of
the array.) This construction may be used with <tt>setf</tt> to alter
the element so selected to some new value <tt>w</tt>:
<P><pre>
(setf (apply #'aref a z) w)
</pre><P>
<P>
<BR><b>[Function]</b><BR>
<tt>svref <i>simple-vector</i> <i>index</i></tt><P>The first argument must be a simple general vector,
that is, an object of type <tt>simple-vector</tt>.
The element of the <i>simple-vector</i> specified by the integer <i>index</i>
is returned.
<P>
The <i>index</i> must be non-negative and less than
the length of the vector.
<P>
<tt>setf</tt> may be used with <tt>svref</tt> to destructively replace
a simple-vector element with a new value.
<P>
<tt>svref</tt> is identical to <tt>aref</tt> except that it requires its first
argument to be a simple vector. In some implementations of Common Lisp,
<tt>svref</tt> may be faster than <tt>aref</tt> in situations where it is applicable.
See also <tt>schar</tt> and <tt>sbit</tt>.
<P>
<BR> <HR><A NAME=tex2html3513 HREF="node160.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3511 HREF="node157.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3505 HREF="node158.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3515 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3516 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html3514 HREF="node160.html"> Array Information</A>
<B>Up:</B> <A NAME=tex2html3512 HREF="node157.html"> Arrays</A>
<B> Previous:</B> <A NAME=tex2html3506 HREF="node158.html"> Array Creation</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>