Mercurial > emacs
comparison lispref/sequences.texi @ 21682:90da2489c498
*** empty log message ***
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Mon, 20 Apr 1998 17:43:57 +0000 |
parents | 66d807bdc5b4 |
children | d4ac295a98b3 |
comparison
equal
deleted
inserted
replaced
21681:11eafe90b842 | 21682:90da2489c498 |
---|---|
5 @setfilename ../info/sequences | 5 @setfilename ../info/sequences |
6 @node Sequences Arrays Vectors, Symbols, Lists, Top | 6 @node Sequences Arrays Vectors, Symbols, Lists, Top |
7 @chapter Sequences, Arrays, and Vectors | 7 @chapter Sequences, Arrays, and Vectors |
8 @cindex sequence | 8 @cindex sequence |
9 | 9 |
10 Recall that the @dfn{sequence} type is the union of three other Lisp | 10 Recall that the @dfn{sequence} type is the union of two other Lisp |
11 types: lists, vectors, and strings. In other words, any list is a | 11 types: lists and arrays. In other words, any list is a sequence, and |
12 sequence, any vector is a sequence, and any string is a sequence. The | 12 any array is a sequence. The common property that all sequences have is |
13 common property that all sequences have is that each is an ordered | 13 that each is an ordered collection of elements. |
14 collection of elements. | |
15 | 14 |
16 An @dfn{array} is a single primitive object that has a slot for each | 15 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 | 16 of its elements. All the elements are accessible in constant time, but |
18 length of an existing array cannot be changed. Strings and vectors are | 17 the length of an existing array cannot be changed. Strings, vectors, |
19 the two types of arrays. | 18 char-tables and bool-vectors are the four types of arrays. |
20 | 19 |
21 A list is a sequence of elements, but it is not a single primitive | 20 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 | 21 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 | 22 @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. | 23 elements farther from the beginning of the list take longer to access. |
26 | 25 |
27 The following diagram shows the relationship between these types: | 26 The following diagram shows the relationship between these types: |
28 | 27 |
29 @example | 28 @example |
30 @group | 29 @group |
31 ___________________________________ | 30 _____________________________________________ |
32 | | | 31 | | |
33 | Sequence | | 32 | Sequence | |
34 | ______ ______________________ | | 33 | ______ ________________________________ | |
35 | | | | | | | 34 | | | | | | |
36 | | List | | Array | | | 35 | | List | | Array | | |
37 | | | | ________ _______ | | | 36 | | | | ________ ________ | | |
38 | |______| | | | | | | | | 37 | |______| | | | | | | | |
39 | | | Vector | | String| | | | 38 | | | Vector | | String | | | |
40 | | |________| |_______| | | | 39 | | |________| |________| | | |
41 | |______________________| | | 40 | | ____________ _____________ | | |
42 |___________________________________| | 41 | | | | | | | | |
42 | | | Char-table | | Bool-vector | | | | |
43 | | |____________| |_____________| | | | |
44 | |________________________________| | | |
45 |_____________________________________________| | |
43 @end group | 46 @end group |
44 @end example | 47 @end example |
45 | 48 |
46 The elements of vectors and lists may be any Lisp objects. The | 49 The elements of vectors and lists may be any Lisp objects. The |
47 elements of strings are all characters. | 50 elements of strings are all characters. |
57 @end menu | 60 @end menu |
58 | 61 |
59 @node Sequence Functions | 62 @node Sequence Functions |
60 @section Sequences | 63 @section Sequences |
61 | 64 |
62 In Emacs Lisp, a @dfn{sequence} is either a list, a vector or a | 65 In Emacs Lisp, a @dfn{sequence} is either a list or an array. The |
63 string. The common property that all sequences have is that each is an | 66 common property of all sequences is that they are ordered collections of |
64 ordered collection of elements. This section describes functions that | 67 elements. This section describes functions that accept any kind of |
65 accept any kind of sequence. | 68 sequence. |
66 | 69 |
67 @defun sequencep object | 70 @defun sequencep object |
68 Returns @code{t} if @var{object} is a list, vector, or | 71 Returns @code{t} if @var{object} is a list, vector, or |
69 string, @code{nil} otherwise. | 72 string, @code{nil} otherwise. |
70 @end defun | |
71 | |
72 @defun copy-sequence sequence | |
73 @cindex copying sequences | |
74 Returns a copy of @var{sequence}. The copy is the same type of object | |
75 as the original sequence, and it has the same elements in the same order. | |
76 | |
77 Storing a new element into the copy does not affect the original | |
78 @var{sequence}, and vice versa. However, the elements of the new | |
79 sequence are not copies; they are identical (@code{eq}) to the elements | |
80 of the original. Therefore, changes made within these elements, as | |
81 found via the copied sequence, are also visible in the original | |
82 sequence. | |
83 | |
84 If the sequence is a string with text properties, the property list in | |
85 the copy is itself a copy, not shared with the original's property | |
86 list. However, the actual values of the properties are shared. | |
87 @xref{Text Properties}. | |
88 | |
89 See also @code{append} in @ref{Building Lists}, @code{concat} in | |
90 @ref{Creating Strings}, and @code{vconcat} in @ref{Vectors}, for others | |
91 ways to copy sequences. | |
92 | |
93 @example | |
94 @group | |
95 (setq bar '(1 2)) | |
96 @result{} (1 2) | |
97 @end group | |
98 @group | |
99 (setq x (vector 'foo bar)) | |
100 @result{} [foo (1 2)] | |
101 @end group | |
102 @group | |
103 (setq y (copy-sequence x)) | |
104 @result{} [foo (1 2)] | |
105 @end group | |
106 | |
107 @group | |
108 (eq x y) | |
109 @result{} nil | |
110 @end group | |
111 @group | |
112 (equal x y) | |
113 @result{} t | |
114 @end group | |
115 @group | |
116 (eq (elt x 1) (elt y 1)) | |
117 @result{} t | |
118 @end group | |
119 | |
120 @group | |
121 ;; @r{Replacing an element of one sequence.} | |
122 (aset x 0 'quux) | |
123 x @result{} [quux (1 2)] | |
124 y @result{} [foo (1 2)] | |
125 @end group | |
126 | |
127 @group | |
128 ;; @r{Modifying the inside of a shared element.} | |
129 (setcar (aref x 1) 69) | |
130 x @result{} [quux (69 2)] | |
131 y @result{} [foo (69 2)] | |
132 @end group | |
133 @end example | |
134 @end defun | 73 @end defun |
135 | 74 |
136 @defun length sequence | 75 @defun length sequence |
137 @cindex string length | 76 @cindex string length |
138 @cindex list length | 77 @cindex list length |
139 @cindex vector length | 78 @cindex vector length |
140 @cindex sequence length | 79 @cindex sequence length |
141 Returns the number of elements in @var{sequence}. If @var{sequence} is | 80 This function returns the number of elements in @var{sequence}. If |
142 a cons cell that is not a list (because the final @sc{cdr} is not | 81 @var{sequence} is a cons cell that is not a list (because the final |
143 @code{nil}), a @code{wrong-type-argument} error is signaled. | 82 @sc{cdr} is not @code{nil}), a @code{wrong-type-argument} error is |
83 signaled. | |
144 | 84 |
145 @xref{List Elements}, for the related function @code{safe-list}. | 85 @xref{List Elements}, for the related function @code{safe-list}. |
146 | 86 |
147 @example | 87 @example |
148 @group | 88 @group |
158 @result{} 6 | 98 @result{} 6 |
159 @end group | 99 @end group |
160 @group | 100 @group |
161 (length [1 2 3]) | 101 (length [1 2 3]) |
162 @result{} 3 | 102 @result{} 3 |
103 @end group | |
104 @group | |
105 (length (make-bool-vector 5 nil)) | |
106 @result{} 5 | |
163 @end group | 107 @end group |
164 @end example | 108 @end example |
165 @end defun | 109 @end defun |
166 | 110 |
167 @defun elt sequence index | 111 @defun elt sequence index |
180 @group | 124 @group |
181 (elt '(1 2 3 4) 2) | 125 (elt '(1 2 3 4) 2) |
182 @result{} 3 | 126 @result{} 3 |
183 @end group | 127 @end group |
184 @group | 128 @group |
185 (char-to-string (elt "1234" 2)) | 129 ;; @r{We use @code{string} to show clearly which character @code{elt} returns.} |
130 (string (elt "1234" 2)) | |
186 @result{} "3" | 131 @result{} "3" |
187 @end group | 132 @end group |
188 @group | 133 @group |
189 (elt [1 2 3 4] 4) | 134 (elt [1 2 3 4] 4) |
190 @error{}Args out of range: [1 2 3 4], 4 | 135 @error{}Args out of range: [1 2 3 4], 4 |
195 @end group | 140 @end group |
196 @end example | 141 @end example |
197 | 142 |
198 This function generalizes @code{aref} (@pxref{Array Functions}) and | 143 This function generalizes @code{aref} (@pxref{Array Functions}) and |
199 @code{nth} (@pxref{List Elements}). | 144 @code{nth} (@pxref{List Elements}). |
145 @end defun | |
146 | |
147 @defun copy-sequence sequence | |
148 @cindex copying sequences | |
149 Returns a copy of @var{sequence}. The copy is the same type of object | |
150 as the original sequence, and it has the same elements in the same order. | |
151 | |
152 Storing a new element into the copy does not affect the original | |
153 @var{sequence}, and vice versa. However, the elements of the new | |
154 sequence are not copies; they are identical (@code{eq}) to the elements | |
155 of the original. Therefore, changes made within these elements, as | |
156 found via the copied sequence, are also visible in the original | |
157 sequence. | |
158 | |
159 If the sequence is a string with text properties, the property list in | |
160 the copy is itself a copy, not shared with the original's property | |
161 list. However, the actual values of the properties are shared. | |
162 @xref{Text Properties}. | |
163 | |
164 See also @code{append} in @ref{Building Lists}, @code{concat} in | |
165 @ref{Creating Strings}, and @code{vconcat} in @ref{Vectors}, for others | |
166 ways to copy sequences. | |
167 | |
168 @example | |
169 @group | |
170 (setq bar '(1 2)) | |
171 @result{} (1 2) | |
172 @end group | |
173 @group | |
174 (setq x (vector 'foo bar)) | |
175 @result{} [foo (1 2)] | |
176 @end group | |
177 @group | |
178 (setq y (copy-sequence x)) | |
179 @result{} [foo (1 2)] | |
180 @end group | |
181 | |
182 @group | |
183 (eq x y) | |
184 @result{} nil | |
185 @end group | |
186 @group | |
187 (equal x y) | |
188 @result{} t | |
189 @end group | |
190 @group | |
191 (eq (elt x 1) (elt y 1)) | |
192 @result{} t | |
193 @end group | |
194 | |
195 @group | |
196 ;; @r{Replacing an element of one sequence.} | |
197 (aset x 0 'quux) | |
198 x @result{} [quux (1 2)] | |
199 y @result{} [foo (1 2)] | |
200 @end group | |
201 | |
202 @group | |
203 ;; @r{Modifying the inside of a shared element.} | |
204 (setcar (aref x 1) 69) | |
205 x @result{} [quux (69 2)] | |
206 y @result{} [foo (69 2)] | |
207 @end group | |
208 @end example | |
200 @end defun | 209 @end defun |
201 | 210 |
202 @node Arrays | 211 @node Arrays |
203 @section Arrays | 212 @section Arrays |
204 @cindex array | 213 @cindex array |
207 objects, called the elements of the array. Any element of an array may | 216 objects, called the elements of the array. Any element of an array may |
208 be accessed in constant time. In contrast, an element of a list | 217 be accessed in constant time. In contrast, an element of a list |
209 requires access time that is proportional to the position of the element | 218 requires access time that is proportional to the position of the element |
210 in the list. | 219 in the list. |
211 | 220 |
212 When you create an array, you must specify how many elements it has. | 221 Emacs defines four types of array, both of which are one-dimensional: |
213 The amount of space allocated depends on the number of elements. | 222 @dfn{strings}, @dfn{vectors}, @dfn{bool-vectors} and @dfn{char-tables}. |
214 Therefore, it is impossible to change the size of an array once it is | 223 A vector is a general array; its elements can be any Lisp objects. A |
215 created; you cannot add or remove elements. However, you can replace an | 224 string is a specialized array; its elements must be characters (i.e., |
216 element with a different value. | 225 integers between 0 and 255). Each type of array has its own read |
217 | 226 syntax. @xref{String Type}, and @ref{Vector Type}. |
218 Emacs defines two types of array, both of which are one-dimensional: | 227 |
219 @dfn{strings} and @dfn{vectors}. A vector is a general array; its | 228 All four kinds of array share these characteristics: |
220 elements can be any Lisp objects. A string is a specialized array; its | |
221 elements must be characters (i.e., integers between 0 and 255). Each | |
222 type of array has its own read syntax. @xref{String Type}, and | |
223 @ref{Vector Type}. | |
224 | |
225 Both kinds of array share these characteristics: | |
226 | 229 |
227 @itemize @bullet | 230 @itemize @bullet |
228 @item | 231 @item |
229 The first element of an array has index zero, the second element has | 232 The first element of an array has index zero, the second element has |
230 index 1, and so on. This is called @dfn{zero-origin} indexing. For | 233 index 1, and so on. This is called @dfn{zero-origin} indexing. For |
231 example, an array of four elements has indices 0, 1, 2, @w{and 3}. | 234 example, an array of four elements has indices 0, 1, 2, @w{and 3}. |
232 | 235 |
233 @item | 236 @item |
237 The length of the array is fixed once you create it; you cannot | |
238 change the length of an existing array. | |
239 | |
240 @item | |
241 The array is a constant, for evaluation---in other words, it evaluates | |
242 to itself. | |
243 | |
244 @item | |
234 The elements of an array may be referenced or changed with the functions | 245 The elements of an array may be referenced or changed with the functions |
235 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}). | 246 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}). |
236 @end itemize | 247 @end itemize |
237 | 248 |
238 In principle, if you wish to have an array of text characters, you | 249 When you create an array, other than a char-table, you must specify |
239 could use either a string or a vector. In practice, we always choose | 250 its length. You cannot specify the length of a char-table, because that |
240 strings for such applications, for four reasons: | 251 is determined by the range of character codes. |
252 | |
253 In principle, if you want an array of text characters, you could use | |
254 either a string or a vector. In practice, we always choose strings for | |
255 such applications, for four reasons: | |
241 | 256 |
242 @itemize @bullet | 257 @itemize @bullet |
243 @item | 258 @item |
244 They occupy one-fourth the space of a vector of the same elements. | 259 They occupy one-fourth the space of a vector of the same elements. |
245 | 260 |
272 vector, a string, a bool-vector or a char-table). | 287 vector, a string, a bool-vector or a char-table). |
273 | 288 |
274 @example | 289 @example |
275 @group | 290 @group |
276 (arrayp [a]) | 291 (arrayp [a]) |
277 @result{} t | 292 @result{} t |
278 (arrayp "asdf") | 293 (arrayp "asdf") |
279 @result{} t | 294 @result{} t |
295 (arrayp (syntax-table)) ;; @r{A char-table.} | |
296 @result{} t | |
280 @end group | 297 @end group |
281 @end example | 298 @end example |
282 @end defun | 299 @end defun |
283 | 300 |
284 @defun aref array index | 301 @defun aref array index |
290 @group | 307 @group |
291 (setq primes [2 3 5 7 11 13]) | 308 (setq primes [2 3 5 7 11 13]) |
292 @result{} [2 3 5 7 11 13] | 309 @result{} [2 3 5 7 11 13] |
293 (aref primes 4) | 310 (aref primes 4) |
294 @result{} 11 | 311 @result{} 11 |
295 (elt primes 4) | 312 @end group |
296 @result{} 11 | |
297 @end group | |
298 | |
299 @group | 313 @group |
300 (aref "abcdefg" 1) | 314 (aref "abcdefg" 1) |
301 @result{} 98 ; @r{@samp{b} is @sc{ASCII} code 98.} | 315 @result{} 98 ; @r{@samp{b} is @sc{ASCII} code 98.} |
302 @end group | 316 @end group |
303 @end example | 317 @end example |
369 @section Vectors | 383 @section Vectors |
370 @cindex vector | 384 @cindex vector |
371 | 385 |
372 Arrays in Lisp, like arrays in most languages, are blocks of memory | 386 Arrays in Lisp, like arrays in most languages, are blocks of memory |
373 whose elements can be accessed in constant time. A @dfn{vector} is a | 387 whose elements can be accessed in constant time. A @dfn{vector} is a |
374 general-purpose array; its elements can be any Lisp objects. (By | 388 general-purpose array of specified length; its elements can be any Lisp |
375 contrast, a string can hold only characters as elements.) Vectors in | 389 objects. (By contrast, a string can hold only characters as elements.) |
376 Emacs are used for obarrays (vectors of symbols), and as part of keymaps | 390 Vectors in Emacs are used for obarrays (vectors of symbols), and as part |
377 (vectors of commands). They are also used internally as part of the | 391 of keymaps (vectors of commands). They are also used internally as part |
378 representation of a byte-compiled function; if you print such a | 392 of the representation of a byte-compiled function; if you print such a |
379 function, you will see a vector in it. | 393 function, you will see a vector in it. |
380 | 394 |
381 In Emacs Lisp, the indices of the elements of a vector start from zero | 395 In Emacs Lisp, the indices of the elements of a vector start from zero |
382 and count up from there. | 396 and count up from there. |
383 | 397 |
403 @result{} t | 417 @result{} t |
404 @end group | 418 @end group |
405 @end example | 419 @end example |
406 | 420 |
407 @node Vector Functions | 421 @node Vector Functions |
408 @section Functions That Operate on Vectors | 422 @section Functions for Vectors |
409 | 423 |
410 Here are some functions that relate to vectors: | 424 Here are some functions that relate to vectors: |
411 | 425 |
412 @defun vectorp object | 426 @defun vectorp object |
413 This function returns @code{t} if @var{object} is a vector. | 427 This function returns @code{t} if @var{object} is a vector. |
470 @result{} [] | 484 @result{} [] |
471 (vconcat [A B C] "aa" '(foo (6 7))) | 485 (vconcat [A B C] "aa" '(foo (6 7))) |
472 @result{} [A B C 97 97 foo (6 7)] | 486 @result{} [A B C 97 97 foo (6 7)] |
473 @end group | 487 @end group |
474 @end example | 488 @end example |
489 | |
490 The @code{vconcat} function also allows byte-code function objects as | |
491 arguments. This is a special feature to make it easy to access the entire | |
492 contents of a byte-code function object. @xref{Byte-Code Objects}. | |
475 | 493 |
476 The @code{vconcat} function also allows integers as arguments. It | 494 The @code{vconcat} function also allows integers as arguments. It |
477 converts them to strings of digits, making up the decimal print | 495 converts them to strings of digits, making up the decimal print |
478 representation of the integer, and then uses the strings instead of the | 496 representation of the integer, and then uses the strings instead of the |
479 original integers. @strong{Don't use this feature; we plan to eliminate | 497 original integers. @strong{Don't use this feature; we plan to eliminate |
503 @section Char-Tables | 521 @section Char-Tables |
504 @cindex char-tables | 522 @cindex char-tables |
505 | 523 |
506 A char-table is much like a vector, except that it is indexed by | 524 A char-table is much like a vector, except that it is indexed by |
507 character codes. Any valid character code, without modifiers, can be | 525 character codes. Any valid character code, without modifiers, can be |
508 used as an index in a char-table. You can access a char-table with | 526 used as an index in a char-table. You can access a char-table's |
509 @code{aref} and @code{aset}, just like a vector. | 527 elements with @code{aref} and @code{aset}, as with any array. |
528 Char-tables are constants when evaluated. | |
510 | 529 |
511 @cindex extra slots of char-table | 530 @cindex extra slots of char-table |
512 @cindex subtype of char-table | 531 @cindex subtype of char-table |
513 Each char-table has a @dfn{subtype} which is a symbol. In order to be | 532 Each char-table has a @dfn{subtype} which is a symbol. In order to be |
514 a valid subtype, a symbol must have a @code{char-table-extra-slots} | 533 a valid subtype, a symbol must have a @code{char-table-extra-slots} |
532 @defun make-char-table subtype &optional init | 551 @defun make-char-table subtype &optional init |
533 Return a newly created char-table, with subtype @var{subtype}. Each | 552 Return a newly created char-table, with subtype @var{subtype}. Each |
534 element is initialized to @var{init}, which defaults to @code{nil}. You | 553 element is initialized to @var{init}, which defaults to @code{nil}. You |
535 cannot alter the subtype of a char-table after the char-table is | 554 cannot alter the subtype of a char-table after the char-table is |
536 created. | 555 created. |
556 | |
557 There is no argument to specify the length of the char-table, because | |
558 all char-tables have room for any valid character code as an index. | |
537 @end defun | 559 @end defun |
538 | 560 |
539 @tindex char-table-p | 561 @tindex char-table-p |
540 @defun char-table-p object | 562 @defun char-table-p object |
541 This function returns @code{t} if @code{object} is a char-table, | 563 This function returns @code{t} if @var{object} is a char-table, |
542 otherwise @code{nil}. | 564 otherwise @code{nil}. |
543 @end defun | 565 @end defun |
544 | 566 |
545 @tindex char-table-subtype | 567 @tindex char-table-subtype |
546 @defun char-table-subtype char-table | 568 @defun char-table-subtype char-table |
626 @defun map-char-table function char-table | 648 @defun map-char-table function char-table |
627 This function calls @var{function} for each element of @var{char-table}. | 649 This function calls @var{function} for each element of @var{char-table}. |
628 @var{function} is called with two arguments, a key and a value. The key | 650 @var{function} is called with two arguments, a key and a value. The key |
629 is a possible @var{range} argument for @code{char-table-range}, and the | 651 is a possible @var{range} argument for @code{char-table-range}, and the |
630 value is @code{(char-table-range @var{char-table} @var{key})}. Invalid | 652 value is @code{(char-table-range @var{char-table} @var{key})}. Invalid |
631 character codes are never used as the key. | 653 character codes are never used as @var{key}. |
632 | 654 |
633 Overall, the keys-value pairs passed to @var{function} describe all the | 655 Overall, the key-value pairs passed to @var{function} describe all the |
634 values stored in @var{char-table}. | 656 values stored in @var{char-table}. |
635 @end defun | 657 @end defun |
636 | 658 |
637 @node Bool-Vectors | 659 @node Bool-Vectors |
638 @section Bool-vectors | 660 @section Bool-vectors |
639 @cindex Bool-vectors | 661 @cindex Bool-vectors |
640 | 662 |
641 A bool-vector is much like a vector, except that it stores only the | 663 A bool-vector is much like a vector, except that it stores only the |
642 values @code{t} and @code{nil}. If you try to store any non-@code{nil} | 664 values @code{t} and @code{nil}. If you try to store any non-@code{nil} |
643 value into an element of the bool-vector, that actually stores @code{t} | 665 value into an element of the bool-vector, the effect is to store |
644 there. | 666 @code{t} there. As with all arrays, bool-vector indices start from 0, |
667 and the length cannot be changed once the bool-vector is created. | |
668 Bool-vectors are constants when evaluated. | |
645 | 669 |
646 There are two special functions for working with bool-vectors; aside | 670 There are two special functions for working with bool-vectors; aside |
647 from that, you manipulate them with same functions used for other kinds | 671 from that, you manipulate them with same functions used for other kinds |
648 of arrays. | 672 of arrays. |
649 | 673 |