2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2001,
4 @c 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 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 fixed-length object with a slot for each of its
17 elements. All the elements are accessible in constant time. The four
18 types of arrays are strings, vectors, char-tables and bool-vectors.
20 A list is a sequence of elements, but it is not a single primitive
21 object; it is made of cons cells, one cell per element. Finding the
22 @var{n}th element requires looking through @var{n} cons cells, so
23 elements farther from the beginning of the list take longer to access.
24 But it is possible to add elements to the list, or remove elements.
26 The following diagram shows the relationship between these types:
30 _____________________________________________
33 | ______ ________________________________ |
35 | | List | | Array | |
36 | | | | ________ ________ | |
37 | |______| | | | | | | |
38 | | | Vector | | String | | |
39 | | |________| |________| | |
40 | | ____________ _____________ | |
42 | | | Char-table | | Bool-vector | | |
43 | | |____________| |_____________| | |
44 | |________________________________| |
45 |_____________________________________________|
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.
55 * Char-Tables:: How to work with char-tables.
56 * Bool-Vectors:: How to work with bool-vectors.
59 @node Sequence Functions
62 In Emacs Lisp, a @dfn{sequence} is either a list or an array. The
63 common property of all sequences is that they are ordered collections of
64 elements. This section describes functions that accept any kind of
67 @defun sequencep object
68 Returns @code{t} if @var{object} is a list, vector, string,
69 bool-vector, or char-table, @code{nil} otherwise.
72 @defun length sequence
76 @cindex sequence length
77 @cindex char-table length
78 This function returns the number of elements in @var{sequence}. If
79 @var{sequence} is a dotted list, a @code{wrong-type-argument} error is
80 signaled. Circular lists may cause an infinite loop. For a
81 char-table, the value returned is always one more than the maximum
84 @xref{Definition of safe-length}, for the related function @code{safe-length}.
104 (length (make-bool-vector 5 nil))
111 See also @code{string-bytes}, in @ref{Text Representations}.
113 @defun elt sequence index
114 @cindex elements of sequences
115 This function returns the element of @var{sequence} indexed by
116 @var{index}. Legitimate values of @var{index} are integers ranging
117 from 0 up to one less than the length of @var{sequence}. If
118 @var{sequence} is a list, out-of-range values behave as for
119 @code{nth}. @xref{Definition of nth}. Otherwise, out-of-range values
120 trigger an @code{args-out-of-range} error.
132 ;; @r{We use @code{string} to show clearly which character @code{elt} returns.}
133 (string (elt "1234" 2))
138 @error{} Args out of range: [1 2 3 4], 4
142 @error{} Args out of range: [1 2 3 4], -1
146 This function generalizes @code{aref} (@pxref{Array Functions}) and
147 @code{nth} (@pxref{Definition of nth}).
150 @defun copy-sequence sequence
151 @cindex copying sequences
152 Returns a copy of @var{sequence}. The copy is the same type of object
153 as the original sequence, and it has the same elements in the same order.
155 Storing a new element into the copy does not affect the original
156 @var{sequence}, and vice versa. However, the elements of the new
157 sequence are not copies; they are identical (@code{eq}) to the elements
158 of the original. Therefore, changes made within these elements, as
159 found via the copied sequence, are also visible in the original
162 If the sequence is a string with text properties, the property list in
163 the copy is itself a copy, not shared with the original's property
164 list. However, the actual values of the properties are shared.
165 @xref{Text Properties}.
167 This function does not work for dotted lists. Trying to copy a
168 circular list may cause an infinite loop.
170 See also @code{append} in @ref{Building Lists}, @code{concat} in
171 @ref{Creating Strings}, and @code{vconcat} in @ref{Vector Functions},
172 for other ways to copy sequences.
180 (setq x (vector 'foo bar))
181 @result{} [foo (1 2)]
184 (setq y (copy-sequence x))
185 @result{} [foo (1 2)]
197 (eq (elt x 1) (elt y 1))
202 ;; @r{Replacing an element of one sequence.}
204 x @result{} [quux (1 2)]
205 y @result{} [foo (1 2)]
209 ;; @r{Modifying the inside of a shared element.}
210 (setcar (aref x 1) 69)
211 x @result{} [quux (69 2)]
212 y @result{} [foo (69 2)]
221 An @dfn{array} object has slots that hold a number of other Lisp
222 objects, called the elements of the array. Any element of an array
223 may be accessed in constant time. In contrast, the time to access an
224 element of a list is proportional to the position of that element in
227 Emacs defines four types of array, all one-dimensional:
228 @dfn{strings} (@pxref{String Type}), @dfn{vectors} (@pxref{Vector
229 Type}), @dfn{bool-vectors} (@pxref{Bool-Vector Type}), and
230 @dfn{char-tables} (@pxref{Char-Table Type}). Vectors and char-tables
231 can hold elements of any type, but strings can only hold characters,
232 and bool-vectors can only hold @code{t} and @code{nil}.
234 All four kinds of array share these characteristics:
238 The first element of an array has index zero, the second element has
239 index 1, and so on. This is called @dfn{zero-origin} indexing. For
240 example, an array of four elements has indices 0, 1, 2, @w{and 3}.
243 The length of the array is fixed once you create it; you cannot
244 change the length of an existing array.
247 For purposes of evaluation, the array is a constant---in other words,
248 it evaluates to itself.
251 The elements of an array may be referenced or changed with the functions
252 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
255 When you create an array, other than a char-table, you must specify
256 its length. You cannot specify the length of a char-table, because that
257 is determined by the range of character codes.
259 In principle, if you want an array of text characters, you could use
260 either a string or a vector. In practice, we always choose strings for
261 such applications, for four reasons:
265 They occupy one-fourth the space of a vector of the same elements.
268 Strings are printed in a way that shows the contents more clearly
272 Strings can hold text properties. @xref{Text Properties}.
275 Many of the specialized editing and I/O facilities of Emacs accept only
276 strings. For example, you cannot insert a vector of characters into a
277 buffer the way you can insert a string. @xref{Strings and Characters}.
280 By contrast, for an array of keyboard input characters (such as a key
281 sequence), a vector may be necessary, because many keyboard input
282 characters are outside the range that will fit in a string. @xref{Key
285 @node Array Functions
286 @section Functions that Operate on Arrays
288 In this section, we describe the functions that accept all types of
292 This function returns @code{t} if @var{object} is an array (i.e., a
293 vector, a string, a bool-vector or a char-table).
301 (arrayp (syntax-table)) ;; @r{A char-table.}
307 @defun aref array index
308 @cindex array elements
309 This function returns the @var{index}th element of @var{array}. The
310 first element is at index zero.
314 (setq primes [2 3 5 7 11 13])
315 @result{} [2 3 5 7 11 13]
321 @result{} 98 ; @r{@samp{b} is @acronym{ASCII} code 98.}
325 See also the function @code{elt}, in @ref{Sequence Functions}.
328 @defun aset array index object
329 This function sets the @var{index}th element of @var{array} to be
330 @var{object}. It returns @var{object}.
334 (setq w [foo bar baz])
335 @result{} [foo bar baz]
339 @result{} [fu bar baz]
352 If @var{array} is a string and @var{object} is not a character, a
353 @code{wrong-type-argument} error results. The function converts a
354 unibyte string to multibyte if necessary to insert a character.
357 @defun fillarray array object
358 This function fills the array @var{array} with @var{object}, so that
359 each element of @var{array} is @var{object}. It returns @var{array}.
363 (setq a [a b c d e f g])
364 @result{} [a b c d e f g]
366 @result{} [0 0 0 0 0 0 0]
368 @result{} [0 0 0 0 0 0 0]
371 (setq s "When in the course")
372 @result{} "When in the course"
374 @result{} "------------------"
378 If @var{array} is a string and @var{object} is not a character, a
379 @code{wrong-type-argument} error results.
382 The general sequence functions @code{copy-sequence} and @code{length}
383 are often useful for objects known to be arrays. @xref{Sequence Functions}.
387 @cindex vector (type)
389 A @dfn{vector} is a general-purpose array whose elements can be any
390 Lisp objects. (By contrast, the elements of a string can only be
391 characters. @xref{Strings and Characters}.) Vectors are used in
392 Emacs for many purposes: as key sequences (@pxref{Key Sequences}), as
393 symbol-lookup tables (@pxref{Creating Symbols}), as part of the
394 representation of a byte-compiled function (@pxref{Byte Compilation}),
397 In Emacs Lisp, the indices of the elements of a vector start from zero
398 and count up from there.
400 Vectors are printed with square brackets surrounding the elements.
401 Thus, a vector whose elements are the symbols @code{a}, @code{b} and
402 @code{a} is printed as @code{[a b a]}. You can write vectors in the
403 same way in Lisp input.
405 A vector, like a string or a number, is considered a constant for
406 evaluation: the result of evaluating it is the same vector. This does
407 not evaluate or even examine the elements of the vector.
408 @xref{Self-Evaluating Forms}.
410 Here are examples illustrating these principles:
414 (setq avector [1 two '(three) "four" [five]])
415 @result{} [1 two (quote (three)) "four" [five]]
417 @result{} [1 two (quote (three)) "four" [five]]
418 (eq avector (eval avector))
423 @node Vector Functions
424 @section Functions for Vectors
426 Here are some functions that relate to vectors:
428 @defun vectorp object
429 This function returns @code{t} if @var{object} is a vector.
441 @defun vector &rest objects
442 This function creates and returns a vector whose elements are the
443 arguments, @var{objects}.
447 (vector 'foo 23 [bar baz] "rats")
448 @result{} [foo 23 [bar baz] "rats"]
455 @defun make-vector length object
456 This function returns a new vector consisting of @var{length} elements,
457 each initialized to @var{object}.
461 (setq sleepy (make-vector 9 'Z))
462 @result{} [Z Z Z Z Z Z Z Z Z]
467 @defun vconcat &rest sequences
468 @cindex copying vectors
469 This function returns a new vector containing all the elements of
470 @var{sequences}. The arguments @var{sequences} may be true lists,
471 vectors, strings or bool-vectors. If no @var{sequences} are given, an
472 empty vector is returned.
474 The value is a newly constructed vector that is not @code{eq} to any
479 (setq a (vconcat '(A B C) '(D E F)))
480 @result{} [A B C D E F]
487 (vconcat [A B C] "aa" '(foo (6 7)))
488 @result{} [A B C 97 97 foo (6 7)]
492 The @code{vconcat} function also allows byte-code function objects as
493 arguments. This is a special feature to make it easy to access the entire
494 contents of a byte-code function object. @xref{Byte-Code Objects}.
496 For other concatenation functions, see @code{mapconcat} in @ref{Mapping
497 Functions}, @code{concat} in @ref{Creating Strings}, and @code{append}
498 in @ref{Building Lists}.
501 The @code{append} function also provides a way to convert a vector into a
502 list with the same elements:
506 (setq avector [1 two (quote (three)) "four" [five]])
507 @result{} [1 two (quote (three)) "four" [five]]
509 @result{} (1 two (quote (three)) "four" [five])
516 @cindex extra slots of char-table
518 A char-table is much like a vector, except that it is indexed by
519 character codes. Any valid character code, without modifiers, can be
520 used as an index in a char-table. You can access a char-table's
521 elements with @code{aref} and @code{aset}, as with any array. In
522 addition, a char-table can have @dfn{extra slots} to hold additional
523 data not associated with particular character codes. Like vectors,
524 char-tables are constants when evaluated, and can hold elements of any
527 @cindex subtype of char-table
528 Each char-table has a @dfn{subtype}, a symbol, which serves two
533 The subtype provides an easy way to tell what the char-table is for.
534 For instance, display tables are char-tables with @code{display-table}
535 as the subtype, and syntax tables are char-tables with
536 @code{syntax-table} as the subtype. The subtype can be queried using
537 the function @code{char-table-subtype}, described below.
540 The subtype controls the number of @dfn{extra slots} in the
541 char-table. This number is specified by the subtype's
542 @code{char-table-extra-slots} symbol property, which should be an
543 integer between 0 and 10. If the subtype has no such symbol property,
544 the char-table has no extra slots. @xref{Property Lists}, for
545 information about symbol properties.
548 @cindex parent of char-table
549 A char-table can have a @dfn{parent}, which is another char-table. If
550 it does, then whenever the char-table specifies @code{nil} for a
551 particular character @var{c}, it inherits the value specified in the
552 parent. In other words, @code{(aref @var{char-table} @var{c})} returns
553 the value from the parent of @var{char-table} if @var{char-table} itself
554 specifies @code{nil}.
556 @cindex default value of char-table
557 A char-table can also have a @dfn{default value}. If so, then
558 @code{(aref @var{char-table} @var{c})} returns the default value
559 whenever the char-table does not specify any other non-@code{nil} value.
561 @defun make-char-table subtype &optional init
562 Return a newly-created char-table, with subtype @var{subtype} (a
563 symbol). Each element is initialized to @var{init}, which defaults to
564 @code{nil}. You cannot alter the subtype of a char-table after the
565 char-table is created.
567 There is no argument to specify the length of the char-table, because
568 all char-tables have room for any valid character code as an index.
570 If @var{subtype} has the @code{char-table-extra-slots} symbol
571 property, that specifies the number of extra slots in the char-table.
572 This should be an integer between 0 and 10; otherwise,
573 @code{make-char-table} raises an error. If @var{subtype} has no
574 @code{char-table-extra-slots} symbol property (@pxref{Property
575 Lists}), the char-table has no extra slots.
578 @defun char-table-p object
579 This function returns @code{t} if @var{object} is a char-table, and
580 @code{nil} otherwise.
583 @defun char-table-subtype char-table
584 This function returns the subtype symbol of @var{char-table}.
587 There is no special function to access default values in a char-table.
588 To do that, use @code{char-table-range} (see below).
590 @defun char-table-parent char-table
591 This function returns the parent of @var{char-table}. The parent is
592 always either @code{nil} or another char-table.
595 @defun set-char-table-parent char-table new-parent
596 This function sets the parent of @var{char-table} to @var{new-parent}.
599 @defun char-table-extra-slot char-table n
600 This function returns the contents of extra slot @var{n} of
601 @var{char-table}. The number of extra slots in a char-table is
602 determined by its subtype.
605 @defun set-char-table-extra-slot char-table n value
606 This function stores @var{value} in extra slot @var{n} of
610 A char-table can specify an element value for a single character code;
611 it can also specify a value for an entire character set.
613 @defun char-table-range char-table range
614 This returns the value specified in @var{char-table} for a range of
615 characters @var{range}. Here are the possibilities for @var{range}:
619 Refers to the default value.
622 Refers to the element for character @var{char}
623 (supposing @var{char} is a valid character code).
625 @item @code{(@var{from} . @var{to})}
626 A cons cell refers to all the characters in the inclusive range
627 @samp{[@var{from}..@var{to}]}.
631 @defun set-char-table-range char-table range value
632 This function sets the value in @var{char-table} for a range of
633 characters @var{range}. Here are the possibilities for @var{range}:
637 Refers to the default value.
640 Refers to the whole range of character codes.
643 Refers to the element for character @var{char}
644 (supposing @var{char} is a valid character code).
646 @item @code{(@var{from} . @var{to})}
647 A cons cell refers to all the characters in the inclusive range
648 @samp{[@var{from}..@var{to}]}.
652 @defun map-char-table function char-table
653 This function calls its argument @var{function} for each element of
654 @var{char-table} that has a non-@code{nil} value. The call to
655 @var{function} is 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 cons cell @code{(@var{from} . @var{to})},
658 specifying a range of characters that share the same value. The value is
659 what @code{(char-table-range @var{char-table} @var{key})} returns.
661 Overall, the key-value pairs passed to @var{function} describe all the
662 values stored in @var{char-table}.
664 The return value is always @code{nil}; to make calls to
665 @code{map-char-table} useful, @var{function} should have side effects.
666 For example, here is how to examine the elements of the syntax table:
671 #'(lambda (key value)
675 (list (car key) (cdr key))
682 (((2597602 4194303) (2)) ((2597523 2597601) (3))
683 ... (65379 (5 . 65378)) (65378 (4 . 65379)) (65377 (1))
684 ... (12 (0)) (11 (3)) (10 (12)) (9 (0)) ((0 8) (3)))
689 @section Bool-vectors
692 A bool-vector is much like a vector, except that it stores only the
693 values @code{t} and @code{nil}. If you try to store any non-@code{nil}
694 value into an element of the bool-vector, the effect is to store
695 @code{t} there. As with all arrays, bool-vector indices start from 0,
696 and the length cannot be changed once the bool-vector is created.
697 Bool-vectors are constants when evaluated.
699 There are two special functions for working with bool-vectors; aside
700 from that, you manipulate them with same functions used for other kinds
703 @defun make-bool-vector length initial
704 Return a new bool-vector of @var{length} elements,
705 each one initialized to @var{initial}.
708 @defun bool-vector-p object
709 This returns @code{t} if @var{object} is a bool-vector,
710 and @code{nil} otherwise.
713 Here is an example of creating, examining, and updating a
714 bool-vector. Note that the printed form represents up to 8 boolean
715 values as a single character.
718 (setq bv (make-bool-vector 5 t))
729 These results make sense because the binary codes for control-_ and
730 control-W are 11111 and 10111, respectively.
733 arch-tag: fcf1084a-cd29-4adc-9f16-68586935b386