2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999
4 @c Free Software Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
6 @setfilename ../info/sequences
7 @node Sequences Arrays Vectors, Hash Tables, Lists, Top
8 @chapter Sequences, Arrays, and Vectors
11 Recall that the @dfn{sequence} type is the union of two other Lisp
12 types: lists and arrays. In other words, any list is a sequence, and
13 any array is a sequence. The common property that all sequences have is
14 that each is an ordered collection of elements.
16 An @dfn{array} is a single primitive object that has a slot for each
17 of its elements. All the elements are accessible in constant time, but
18 the length of an existing array cannot be changed. Strings, vectors,
19 char-tables and bool-vectors are the four types of arrays.
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 | | | Vector | | String | | |
40 | | |________| |________| | |
41 | | ____________ _____________ | |
43 | | | Char-table | | Bool-vector | | |
44 | | |____________| |_____________| | |
45 | |________________________________| |
46 |_____________________________________________|
50 The elements of vectors and lists may be any Lisp objects. The
51 elements of strings are all characters.
54 * Sequence Functions:: Functions that accept any kind of sequence.
55 * Arrays:: Characteristics of arrays in Emacs Lisp.
56 * Array Functions:: Functions specifically for arrays.
57 * Vectors:: Special characteristics of Emacs Lisp vectors.
58 * Vector Functions:: Functions specifically for vectors.
59 * Char-Tables:: How to work with char-tables.
60 * Bool-Vectors:: How to work with bool-vectors.
63 @node Sequence Functions
66 In Emacs Lisp, a @dfn{sequence} is either a list or an array. The
67 common property of all sequences is that they are ordered collections of
68 elements. This section describes functions that accept any kind of
71 @defun sequencep object
72 Returns @code{t} if @var{object} is a list, vector, or
73 string, @code{nil} otherwise.
76 @defun length sequence
80 @cindex sequence length
81 This function returns the number of elements in @var{sequence}. If
82 @var{sequence} is a cons cell that is not a list (because the final
83 @sc{cdr} is not @code{nil}), a @code{wrong-type-argument} error is
86 @xref{List Elements}, for the related function @code{safe-length}.
106 (length (make-bool-vector 5 nil))
112 @defun elt sequence index
113 @cindex elements of sequences
114 This function returns the element of @var{sequence} indexed by
115 @var{index}. Legitimate values of @var{index} are integers ranging from
116 0 up to one less than the length of @var{sequence}. If @var{sequence}
117 is a list, then out-of-range values of @var{index} return @code{nil};
118 otherwise, they trigger an @code{args-out-of-range} error.
130 ;; @r{We use @code{string} to show clearly which character @code{elt} returns.}
131 (string (elt "1234" 2))
136 @error{} Args out of range: [1 2 3 4], 4
140 @error{} Args out of range: [1 2 3 4], -1
144 This function generalizes @code{aref} (@pxref{Array Functions}) and
145 @code{nth} (@pxref{List Elements}).
148 @defun copy-sequence sequence
149 @cindex copying sequences
150 Returns a copy of @var{sequence}. The copy is the same type of object
151 as the original sequence, and it has the same elements in the same order.
153 Storing a new element into the copy does not affect the original
154 @var{sequence}, and vice versa. However, the elements of the new
155 sequence are not copies; they are identical (@code{eq}) to the elements
156 of the original. Therefore, changes made within these elements, as
157 found via the copied sequence, are also visible in the original
160 If the sequence is a string with text properties, the property list in
161 the copy is itself a copy, not shared with the original's property
162 list. However, the actual values of the properties are shared.
163 @xref{Text Properties}.
165 See also @code{append} in @ref{Building Lists}, @code{concat} in
166 @ref{Creating Strings}, and @code{vconcat} in @ref{Vectors}, for other
167 ways to copy sequences.
175 (setq x (vector 'foo bar))
176 @result{} [foo (1 2)]
179 (setq y (copy-sequence x))
180 @result{} [foo (1 2)]
192 (eq (elt x 1) (elt y 1))
197 ;; @r{Replacing an element of one sequence.}
199 x @result{} [quux (1 2)]
200 y @result{} [foo (1 2)]
204 ;; @r{Modifying the inside of a shared element.}
205 (setcar (aref x 1) 69)
206 x @result{} [quux (69 2)]
207 y @result{} [foo (69 2)]
216 An @dfn{array} object has slots that hold a number of other Lisp
217 objects, called the elements of the array. Any element of an array may
218 be accessed in constant time. In contrast, an element of a list
219 requires access time that is proportional to the position of the element
222 Emacs defines four types of array, all one-dimensional: @dfn{strings},
223 @dfn{vectors}, @dfn{bool-vectors} and @dfn{char-tables}. A vector is a
224 general array; its elements can be any Lisp objects. A string is a
225 specialized array; its elements must be characters. Each type of array
226 has its own read syntax.
227 @xref{String Type}, and @ref{Vector Type}.
229 All four kinds of array share these characteristics:
233 The first element of an array has index zero, the second element has
234 index 1, and so on. This is called @dfn{zero-origin} indexing. For
235 example, an array of four elements has indices 0, 1, 2, @w{and 3}.
238 The length of the array is fixed once you create it; you cannot
239 change the length of an existing array.
242 The array is a constant, for evaluation---in other words, it evaluates
246 The elements of an array may be referenced or changed with the functions
247 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
250 When you create an array, other than a char-table, you must specify
251 its length. You cannot specify the length of a char-table, because that
252 is determined by the range of character codes.
254 In principle, if you want an array of text characters, you could use
255 either a string or a vector. In practice, we always choose strings for
256 such applications, for four reasons:
260 They occupy one-fourth the space of a vector of the same elements.
263 Strings are printed in a way that shows the contents more clearly
267 Strings can hold text properties. @xref{Text Properties}.
270 Many of the specialized editing and I/O facilities of Emacs accept only
271 strings. For example, you cannot insert a vector of characters into a
272 buffer the way you can insert a string. @xref{Strings and Characters}.
275 By contrast, for an array of keyboard input characters (such as a key
276 sequence), a vector may be necessary, because many keyboard input
277 characters are outside the range that will fit in a string. @xref{Key
280 @node Array Functions
281 @section Functions that Operate on Arrays
283 In this section, we describe the functions that accept all types of
287 This function returns @code{t} if @var{object} is an array (i.e., a
288 vector, a string, a bool-vector or a char-table).
296 (arrayp (syntax-table)) ;; @r{A char-table.}
302 @defun aref array index
303 @cindex array elements
304 This function returns the @var{index}th element of @var{array}. The
305 first element is at index zero.
309 (setq primes [2 3 5 7 11 13])
310 @result{} [2 3 5 7 11 13]
316 @result{} 98 ; @r{@samp{b} is @sc{ascii} code 98.}
320 See also the function @code{elt}, in @ref{Sequence Functions}.
323 @defun aset array index object
324 This function sets the @var{index}th element of @var{array} to be
325 @var{object}. It returns @var{object}.
329 (setq w [foo bar baz])
330 @result{} [foo bar baz]
334 @result{} [fu bar baz]
347 If @var{array} is a string and @var{object} is not a character, a
348 @code{wrong-type-argument} error results. The function converts a
349 unibyte string to multibyte if necessary to insert a character.
352 @defun fillarray array object
353 This function fills the array @var{array} with @var{object}, so that
354 each element of @var{array} is @var{object}. It returns @var{array}.
358 (setq a [a b c d e f g])
359 @result{} [a b c d e f g]
361 @result{} [0 0 0 0 0 0 0]
363 @result{} [0 0 0 0 0 0 0]
366 (setq s "When in the course")
367 @result{} "When in the course"
369 @result{} "------------------"
373 If @var{array} is a string and @var{object} is not a character, a
374 @code{wrong-type-argument} error results.
377 The general sequence functions @code{copy-sequence} and @code{length}
378 are often useful for objects known to be arrays. @xref{Sequence Functions}.
384 Arrays in Lisp, like arrays in most languages, are blocks of memory
385 whose elements can be accessed in constant time. A @dfn{vector} is a
386 general-purpose array of specified length; its elements can be any Lisp
387 objects. (By contrast, a string can hold only characters as elements.)
388 Vectors in Emacs are used for obarrays (vectors of symbols), and as part
389 of keymaps (vectors of commands). They are also used internally as part
390 of the representation of a byte-compiled function; if you print such a
391 function, you will see a vector in it.
393 In Emacs Lisp, the indices of the elements of a vector start from zero
394 and count up from there.
396 Vectors are printed with square brackets surrounding the elements.
397 Thus, a vector whose elements are the symbols @code{a}, @code{b} and
398 @code{a} is printed as @code{[a b a]}. You can write vectors in the
399 same way in Lisp input.
401 A vector, like a string or a number, is considered a constant for
402 evaluation: the result of evaluating it is the same vector. This does
403 not evaluate or even examine the elements of the vector.
404 @xref{Self-Evaluating Forms}.
406 Here are examples illustrating these principles:
410 (setq avector [1 two '(three) "four" [five]])
411 @result{} [1 two (quote (three)) "four" [five]]
413 @result{} [1 two (quote (three)) "four" [five]]
414 (eq avector (eval avector))
419 @node Vector Functions
420 @section Functions for Vectors
422 Here are some functions that relate to vectors:
424 @defun vectorp object
425 This function returns @code{t} if @var{object} is a vector.
437 @defun vector &rest objects
438 This function creates and returns a vector whose elements are the
439 arguments, @var{objects}.
443 (vector 'foo 23 [bar baz] "rats")
444 @result{} [foo 23 [bar baz] "rats"]
451 @defun make-vector length object
452 This function returns a new vector consisting of @var{length} elements,
453 each initialized to @var{object}.
457 (setq sleepy (make-vector 9 'Z))
458 @result{} [Z Z Z Z Z Z Z Z Z]
463 @defun vconcat &rest sequences
464 @cindex copying vectors
465 This function returns a new vector containing all the elements of the
466 @var{sequences}. The arguments @var{sequences} may be any kind of
467 arrays, including lists, vectors, or strings. If no @var{sequences} are
468 given, an empty vector is returned.
470 The value is a newly constructed vector that is not @code{eq} to any
475 (setq a (vconcat '(A B C) '(D E F)))
476 @result{} [A B C D E F]
483 (vconcat [A B C] "aa" '(foo (6 7)))
484 @result{} [A B C 97 97 foo (6 7)]
488 The @code{vconcat} function also allows byte-code function objects as
489 arguments. This is a special feature to make it easy to access the entire
490 contents of a byte-code function object. @xref{Byte-Code Objects}.
492 The @code{vconcat} function also allows integers as arguments. It
493 converts them to strings of digits, making up the decimal print
494 representation of the integer, and then uses the strings instead of the
495 original integers. @strong{Don't use this feature; we plan to eliminate
496 it. If you already use this feature, change your programs now!} The
497 proper way to convert an integer to a decimal number in this way is with
498 @code{format} (@pxref{Formatting Strings}) or @code{number-to-string}
499 (@pxref{String Conversion}).
501 For other concatenation functions, see @code{mapconcat} in @ref{Mapping
502 Functions}, @code{concat} in @ref{Creating Strings}, and @code{append}
503 in @ref{Building Lists}.
506 The @code{append} function provides a way to convert a vector into a
507 list with the same elements (@pxref{Building Lists}):
511 (setq avector [1 two (quote (three)) "four" [five]])
512 @result{} [1 two (quote (three)) "four" [five]]
514 @result{} (1 two (quote (three)) "four" [five])
521 @cindex extra slots of char-table
523 A char-table is much like a vector, except that it is indexed by
524 character codes. Any valid character code, without modifiers, can be
525 used as an index in a char-table. You can access a char-table's
526 elements with @code{aref} and @code{aset}, as with any array. In
527 addition, a char-table can have @dfn{extra slots} to hold additional
528 data not associated with particular character codes. Char-tables are
529 constants when evaluated.
531 @cindex subtype of char-table
532 Each char-table has a @dfn{subtype} which is a symbol. The subtype
533 has two purposes: to distinguish char-tables meant for different uses,
534 and to control the number of extra slots. For example, display tables
535 are char-tables with @code{display-table} as the subtype, and syntax
536 tables are char-tables with @code{syntax-table} as the subtype. A valid
537 subtype must have a @code{char-table-extra-slots} property which is an
538 integer between 0 and 10. This integer specifies the number of
539 @dfn{extra slots} in the char-table.
541 @cindex parent of char-table
542 A char-table can have a @dfn{parent}. which is another char-table. If
543 it does, then whenever the char-table specifies @code{nil} for a
544 particular character @var{c}, it inherits the value specified in the
545 parent. In other words, @code{(aref @var{char-table} @var{c})} returns
546 the value from the parent of @var{char-table} if @var{char-table} itself
547 specifies @code{nil}.
549 @cindex default value of char-table
550 A char-table can also have a @dfn{default value}. If so, then
551 @code{(aref @var{char-table} @var{c})} returns the default value
552 whenever the char-table does not specify any other non-@code{nil} value.
554 @defun make-char-table subtype &optional init
555 Return a newly created char-table, with subtype @var{subtype}. Each
556 element is initialized to @var{init}, which defaults to @code{nil}. You
557 cannot alter the subtype of a char-table after the char-table is
560 There is no argument to specify the length of the char-table, because
561 all char-tables have room for any valid character code as an index.
564 @defun char-table-p object
565 This function returns @code{t} if @var{object} is a char-table,
566 otherwise @code{nil}.
569 @defun char-table-subtype char-table
570 This function returns the subtype symbol of @var{char-table}.
573 @defun set-char-table-default char-table new-default
574 This function sets the default value of @var{char-table} to
577 There is no special function to access the default value of a char-table.
578 To do that, use @code{(char-table-range @var{char-table} nil)}.
581 @defun char-table-parent char-table
582 This function returns the parent of @var{char-table}. The parent is
583 always either @code{nil} or another char-table.
586 @defun set-char-table-parent char-table new-parent
587 This function sets the parent of @var{char-table} to @var{new-parent}.
590 @defun char-table-extra-slot char-table n
591 This function returns the contents of extra slot @var{n} of
592 @var{char-table}. The number of extra slots in a char-table is
593 determined by its subtype.
596 @defun set-char-table-extra-slot char-table n value
597 This function stores @var{value} in extra slot @var{n} of
601 A char-table can specify an element value for a single character code;
602 it can also specify a value for an entire character set.
604 @defun char-table-range char-table range
605 This returns the value specified in @var{char-table} for a range of
606 characters @var{range}. Here are the possibilities for @var{range}:
610 Refers to the default value.
613 Refers to the element for character @var{char}
614 (supposing @var{char} is a valid character code).
617 Refers to the value specified for the whole character set
618 @var{charset} (@pxref{Character Sets}).
620 @item @var{generic-char}
621 A generic character stands for a character set; specifying the generic
622 character as argument is equivalent to specifying the character set
623 name. @xref{Splitting Characters}, for a description of generic characters.
627 @defun set-char-table-range char-table range value
628 This function sets the value in @var{char-table} for a range of
629 characters @var{range}. Here are the possibilities for @var{range}:
633 Refers to the default value.
636 Refers to the whole range of character codes.
639 Refers to the element for character @var{char}
640 (supposing @var{char} is a valid character code).
643 Refers to the value specified for the whole character set
644 @var{charset} (@pxref{Character Sets}).
646 @item @var{generic-char}
647 A generic character stands for a character set; specifying the generic
648 character as argument is equivalent to specifying the character set
649 name. @xref{Splitting Characters}, for a description of generic characters.
653 @defun map-char-table function char-table
654 This function calls @var{function} for each element of @var{char-table}.
655 @var{function} is called with two arguments, a key and a value. The key
656 is a possible @var{range} argument for @code{char-table-range}---either
657 a valid character or a generic character---and the value is
658 @code{(char-table-range @var{char-table} @var{key})}.
660 Overall, the key-value pairs passed to @var{function} describe all the
661 values stored in @var{char-table}.
663 The return value is always @code{nil}; to make this function useful,
664 @var{function} should have side effects. For example,
665 here is how to examine each element of the syntax table:
670 #'(lambda (key value)
672 (cons (list key value) accumulator)))
676 ((475008 nil) (474880 nil) (474752 nil) (474624 nil)
677 ... (5 (3)) (4 (3)) (3 (3)) (2 (3)) (1 (3)) (0 (3)))
682 @section Bool-vectors
685 A bool-vector is much like a vector, except that it stores only the
686 values @code{t} and @code{nil}. If you try to store any non-@code{nil}
687 value into an element of the bool-vector, the effect is to store
688 @code{t} there. As with all arrays, bool-vector indices start from 0,
689 and the length cannot be changed once the bool-vector is created.
690 Bool-vectors are constants when evaluated.
692 There are two special functions for working with bool-vectors; aside
693 from that, you manipulate them with same functions used for other kinds
696 @defun make-bool-vector length initial
697 Return a new book-vector of @var{length} elements,
698 each one initialized to @var{initial}.
701 @defun bool-vector-p object
702 This returns @code{t} if @var{object} is a bool-vector,
703 and @code{nil} otherwise.