2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../info/sequences
6 @node Sequences Arrays Vectors, Symbols, Lists, Top
7 @chapter Sequences, Arrays, and Vectors
10 Recall that the @dfn{sequence} type is the union of three other Lisp
11 types: lists, vectors, and strings. In other words, any list is a
12 sequence, any vector is a sequence, and any string is a sequence. The
13 common property that all sequences have is that each is an ordered
14 collection of elements.
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
18 length of an existing array cannot be changed. Both strings and vectors
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
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.
25 But it is possible to add elements to the list, or remove elements.
27 The following diagram shows the relationship between these types:
31 ___________________________________
34 | ______ ______________________ |
36 | | List | | Array | |
37 | | | | ________ _______ | |
38 | |______| | | | | | | |
39 | | | String | | Vector| | |
40 | | |________| |_______| | |
41 | |______________________| |
42 |___________________________________|
46 The elements of vectors and lists may be any Lisp objects. The
47 elements of strings are all characters.
50 * Sequence Functions:: Functions that accept any kind of sequence.
51 * Arrays:: Characteristics of arrays in Emacs Lisp.
52 * Array Functions:: Functions specifically for arrays.
53 * Vectors:: Special characteristics of Emacs Lisp vectors.
54 * Vector Functions:: Functions specifically for vectors.
57 @node Sequence Functions
60 In Emacs Lisp, a @dfn{sequence} is either a list, a vector or a
61 string. The common property that all sequences have is that each is an
62 ordered collection of elements. This section describes functions that
63 accept any kind of sequence.
65 @defun sequencep object
66 Returns @code{t} if @var{object} is a list, vector, or
67 string, @code{nil} otherwise.
70 @defun copy-sequence sequence
71 @cindex copying sequences
72 Returns a copy of @var{sequence}. The copy is the same type of object
73 as the original sequence, and it has the same elements in the same order.
75 Storing a new element into the copy does not affect the original
76 @var{sequence}, and vice versa. However, the elements of the new
77 sequence are not copies; they are identical (@code{eq}) to the elements
78 of the original. Therefore, changes made within these elements, as
79 found via the copied sequence, are also visible in the original
82 If the sequence is a string with text properties, the property list in
83 the copy is itself a copy, not shared with the original's property
84 list. However, the actual values of the properties are shared.
85 @xref{Text Properties}.
87 See also @code{append} in @ref{Building Lists}, @code{concat} in
88 @ref{Creating Strings}, and @code{vconcat} in @ref{Vectors}, for others
89 ways to copy sequences.
97 (setq x (vector 'foo bar))
101 (setq y (copy-sequence x))
102 @result{} [foo (1 2)]
114 (eq (elt x 1) (elt y 1))
119 ;; @r{Replacing an element of one sequence.}
121 x @result{} [quux (1 2)]
122 y @result{} [foo (1 2)]
126 ;; @r{Modifying the inside of a shared element.}
127 (setcar (aref x 1) 69)
128 x @result{} [quux (69 2)]
129 y @result{} [foo (69 2)]
134 @defun length sequence
135 @cindex string length
137 @cindex vector length
138 @cindex sequence length
139 Returns the number of elements in @var{sequence}. If @var{sequence} is
140 a cons cell that is not a list (because the final @sc{cdr} is not
141 @code{nil}), a @code{wrong-type-argument} error is signaled.
163 @defun elt sequence index
164 @cindex elements of sequences
165 This function returns the element of @var{sequence} indexed by
166 @var{index}. Legitimate values of @var{index} are integers ranging from
167 0 up to one less than the length of @var{sequence}. If @var{sequence}
168 is a list, then out-of-range values of @var{index} return @code{nil};
169 otherwise, they trigger an @code{args-out-of-range} error.
181 (char-to-string (elt "1234" 2))
186 @error{}Args out of range: [1 2 3 4], 4
190 @error{}Args out of range: [1 2 3 4], -1
194 This function duplicates @code{aref} (@pxref{Array Functions}) and
195 @code{nth} (@pxref{List Elements}), except that it works for any kind of
203 An @dfn{array} object has slots that hold a number of other Lisp
204 objects, called the elements of the array. Any element of an array may
205 be accessed in constant time. In contrast, an element of a list
206 requires access time that is proportional to the position of the element
209 When you create an array, you must specify how many elements it has.
210 The amount of space allocated depends on the number of elements.
211 Therefore, it is impossible to change the size of an array once it is
212 created; you cannot add or remove elements. However, you can replace an
213 element with a different value.
215 Emacs defines two types of array, both of which are one-dimensional:
216 @dfn{strings} and @dfn{vectors}. A vector is a general array; its
217 elements can be any Lisp objects. A string is a specialized array; its
218 elements must be characters (i.e., integers between 0 and 255). Each
219 type of array has its own read syntax. @xref{String Type}, and
222 Both kinds of array share these characteristics:
226 The first element of an array has index zero, the second element has
227 index 1, and so on. This is called @dfn{zero-origin} indexing. For
228 example, an array of four elements has indices 0, 1, 2, @w{and 3}.
231 The elements of an array may be referenced or changed with the functions
232 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
235 In principle, if you wish to have an array of characters, you could use
236 either a string or a vector. In practice, we always choose strings for
237 such applications, for four reasons:
241 They occupy one-fourth the space of a vector of the same elements.
244 Strings are printed in a way that shows the contents more clearly
248 Strings can hold text properties. @xref{Text Properties}.
251 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
253 buffer the way you can insert a string. @xref{Strings and Characters}.
256 @node Array Functions
257 @section Functions that Operate on Arrays
259 In this section, we describe the functions that accept both strings
263 This function returns @code{t} if @var{object} is an array (i.e., either a
276 @defun aref array index
277 @cindex array elements
278 This function returns the @var{index}th element of @var{array}. The
279 first element is at index zero.
283 (setq primes [2 3 5 7 11 13])
284 @result{} [2 3 5 7 11 13]
293 @result{} 98 ; @r{@samp{b} is @sc{ASCII} code 98.}
297 See also the function @code{elt}, in @ref{Sequence Functions}.
300 @defun aset array index object
301 This function sets the @var{index}th element of @var{array} to be
302 @var{object}. It returns @var{object}.
306 (setq w [foo bar baz])
307 @result{} [foo bar baz]
311 @result{} [fu bar baz]
324 If @var{array} is a string and @var{object} is not a character, a
325 @code{wrong-type-argument} error results.
328 @defun fillarray array object
329 This function fills the array @var{array} with @var{object}, so that
330 each element of @var{array} is @var{object}. It returns @var{array}.
334 (setq a [a b c d e f g])
335 @result{} [a b c d e f g]
337 @result{} [0 0 0 0 0 0 0]
339 @result{} [0 0 0 0 0 0 0]
342 (setq s "When in the course")
343 @result{} "When in the course"
345 @result{} "------------------"
349 If @var{array} is a string and @var{object} is not a character, a
350 @code{wrong-type-argument} error results.
353 The general sequence functions @code{copy-sequence} and @code{length}
354 are often useful for objects known to be arrays. @xref{Sequence Functions}.
360 Arrays in Lisp, like arrays in most languages, are blocks of memory
361 whose elements can be accessed in constant time. A @dfn{vector} is a
362 general-purpose array; its elements can be any Lisp objects. (The other
363 kind of array in Emacs Lisp is the @dfn{string}, whose elements must be
364 characters.) Vectors in Emacs serve as syntax tables (vectors of
365 integers), as obarrays (vectors of symbols), and in keymaps (vectors of
366 commands). They are also used internally as part of the representation
367 of a byte-compiled function; if you print such a function, you will see
370 In Emacs Lisp, the indices of the elements of a vector start from zero
371 and count up from there.
373 Vectors are printed with square brackets surrounding the elements.
374 Thus, a vector whose elements are the symbols @code{a}, @code{b} and
375 @code{a} is printed as @code{[a b a]}. You can write vectors in the
376 same way in Lisp input.
378 A vector, like a string or a number, is considered a constant for
379 evaluation: the result of evaluating it is the same vector. This does
380 not evaluate or even examine the elements of the vector.
381 @xref{Self-Evaluating Forms}.
383 Here are examples of these principles:
387 (setq avector [1 two '(three) "four" [five]])
388 @result{} [1 two (quote (three)) "four" [five]]
390 @result{} [1 two (quote (three)) "four" [five]]
391 (eq avector (eval avector))
396 @node Vector Functions
397 @section Functions That Operate on Vectors
399 Here are some functions that relate to vectors:
401 @defun vectorp object
402 This function returns @code{t} if @var{object} is a vector.
414 @defun vector &rest objects
415 This function creates and returns a vector whose elements are the
416 arguments, @var{objects}.
420 (vector 'foo 23 [bar baz] "rats")
421 @result{} [foo 23 [bar baz] "rats"]
428 @defun make-vector length object
429 This function returns a new vector consisting of @var{length} elements,
430 each initialized to @var{object}.
434 (setq sleepy (make-vector 9 'Z))
435 @result{} [Z Z Z Z Z Z Z Z Z]
440 @defun vconcat &rest sequences
441 @cindex copying vectors
442 This function returns a new vector containing all the elements of the
443 @var{sequences}. The arguments @var{sequences} may be lists, vectors,
444 or strings. If no @var{sequences} are given, an empty vector is
447 The value is a newly constructed vector that is not @code{eq} to any
452 (setq a (vconcat '(A B C) '(D E F)))
453 @result{} [A B C D E F]
460 (vconcat [A B C] "aa" '(foo (6 7)))
461 @result{} [A B C 97 97 foo (6 7)]
465 When an argument is an integer (not a sequence of integers), it is
466 converted to a string of digits making up the decimal printed
467 representation of the integer. This special case exists for
468 compatibility with Mocklisp, and we don't recommend you take advantage
469 of it. If you want to convert an integer to digits in this way, use
470 @code{format} (@pxref{Formatting Strings}) or @code{number-to-string}
471 (@pxref{String Conversion}).
473 For other concatenation functions, see @code{mapconcat} in @ref{Mapping
474 Functions}, @code{concat} in @ref{Creating Strings}, and @code{append}
475 in @ref{Building Lists}.
478 The @code{append} function provides a way to convert a vector into a
479 list with the same elements (@pxref{Building Lists}):
483 (setq avector [1 two (quote (three)) "four" [five]])
484 @result{} [1 two (quote (three)) "four" [five]]
486 @result{} (1 two (quote (three)) "four" [five])