emacs.d/clones/lisp/colinallen.dnsalias.org/lp/node54.html

99 lines
3.6 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Originally converted to HTML using LaTeX2HTML 95 (Thu Jan 19 1995) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE> Arrays and Vectors</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Arrays and Vectors">
<meta name="keywords" value="lp">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<BR> <HR>
<A HREF="node55.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
<A HREF="node53.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
<A HREF="node53.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <BR>
<A HREF="lp.html"><B>Contents</B></A>
<B> Next:</B>
<A HREF="node55.html"> Strings</A>
<B>Up:</B>
<A HREF="node53.html"> ArraysVectors, and </A>
<B> Previous:</B>
<A HREF="node53.html"> ArraysVectors, and </A>
<BR> <HR> <P>
<H2> Arrays and Vectors</H2>
<P>
An array is a special type of data object in Lisp. Arrays are created using the make-array function. To make an array it is necessary to specify the size and dimensions. The simplest case is an array of one dimension, also called a vector.
<P>
Make-array returns an array. Most often one wants to bind this array to a symbol. Here's an example:
<P>
<BLOCKQUOTE>
<PRE>&gt; (setf my-vector (make-array '(3)))
#(NIL NIL NIL)
</PRE>
</BLOCKQUOTE>
In this case, the argument to make-array specified that the array should have one dimension of three elements. The array that was returned has three elements, all of them initially set to nil. (The actual printed representation of the array may vary between different Lisp implementations.)
<P>
These elements can be accessed and changed using aref. These examples illustrate:
<P>
<BLOCKQUOTE>
<PRE>&gt; (aref my-vector 2)
NIL
&gt; (setf (aref my-vector 0) t)
T
&gt; my-vector
#(T NIL NIL)
</PRE>
</BLOCKQUOTE>
Indexing of arrays starts with 0. (Just like indexing of lists using nth.) Here's an example of a two-dimensional array and some assignments.
<P>
<BLOCKQUOTE>
<PRE>&gt; (setf my-array (make-array '(2 3)))
#2A((NIL NIL NIL) (NIL NIL NIL))
&gt; (aref my-array 0 1)
NIL
&gt; (setf (aref my-array 0 1) 'hi)
HI
&gt; (setf (aref my-array 1 0) 'bye)
BYE
&gt; my-array
#2A((NIL HI NIL) (BYE NIL NIL))
</PRE>
</BLOCKQUOTE>
From this example you should be able to work out the indexing scheme.
<P>
Make array has a number of additional features we will not cover here. However, one that is particularly useful is the :initial-contents keyword. Here is an example to illustrate the use of :initial-contents.
<P>
<BLOCKQUOTE>
<PRE>&gt; (make-array '(2 3 4) :initial-contents
'(((a b c d) (e f g h) (i j k l))
((m n o p) (q r s t) (u v w x))))
#3A(((A B C D) (E F G H) (I J K L)) ((M N O P) (Q R S T)
(U V W X)))
</PRE>
</BLOCKQUOTE>
Initial contents are specified with a list of elements having the required sublist structure to match the array.
<P>
The use of :initial-contents is entirely optional with make-array, as is the use of other keywords not introduced here.
<P>
Programmers like to use arrays because they give uniformly fast access to all their elements. In general, however, arrays are less flexible for representing structure than lists. Consider the structure represented in this list:
<P>
<BLOCKQUOTE>
<PRE>(a (b c) (d e f (g h)))
</PRE>
</BLOCKQUOTE>
No array can concisely reproduce this structure, since it must have a uniform number of elements for each of its dimensions.
<P>
<BR> <HR>
<P>
<ADDRESS>
<I>&#169; Colin Allen &amp; Maneesh Dhagat <BR>
March 2007 </I>
</ADDRESS>
</BODY>