comparison lispref/sequences.texi @ 12098:a6eb5f12b0f3

*** empty log message ***
author Karl Heuer <kwzh@gnu.org>
date Tue, 06 Jun 1995 19:21:15 +0000
parents 73dc8205d259
children 66d807bdc5b4
comparison
equal deleted inserted replaced
12097:d8c035332be8 12098:a6eb5f12b0f3
13 common property that all sequences have is that each is an ordered 13 common property that all sequences have is that each is an ordered
14 collection of elements. 14 collection of elements.
15 15
16 An @dfn{array} is a single primitive object that has a slot for each 16 An @dfn{array} is a single primitive object that has a slot for each
17 elements. All the elements are accessible in constant time, but the 17 elements. All the elements are accessible in constant time, but the
18 length of an existing array cannot be changed. Both strings and vectors 18 length of an existing array cannot be changed. Strings and vectors are
19 are arrays. 19 the two types of arrays.
20 20
21 A list is a sequence of elements, but it is not a single primitive 21 A list is a sequence of elements, but it is not a single primitive
22 object; it is made of cons cells, one cell per element. Finding the 22 object; it is made of cons cells, one cell per element. Finding the
23 @var{n}th element requires looking through @var{n} cons cells, so 23 @var{n}th element requires looking through @var{n} cons cells, so
24 elements farther from the beginning of the list take longer to access. 24 elements farther from the beginning of the list take longer to access.
34 | ______ ______________________ | 34 | ______ ______________________ |
35 | | | | | | 35 | | | | | |
36 | | List | | Array | | 36 | | List | | Array | |
37 | | | | ________ _______ | | 37 | | | | ________ _______ | |
38 | |______| | | | | | | | 38 | |______| | | | | | | |
39 | | | String | | Vector| | | 39 | | | Vector | | String| | |
40 | | |________| |_______| | | 40 | | |________| |_______| | |
41 | |______________________| | 41 | |______________________| |
42 |___________________________________| 42 |___________________________________|
43 @end group 43 @end group
44 @end example 44 @end example
189 (elt [1 2 3 4] -1) 189 (elt [1 2 3 4] -1)
190 @error{}Args out of range: [1 2 3 4], -1 190 @error{}Args out of range: [1 2 3 4], -1
191 @end group 191 @end group
192 @end example 192 @end example
193 193
194 This function duplicates @code{aref} (@pxref{Array Functions}) and 194 This function generalizes @code{aref} (@pxref{Array Functions}) and
195 @code{nth} (@pxref{List Elements}), except that it works for any kind of 195 @code{nth} (@pxref{List Elements}).
196 sequence.
197 @end defun 196 @end defun
198 197
199 @node Arrays 198 @node Arrays
200 @section Arrays 199 @section Arrays
201 @cindex array 200 @cindex array
230 @item 229 @item
231 The elements of an array may be referenced or changed with the functions 230 The elements of an array may be referenced or changed with the functions
232 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}). 231 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
233 @end itemize 232 @end itemize
234 233
235 In principle, if you wish to have an array of characters, you could use 234 In principle, if you wish to have an array of text characters, you
236 either a string or a vector. In practice, we always choose strings for 235 could use either a string or a vector. In practice, we always choose
237 such applications, for four reasons: 236 strings for such applications, for four reasons:
238 237
239 @itemize @bullet 238 @itemize @bullet
240 @item 239 @item
241 They occupy one-fourth the space of a vector of the same elements. 240 They occupy one-fourth the space of a vector of the same elements.
242 241
250 @item 249 @item
251 Many of the specialized editing and I/O facilities of Emacs accept only 250 Many of the specialized editing and I/O facilities of Emacs accept only
252 strings. For example, you cannot insert a vector of characters into a 251 strings. For example, you cannot insert a vector of characters into a
253 buffer the way you can insert a string. @xref{Strings and Characters}. 252 buffer the way you can insert a string. @xref{Strings and Characters}.
254 @end itemize 253 @end itemize
254
255 By contrast, for an array of keyboard input characters (such as a key
256 sequence), a vector may be necessary, because many keyboard input
257 characters are outside the range that will fit in a string. @xref{Key
258 Sequence Input}.
255 259
256 @node Array Functions 260 @node Array Functions
257 @section Functions that Operate on Arrays 261 @section Functions that Operate on Arrays
258 262
259 In this section, we describe the functions that accept both strings 263 In this section, we describe the functions that accept both strings