2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2002, 2003,
4 @c 2004, 2005, 2006 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, string,
73 bool-vector, or char-table, @code{nil} otherwise.
76 @defun length sequence
80 @cindex sequence length
81 @cindex char-table length
82 This function returns the number of elements in @var{sequence}. If
83 @var{sequence} is a dotted list, a @code{wrong-type-argument} error is
84 signaled. Circular lists may cause an infinite loop. For a
85 char-table, the value returned is always one more than the maximum
88 @xref{Definition of safe-length}, for the related function @code{safe-length}.
108 (length (make-bool-vector 5 nil))
114 @defun string-bytes string
115 @cindex string, number of bytes
116 This function returns the number of bytes in @var{string}.
117 If @var{string} is a multibyte string, this is greater than
118 @code{(length @var{string})}.
121 @defun elt sequence index
122 @cindex elements of sequences
123 This function returns the element of @var{sequence} indexed by
124 @var{index}. Legitimate values of @var{index} are integers ranging
125 from 0 up to one less than the length of @var{sequence}. If
126 @var{sequence} is a list, out-of-range values behave as for
127 @code{nth}. @xref{Definition of nth}. Otherwise, out-of-range values
128 trigger an @code{args-out-of-range} error.
140 ;; @r{We use @code{string} to show clearly which character @code{elt} returns.}
141 (string (elt "1234" 2))
146 @error{} Args out of range: [1 2 3 4], 4
150 @error{} Args out of range: [1 2 3 4], -1
154 This function generalizes @code{aref} (@pxref{Array Functions}) and
155 @code{nth} (@pxref{Definition of nth}).
158 @defun copy-sequence sequence
159 @cindex copying sequences
160 Returns a copy of @var{sequence}. The copy is the same type of object
161 as the original sequence, and it has the same elements in the same order.
163 Storing a new element into the copy does not affect the original
164 @var{sequence}, and vice versa. However, the elements of the new
165 sequence are not copies; they are identical (@code{eq}) to the elements
166 of the original. Therefore, changes made within these elements, as
167 found via the copied sequence, are also visible in the original
170 If the sequence is a string with text properties, the property list in
171 the copy is itself a copy, not shared with the original's property
172 list. However, the actual values of the properties are shared.
173 @xref{Text Properties}.
175 This function does not work for dotted lists. Trying to copy a
176 circular list may cause an infinite loop.
178 See also @code{append} in @ref{Building Lists}, @code{concat} in
179 @ref{Creating Strings}, and @code{vconcat} in @ref{Vector Functions},
180 for other ways to copy sequences.
188 (setq x (vector 'foo bar))
189 @result{} [foo (1 2)]
192 (setq y (copy-sequence x))
193 @result{} [foo (1 2)]
205 (eq (elt x 1) (elt y 1))
210 ;; @r{Replacing an element of one sequence.}
212 x @result{} [quux (1 2)]
213 y @result{} [foo (1 2)]
217 ;; @r{Modifying the inside of a shared element.}
218 (setcar (aref x 1) 69)
219 x @result{} [quux (69 2)]
220 y @result{} [foo (69 2)]
229 An @dfn{array} object has slots that hold a number of other Lisp
230 objects, called the elements of the array. Any element of an array may
231 be accessed in constant time. In contrast, an element of a list
232 requires access time that is proportional to the position of the element
235 Emacs defines four types of array, all one-dimensional: @dfn{strings},
236 @dfn{vectors}, @dfn{bool-vectors} and @dfn{char-tables}. A vector is a
237 general array; its elements can be any Lisp objects. A string is a
238 specialized array; its elements must be characters. Each type of array
239 has its own read syntax.
240 @xref{String Type}, and @ref{Vector Type}.
242 All four kinds of array share these characteristics:
246 The first element of an array has index zero, the second element has
247 index 1, and so on. This is called @dfn{zero-origin} indexing. For
248 example, an array of four elements has indices 0, 1, 2, @w{and 3}.
251 The length of the array is fixed once you create it; you cannot
252 change the length of an existing array.
255 For purposes of evaluation, the array is a constant---in other words,
256 it evaluates to itself.
259 The elements of an array may be referenced or changed with the functions
260 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
263 When you create an array, other than a char-table, you must specify
264 its length. You cannot specify the length of a char-table, because that
265 is determined by the range of character codes.
267 In principle, if you want an array of text characters, you could use
268 either a string or a vector. In practice, we always choose strings for
269 such applications, for four reasons:
273 They occupy one-fourth the space of a vector of the same elements.
276 Strings are printed in a way that shows the contents more clearly
280 Strings can hold text properties. @xref{Text Properties}.
283 Many of the specialized editing and I/O facilities of Emacs accept only
284 strings. For example, you cannot insert a vector of characters into a
285 buffer the way you can insert a string. @xref{Strings and Characters}.
288 By contrast, for an array of keyboard input characters (such as a key
289 sequence), a vector may be necessary, because many keyboard input
290 characters are outside the range that will fit in a string. @xref{Key
293 @node Array Functions
294 @section Functions that Operate on Arrays
296 In this section, we describe the functions that accept all types of
300 This function returns @code{t} if @var{object} is an array (i.e., a
301 vector, a string, a bool-vector or a char-table).
309 (arrayp (syntax-table)) ;; @r{A char-table.}
315 @defun aref array index
316 @cindex array elements
317 This function returns the @var{index}th element of @var{array}. The
318 first element is at index zero.
322 (setq primes [2 3 5 7 11 13])
323 @result{} [2 3 5 7 11 13]
329 @result{} 98 ; @r{@samp{b} is @acronym{ASCII} code 98.}
333 See also the function @code{elt}, in @ref{Sequence Functions}.
336 @defun aset array index object
337 This function sets the @var{index}th element of @var{array} to be
338 @var{object}. It returns @var{object}.
342 (setq w [foo bar baz])
343 @result{} [foo bar baz]
347 @result{} [fu bar baz]
360 If @var{array} is a string and @var{object} is not a character, a
361 @code{wrong-type-argument} error results. The function converts a
362 unibyte string to multibyte if necessary to insert a character.
365 @defun fillarray array object
366 This function fills the array @var{array} with @var{object}, so that
367 each element of @var{array} is @var{object}. It returns @var{array}.
371 (setq a [a b c d e f g])
372 @result{} [a b c d e f g]
374 @result{} [0 0 0 0 0 0 0]
376 @result{} [0 0 0 0 0 0 0]
379 (setq s "When in the course")
380 @result{} "When in the course"
382 @result{} "------------------"
386 If @var{array} is a string and @var{object} is not a character, a
387 @code{wrong-type-argument} error results.
390 The general sequence functions @code{copy-sequence} and @code{length}
391 are often useful for objects known to be arrays. @xref{Sequence Functions}.
397 Arrays in Lisp, like arrays in most languages, are blocks of memory
398 whose elements can be accessed in constant time. A @dfn{vector} is a
399 general-purpose array of specified length; its elements can be any Lisp
400 objects. (By contrast, a string can hold only characters as elements.)
401 Vectors in Emacs are used for obarrays (vectors of symbols), and as part
402 of keymaps (vectors of commands). They are also used internally as part
403 of the representation of a byte-compiled function; if you print such a
404 function, you will see a vector in it.
406 In Emacs Lisp, the indices of the elements of a vector start from zero
407 and count up from there.
409 Vectors are printed with square brackets surrounding the elements.
410 Thus, a vector whose elements are the symbols @code{a}, @code{b} and
411 @code{a} is printed as @code{[a b a]}. You can write vectors in the
412 same way in Lisp input.
414 A vector, like a string or a number, is considered a constant for
415 evaluation: the result of evaluating it is the same vector. This does
416 not evaluate or even examine the elements of the vector.
417 @xref{Self-Evaluating Forms}.
419 Here are examples illustrating these principles:
423 (setq avector [1 two '(three) "four" [five]])
424 @result{} [1 two (quote (three)) "four" [five]]
426 @result{} [1 two (quote (three)) "four" [five]]
427 (eq avector (eval avector))
432 @node Vector Functions
433 @section Functions for Vectors
435 Here are some functions that relate to vectors:
437 @defun vectorp object
438 This function returns @code{t} if @var{object} is a vector.
450 @defun vector &rest objects
451 This function creates and returns a vector whose elements are the
452 arguments, @var{objects}.
456 (vector 'foo 23 [bar baz] "rats")
457 @result{} [foo 23 [bar baz] "rats"]
464 @defun make-vector length object
465 This function returns a new vector consisting of @var{length} elements,
466 each initialized to @var{object}.
470 (setq sleepy (make-vector 9 'Z))
471 @result{} [Z Z Z Z Z Z Z Z Z]
476 @defun vconcat &rest sequences
477 @cindex copying vectors
478 This function returns a new vector containing all the elements of the
479 @var{sequences}. The arguments @var{sequences} may be true lists,
480 vectors, strings or bool-vectors. If no @var{sequences} are given, an
481 empty vector is returned.
483 The value is a newly constructed vector that is not @code{eq} to any
488 (setq a (vconcat '(A B C) '(D E F)))
489 @result{} [A B C D E F]
496 (vconcat [A B C] "aa" '(foo (6 7)))
497 @result{} [A B C 97 97 foo (6 7)]
501 The @code{vconcat} function also allows byte-code function objects as
502 arguments. This is a special feature to make it easy to access the entire
503 contents of a byte-code function object. @xref{Byte-Code Objects}.
505 In Emacs versions before 21, the @code{vconcat} function allowed
506 integers as arguments, converting them to strings of digits, but that
507 feature has been eliminated. The proper way to convert an integer to
508 a decimal number in this way is with @code{format} (@pxref{Formatting
509 Strings}) or @code{number-to-string} (@pxref{String Conversion}).
511 For other concatenation functions, see @code{mapconcat} in @ref{Mapping
512 Functions}, @code{concat} in @ref{Creating Strings}, and @code{append}
513 in @ref{Building Lists}.
516 The @code{append} function provides a way to convert a vector into a
517 list with the same elements (@pxref{Building Lists}):
521 (setq avector [1 two (quote (three)) "four" [five]])
522 @result{} [1 two (quote (three)) "four" [five]]
524 @result{} (1 two (quote (three)) "four" [five])
531 @cindex extra slots of char-table
533 A char-table is much like a vector, except that it is indexed by
534 character codes. Any valid character code, without modifiers, can be
535 used as an index in a char-table. You can access a char-table's
536 elements with @code{aref} and @code{aset}, as with any array. In
537 addition, a char-table can have @dfn{extra slots} to hold additional
538 data not associated with particular character codes. Char-tables are
539 constants when evaluated.
541 @cindex subtype of char-table
542 Each char-table has a @dfn{subtype} which is a symbol. The subtype
543 has two purposes: to distinguish char-tables meant for different uses,
544 and to control the number of extra slots. For example, display tables
545 are char-tables with @code{display-table} as the subtype, and syntax
546 tables are char-tables with @code{syntax-table} as the subtype. A valid
547 subtype must have a @code{char-table-extra-slots} property which is an
548 integer between 0 and 10. This integer specifies the number of
549 @dfn{extra slots} in the char-table.
551 @cindex parent of char-table
552 A char-table can have a @dfn{parent}, which is another char-table. If
553 it does, then whenever the char-table specifies @code{nil} for a
554 particular character @var{c}, it inherits the value specified in the
555 parent. In other words, @code{(aref @var{char-table} @var{c})} returns
556 the value from the parent of @var{char-table} if @var{char-table} itself
557 specifies @code{nil}.
559 @cindex default value of char-table
560 A char-table can also have a @dfn{default value}. If so, then
561 @code{(aref @var{char-table} @var{c})} returns the default value
562 whenever the char-table does not specify any other non-@code{nil} value.
564 @defun make-char-table subtype &optional init
565 Return a newly created char-table, with subtype @var{subtype}. Each
566 element is initialized to @var{init}, which defaults to @code{nil}. You
567 cannot alter the subtype of a char-table after the char-table is
570 There is no argument to specify the length of the char-table, because
571 all char-tables have room for any valid character code as an index.
574 @defun char-table-p object
575 This function returns @code{t} if @var{object} is a char-table,
576 otherwise @code{nil}.
579 @defun char-table-subtype char-table
580 This function returns the subtype symbol of @var{char-table}.
583 @defun set-char-table-default char-table char new-default
584 This function sets the default value of generic character @var{char}
585 in @var{char-table} to @var{new-default}.
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).
591 @defun char-table-parent char-table
592 This function returns the parent of @var{char-table}. The parent is
593 always either @code{nil} or another char-table.
596 @defun set-char-table-parent char-table new-parent
597 This function sets the parent of @var{char-table} to @var{new-parent}.
600 @defun char-table-extra-slot char-table n
601 This function returns the contents of extra slot @var{n} of
602 @var{char-table}. The number of extra slots in a char-table is
603 determined by its subtype.
606 @defun set-char-table-extra-slot char-table n value
607 This function stores @var{value} in extra slot @var{n} of
611 A char-table can specify an element value for a single character code;
612 it can also specify a value for an entire character set.
614 @defun char-table-range char-table range
615 This returns the value specified in @var{char-table} for a range of
616 characters @var{range}. Here are the possibilities for @var{range}:
620 Refers to the default value.
623 Refers to the element for character @var{char}
624 (supposing @var{char} is a valid character code).
627 Refers to the value specified for the whole character set
628 @var{charset} (@pxref{Character Sets}).
630 @item @var{generic-char}
631 A generic character stands for a character set, or a row of a
632 character set; specifying the generic character as argument is
633 equivalent to specifying the character set name. @xref{Splitting
634 Characters}, for a description of generic characters.
638 @defun set-char-table-range char-table range value
639 This function sets the value in @var{char-table} for a range of
640 characters @var{range}. Here are the possibilities for @var{range}:
644 Refers to the default value.
647 Refers to the whole range of character codes.
650 Refers to the element for character @var{char}
651 (supposing @var{char} is a valid character code).
654 Refers to the value specified for the whole character set
655 @var{charset} (@pxref{Character Sets}).
657 @item @var{generic-char}
658 A generic character stands for a character set; specifying the generic
659 character as argument is equivalent to specifying the character set
660 name. @xref{Splitting Characters}, for a description of generic characters.
664 @defun map-char-table function char-table
665 This function calls @var{function} for each element of @var{char-table}.
666 @var{function} is called with two arguments, a key and a value. The key
667 is a possible @var{range} argument for @code{char-table-range}---either
668 a valid character or a generic character---and the value is
669 @code{(char-table-range @var{char-table} @var{key})}.
671 Overall, the key-value pairs passed to @var{function} describe all the
672 values stored in @var{char-table}.
674 The return value is always @code{nil}; to make this function useful,
675 @var{function} should have side effects. For example,
676 here is how to examine each element of the syntax table:
681 #'(lambda (key value)
683 (cons (list key value) accumulator)))
687 ((475008 nil) (474880 nil) (474752 nil) (474624 nil)
688 ... (5 (3)) (4 (3)) (3 (3)) (2 (3)) (1 (3)) (0 (3)))
693 @section Bool-vectors
696 A bool-vector is much like a vector, except that it stores only the
697 values @code{t} and @code{nil}. If you try to store any non-@code{nil}
698 value into an element of the bool-vector, the effect is to store
699 @code{t} there. As with all arrays, bool-vector indices start from 0,
700 and the length cannot be changed once the bool-vector is created.
701 Bool-vectors are constants when evaluated.
703 There are two special functions for working with bool-vectors; aside
704 from that, you manipulate them with same functions used for other kinds
707 @defun make-bool-vector length initial
708 Return a new bool-vector of @var{length} elements,
709 each one initialized to @var{initial}.
712 @defun bool-vector-p object
713 This returns @code{t} if @var{object} is a bool-vector,
714 and @code{nil} otherwise.
717 Here is an example of creating, examining, and updating a
718 bool-vector. Note that the printed form represents up to 8 boolean
719 values as a single character.
722 (setq bv (make-bool-vector 5 t))
733 These results make sense because the binary codes for control-_ and
734 control-W are 11111 and 10111, respectively.
737 arch-tag: fcf1084a-cd29-4adc-9f16-68586935b386