2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2014 Free Software
5 @c See the file elisp.texi for copying conditions.
6 @node Sequences Arrays Vectors
7 @chapter Sequences, Arrays, and Vectors
10 The @dfn{sequence} type is the union of two other Lisp types: lists
11 and arrays. In other words, any list is a sequence, and any array is
12 a sequence. The common property that all sequences have is that each
13 is an ordered collection of elements.
15 An @dfn{array} is a fixed-length object with a slot for each of its
16 elements. All the elements are accessible in constant time. The four
17 types of arrays are strings, vectors, char-tables and bool-vectors.
19 A list is a sequence of elements, but it is not a single primitive
20 object; it is made of cons cells, one cell per element. Finding the
21 @var{n}th element requires looking through @var{n} cons cells, so
22 elements farther from the beginning of the list take longer to access.
23 But it is possible to add elements to the list, or remove elements.
25 The following diagram shows the relationship between these types:
29 _____________________________________________
32 | ______ ________________________________ |
34 | | List | | Array | |
35 | | | | ________ ________ | |
36 | |______| | | | | | | |
37 | | | Vector | | String | | |
38 | | |________| |________| | |
39 | | ____________ _____________ | |
41 | | | Char-table | | Bool-vector | | |
42 | | |____________| |_____________| | |
43 | |________________________________| |
44 |_____________________________________________|
49 * Sequence Functions:: Functions that accept any kind of sequence.
50 * Arrays:: Characteristics of arrays in Emacs Lisp.
51 * Array Functions:: Functions specifically for arrays.
52 * Vectors:: Special characteristics of Emacs Lisp vectors.
53 * Vector Functions:: Functions specifically for vectors.
54 * Char-Tables:: How to work with char-tables.
55 * Bool-Vectors:: How to work with bool-vectors.
56 * Rings:: Managing a fixed-size ring of objects.
59 @node Sequence Functions
62 This section describes functions that accept any kind of sequence.
64 @defun sequencep object
65 This function returns @code{t} if @var{object} is a list, vector,
66 string, bool-vector, or char-table, @code{nil} otherwise.
69 @defun length sequence
73 @cindex sequence length
74 @cindex char-table length
75 This function returns the number of elements in @var{sequence}. If
76 @var{sequence} is a dotted list, a @code{wrong-type-argument} error is
77 signaled. Circular lists may cause an infinite loop. For a
78 char-table, the value returned is always one more than the maximum
81 @xref{Definition of safe-length}, for the related function @code{safe-length}.
101 (length (make-bool-vector 5 nil))
108 See also @code{string-bytes}, in @ref{Text Representations}.
110 If you need to compute the width of a string on display, you should use
111 @code{string-width} (@pxref{Size of Displayed Text}), not @code{length},
112 since @code{length} only counts the number of characters, but does not
113 account for the display width of each character.
115 @defun elt sequence index
116 @cindex elements of sequences
117 This function returns the element of @var{sequence} indexed by
118 @var{index}. Legitimate values of @var{index} are integers ranging
119 from 0 up to one less than the length of @var{sequence}. If
120 @var{sequence} is a list, out-of-range values behave as for
121 @code{nth}. @xref{Definition of nth}. Otherwise, out-of-range values
122 trigger an @code{args-out-of-range} error.
134 ;; @r{We use @code{string} to show clearly which character @code{elt} returns.}
135 (string (elt "1234" 2))
140 @error{} Args out of range: [1 2 3 4], 4
144 @error{} Args out of range: [1 2 3 4], -1
148 This function generalizes @code{aref} (@pxref{Array Functions}) and
149 @code{nth} (@pxref{Definition of nth}).
152 @defun copy-sequence sequence
153 @cindex copying sequences
154 This function returns a copy of @var{sequence}. The copy is the same
155 type of object as the original sequence, and it has the same elements
158 Storing a new element into the copy does not affect the original
159 @var{sequence}, and vice versa. However, the elements of the new
160 sequence are not copies; they are identical (@code{eq}) to the elements
161 of the original. Therefore, changes made within these elements, as
162 found via the copied sequence, are also visible in the original
165 If the sequence is a string with text properties, the property list in
166 the copy is itself a copy, not shared with the original's property
167 list. However, the actual values of the properties are shared.
168 @xref{Text Properties}.
170 This function does not work for dotted lists. Trying to copy a
171 circular list may cause an infinite loop.
173 See also @code{append} in @ref{Building Lists}, @code{concat} in
174 @ref{Creating Strings}, and @code{vconcat} in @ref{Vector Functions},
175 for other ways to copy sequences.
183 (setq x (vector 'foo bar))
184 @result{} [foo (1 2)]
187 (setq y (copy-sequence x))
188 @result{} [foo (1 2)]
200 (eq (elt x 1) (elt y 1))
205 ;; @r{Replacing an element of one sequence.}
207 x @result{} [quux (1 2)]
208 y @result{} [foo (1 2)]
212 ;; @r{Modifying the inside of a shared element.}
213 (setcar (aref x 1) 69)
214 x @result{} [quux (69 2)]
215 y @result{} [foo (69 2)]
221 @cindex string reverse
223 @cindex vector reverse
224 @cindex sequence reverse
225 This function creates a new sequence whose elements are the elements
226 of @var{seq}, but in reverse order. The original argument @var{seq}
227 is @emph{not} altered. Note that char-table cannot be reversed.
264 @cindex reversing a string
265 @cindex reversing a list
266 @cindex reversing a vector
267 This function reverses the order of the elements of @var{seq}.
268 Unlike @code{reverse} the original @var{seq} may be modified.
284 ;; @r{The cons cell that was first is now last.}
290 To avoid confusion, we usually store the result of @code{nreverse}
291 back in the same variable which held the original list:
294 (setq x (nreverse x))
297 Here is the @code{nreverse} of our favorite example, @code{(a b c)},
298 presented graphically:
302 @r{Original list head:} @r{Reversed list:}
303 ------------- ------------- ------------
304 | car | cdr | | car | cdr | | car | cdr |
305 | a | nil |<-- | b | o |<-- | c | o |
306 | | | | | | | | | | | | |
307 ------------- | --------- | - | -------- | -
309 ------------- ------------
313 For the vector, it is even simpler because you don't need setq:
324 Note that unlike @code{reverse}, this function doesn't work with strings.
325 Although you can alter string data by using @code{aset}, it is strongly
326 encouraged to treat strings as immutable.
334 An @dfn{array} object has slots that hold a number of other Lisp
335 objects, called the elements of the array. Any element of an array
336 may be accessed in constant time. In contrast, the time to access an
337 element of a list is proportional to the position of that element in
340 Emacs defines four types of array, all one-dimensional:
341 @dfn{strings} (@pxref{String Type}), @dfn{vectors} (@pxref{Vector
342 Type}), @dfn{bool-vectors} (@pxref{Bool-Vector Type}), and
343 @dfn{char-tables} (@pxref{Char-Table Type}). Vectors and char-tables
344 can hold elements of any type, but strings can only hold characters,
345 and bool-vectors can only hold @code{t} and @code{nil}.
347 All four kinds of array share these characteristics:
351 The first element of an array has index zero, the second element has
352 index 1, and so on. This is called @dfn{zero-origin} indexing. For
353 example, an array of four elements has indices 0, 1, 2, @w{and 3}.
356 The length of the array is fixed once you create it; you cannot
357 change the length of an existing array.
360 For purposes of evaluation, the array is a constant---i.e.,
361 it evaluates to itself.
364 The elements of an array may be referenced or changed with the functions
365 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
368 When you create an array, other than a char-table, you must specify
369 its length. You cannot specify the length of a char-table, because that
370 is determined by the range of character codes.
372 In principle, if you want an array of text characters, you could use
373 either a string or a vector. In practice, we always choose strings for
374 such applications, for four reasons:
378 They occupy one-fourth the space of a vector of the same elements.
381 Strings are printed in a way that shows the contents more clearly
385 Strings can hold text properties. @xref{Text Properties}.
388 Many of the specialized editing and I/O facilities of Emacs accept only
389 strings. For example, you cannot insert a vector of characters into a
390 buffer the way you can insert a string. @xref{Strings and Characters}.
393 By contrast, for an array of keyboard input characters (such as a key
394 sequence), a vector may be necessary, because many keyboard input
395 characters are outside the range that will fit in a string. @xref{Key
398 @node Array Functions
399 @section Functions that Operate on Arrays
401 In this section, we describe the functions that accept all types of
405 This function returns @code{t} if @var{object} is an array (i.e., a
406 vector, a string, a bool-vector or a char-table).
414 (arrayp (syntax-table)) ;; @r{A char-table.}
420 @defun aref array index
421 @cindex array elements
422 This function returns the @var{index}th element of @var{array}. The
423 first element is at index zero.
427 (setq primes [2 3 5 7 11 13])
428 @result{} [2 3 5 7 11 13]
434 @result{} 98 ; @r{@samp{b} is @acronym{ASCII} code 98.}
438 See also the function @code{elt}, in @ref{Sequence Functions}.
441 @defun aset array index object
442 This function sets the @var{index}th element of @var{array} to be
443 @var{object}. It returns @var{object}.
447 (setq w [foo bar baz])
448 @result{} [foo bar baz]
452 @result{} [fu bar baz]
465 If @var{array} is a string and @var{object} is not a character, a
466 @code{wrong-type-argument} error results. The function converts a
467 unibyte string to multibyte if necessary to insert a character.
470 @defun fillarray array object
471 This function fills the array @var{array} with @var{object}, so that
472 each element of @var{array} is @var{object}. It returns @var{array}.
476 (setq a [a b c d e f g])
477 @result{} [a b c d e f g]
479 @result{} [0 0 0 0 0 0 0]
481 @result{} [0 0 0 0 0 0 0]
484 (setq s "When in the course")
485 @result{} "When in the course"
487 @result{} "------------------"
491 If @var{array} is a string and @var{object} is not a character, a
492 @code{wrong-type-argument} error results.
495 The general sequence functions @code{copy-sequence} and @code{length}
496 are often useful for objects known to be arrays. @xref{Sequence Functions}.
500 @cindex vector (type)
502 A @dfn{vector} is a general-purpose array whose elements can be any
503 Lisp objects. (By contrast, the elements of a string can only be
504 characters. @xref{Strings and Characters}.) Vectors are used in
505 Emacs for many purposes: as key sequences (@pxref{Key Sequences}), as
506 symbol-lookup tables (@pxref{Creating Symbols}), as part of the
507 representation of a byte-compiled function (@pxref{Byte Compilation}),
510 Like other arrays, vectors use zero-origin indexing: the first
513 Vectors are printed with square brackets surrounding the elements.
514 Thus, a vector whose elements are the symbols @code{a}, @code{b} and
515 @code{a} is printed as @code{[a b a]}. You can write vectors in the
516 same way in Lisp input.
518 A vector, like a string or a number, is considered a constant for
519 evaluation: the result of evaluating it is the same vector. This does
520 not evaluate or even examine the elements of the vector.
521 @xref{Self-Evaluating Forms}.
523 Here are examples illustrating these principles:
527 (setq avector [1 two '(three) "four" [five]])
528 @result{} [1 two (quote (three)) "four" [five]]
530 @result{} [1 two (quote (three)) "four" [five]]
531 (eq avector (eval avector))
536 @node Vector Functions
537 @section Functions for Vectors
539 Here are some functions that relate to vectors:
541 @defun vectorp object
542 This function returns @code{t} if @var{object} is a vector.
554 @defun vector &rest objects
555 This function creates and returns a vector whose elements are the
556 arguments, @var{objects}.
560 (vector 'foo 23 [bar baz] "rats")
561 @result{} [foo 23 [bar baz] "rats"]
568 @defun make-vector length object
569 This function returns a new vector consisting of @var{length} elements,
570 each initialized to @var{object}.
574 (setq sleepy (make-vector 9 'Z))
575 @result{} [Z Z Z Z Z Z Z Z Z]
580 @defun vconcat &rest sequences
581 @cindex copying vectors
582 This function returns a new vector containing all the elements of
583 @var{sequences}. The arguments @var{sequences} may be true lists,
584 vectors, strings or bool-vectors. If no @var{sequences} are given,
585 the empty vector is returned.
587 The value is either the empty vector, or is a newly constructed
588 nonempty vector that is not @code{eq} to any existing vector.
592 (setq a (vconcat '(A B C) '(D E F)))
593 @result{} [A B C D E F]
600 (vconcat [A B C] "aa" '(foo (6 7)))
601 @result{} [A B C 97 97 foo (6 7)]
605 The @code{vconcat} function also allows byte-code function objects as
606 arguments. This is a special feature to make it easy to access the entire
607 contents of a byte-code function object. @xref{Byte-Code Objects}.
609 For other concatenation functions, see @code{mapconcat} in @ref{Mapping
610 Functions}, @code{concat} in @ref{Creating Strings}, and @code{append}
611 in @ref{Building Lists}.
614 The @code{append} function also provides a way to convert a vector into a
615 list with the same elements:
619 (setq avector [1 two (quote (three)) "four" [five]])
620 @result{} [1 two (quote (three)) "four" [five]]
622 @result{} (1 two (quote (three)) "four" [five])
629 @cindex extra slots of char-table
631 A char-table is much like a vector, except that it is indexed by
632 character codes. Any valid character code, without modifiers, can be
633 used as an index in a char-table. You can access a char-table's
634 elements with @code{aref} and @code{aset}, as with any array. In
635 addition, a char-table can have @dfn{extra slots} to hold additional
636 data not associated with particular character codes. Like vectors,
637 char-tables are constants when evaluated, and can hold elements of any
640 @cindex subtype of char-table
641 Each char-table has a @dfn{subtype}, a symbol, which serves two
646 The subtype provides an easy way to tell what the char-table is for.
647 For instance, display tables are char-tables with @code{display-table}
648 as the subtype, and syntax tables are char-tables with
649 @code{syntax-table} as the subtype. The subtype can be queried using
650 the function @code{char-table-subtype}, described below.
653 The subtype controls the number of @dfn{extra slots} in the
654 char-table. This number is specified by the subtype's
655 @code{char-table-extra-slots} symbol property (@pxref{Symbol
656 Properties}), whose value should be an integer between 0 and 10. If
657 the subtype has no such symbol property, the char-table has no extra
661 @cindex parent of char-table
662 A char-table can have a @dfn{parent}, which is another char-table. If
663 it does, then whenever the char-table specifies @code{nil} for a
664 particular character @var{c}, it inherits the value specified in the
665 parent. In other words, @code{(aref @var{char-table} @var{c})} returns
666 the value from the parent of @var{char-table} if @var{char-table} itself
667 specifies @code{nil}.
669 @cindex default value of char-table
670 A char-table can also have a @dfn{default value}. If so, then
671 @code{(aref @var{char-table} @var{c})} returns the default value
672 whenever the char-table does not specify any other non-@code{nil} value.
674 @defun make-char-table subtype &optional init
675 Return a newly-created char-table, with subtype @var{subtype} (a
676 symbol). Each element is initialized to @var{init}, which defaults to
677 @code{nil}. You cannot alter the subtype of a char-table after the
678 char-table is created.
680 There is no argument to specify the length of the char-table, because
681 all char-tables have room for any valid character code as an index.
683 If @var{subtype} has the @code{char-table-extra-slots} symbol
684 property, that specifies the number of extra slots in the char-table.
685 This should be an integer between 0 and 10; otherwise,
686 @code{make-char-table} raises an error. If @var{subtype} has no
687 @code{char-table-extra-slots} symbol property (@pxref{Property
688 Lists}), the char-table has no extra slots.
691 @defun char-table-p object
692 This function returns @code{t} if @var{object} is a char-table, and
693 @code{nil} otherwise.
696 @defun char-table-subtype char-table
697 This function returns the subtype symbol of @var{char-table}.
700 There is no special function to access default values in a char-table.
701 To do that, use @code{char-table-range} (see below).
703 @defun char-table-parent char-table
704 This function returns the parent of @var{char-table}. The parent is
705 always either @code{nil} or another char-table.
708 @defun set-char-table-parent char-table new-parent
709 This function sets the parent of @var{char-table} to @var{new-parent}.
712 @defun char-table-extra-slot char-table n
713 This function returns the contents of extra slot @var{n} of
714 @var{char-table}. The number of extra slots in a char-table is
715 determined by its subtype.
718 @defun set-char-table-extra-slot char-table n value
719 This function stores @var{value} in extra slot @var{n} of
723 A char-table can specify an element value for a single character code;
724 it can also specify a value for an entire character set.
726 @defun char-table-range char-table range
727 This returns the value specified in @var{char-table} for a range of
728 characters @var{range}. Here are the possibilities for @var{range}:
732 Refers to the default value.
735 Refers to the element for character @var{char}
736 (supposing @var{char} is a valid character code).
738 @item @code{(@var{from} . @var{to})}
739 A cons cell refers to all the characters in the inclusive range
740 @samp{[@var{from}..@var{to}]}.
744 @defun set-char-table-range char-table range value
745 This function sets the value in @var{char-table} for a range of
746 characters @var{range}. Here are the possibilities for @var{range}:
750 Refers to the default value.
753 Refers to the whole range of character codes.
756 Refers to the element for character @var{char}
757 (supposing @var{char} is a valid character code).
759 @item @code{(@var{from} . @var{to})}
760 A cons cell refers to all the characters in the inclusive range
761 @samp{[@var{from}..@var{to}]}.
765 @defun map-char-table function char-table
766 This function calls its argument @var{function} for each element of
767 @var{char-table} that has a non-@code{nil} value. The call to
768 @var{function} is with two arguments, a key and a value. The key
769 is a possible @var{range} argument for @code{char-table-range}---either
770 a valid character or a cons cell @code{(@var{from} . @var{to})},
771 specifying a range of characters that share the same value. The value is
772 what @code{(char-table-range @var{char-table} @var{key})} returns.
774 Overall, the key-value pairs passed to @var{function} describe all the
775 values stored in @var{char-table}.
777 The return value is always @code{nil}; to make calls to
778 @code{map-char-table} useful, @var{function} should have side effects.
779 For example, here is how to examine the elements of the syntax table:
784 #'(lambda (key value)
788 (list (car key) (cdr key))
795 (((2597602 4194303) (2)) ((2597523 2597601) (3))
796 ... (65379 (5 . 65378)) (65378 (4 . 65379)) (65377 (1))
797 ... (12 (0)) (11 (3)) (10 (12)) (9 (0)) ((0 8) (3)))
802 @section Bool-vectors
805 A bool-vector is much like a vector, except that it stores only the
806 values @code{t} and @code{nil}. If you try to store any non-@code{nil}
807 value into an element of the bool-vector, the effect is to store
808 @code{t} there. As with all arrays, bool-vector indices start from 0,
809 and the length cannot be changed once the bool-vector is created.
810 Bool-vectors are constants when evaluated.
812 Several functions work specifically with bool-vectors; aside
813 from that, you manipulate them with same functions used for other kinds
816 @defun make-bool-vector length initial
817 Return a new bool-vector of @var{length} elements,
818 each one initialized to @var{initial}.
821 @defun bool-vector &rest objects
822 This function creates and returns a bool-vector whose elements are the
823 arguments, @var{objects}.
826 @defun bool-vector-p object
827 This returns @code{t} if @var{object} is a bool-vector,
828 and @code{nil} otherwise.
831 There are also some bool-vector set operation functions, described below:
833 @defun bool-vector-exclusive-or a b &optional c
834 Return @dfn{bitwise exclusive or} of bool vectors @var{a} and @var{b}.
835 If optional argument @var{c} is given, the result of this operation is
836 stored into @var{c}. All arguments should be bool vectors of the same length.
839 @defun bool-vector-union a b &optional c
840 Return @dfn{bitwise or} of bool vectors @var{a} and @var{b}. If
841 optional argument @var{c} is given, the result of this operation is
842 stored into @var{c}. All arguments should be bool vectors of the same length.
845 @defun bool-vector-intersection a b &optional c
846 Return @dfn{bitwise and} of bool vectors @var{a} and @var{b}. If
847 optional argument @var{c} is given, the result of this operation is
848 stored into @var{c}. All arguments should be bool vectors of the same length.
851 @defun bool-vector-set-difference a b &optional c
852 Return @dfn{set difference} of bool vectors @var{a} and @var{b}. If
853 optional argument @var{c} is given, the result of this operation is
854 stored into @var{c}. All arguments should be bool vectors of the same length.
857 @defun bool-vector-not a &optional b
858 Return @dfn{set complement} of bool vector @var{a}. If optional
859 argument @var{b} is given, the result of this operation is stored into
860 @var{b}. All arguments should be bool vectors of the same length.
863 @defun bool-vector-subsetp a b
864 Return @code{t} if every @code{t} value in @var{a} is also t in
865 @var{b}, @code{nil} otherwise. All arguments should be bool vectors of the
869 @defun bool-vector-count-consecutive a b i
870 Return the number of consecutive elements in @var{a} equal @var{b}
871 starting at @var{i}. @code{a} is a bool vector, @var{b} is @code{t}
872 or @code{nil}, and @var{i} is an index into @code{a}.
875 @defun bool-vector-count-population a
876 Return the number of elements that are @code{t} in bool vector @var{a}.
879 The printed form represents up to 8 boolean values as a single
884 (bool-vector t nil t nil)
891 You can use @code{vconcat} to print a bool-vector like other vectors:
895 (vconcat (bool-vector nil t nil t))
896 @result{} [nil t nil t]
900 Here is another example of creating, examining, and updating a
904 (setq bv (make-bool-vector 5 t))
915 These results make sense because the binary codes for control-_ and
916 control-W are 11111 and 10111, respectively.
919 @section Managing a Fixed-Size Ring of Objects
921 @cindex ring data structure
922 A @dfn{ring} is a fixed-size data structure that supports insertion,
923 deletion, rotation, and modulo-indexed reference and traversal. An
924 efficient ring data structure is implemented by the @code{ring}
925 package. It provides the functions listed in this section.
927 Note that several ``rings'' in Emacs, like the kill ring and the
928 mark ring, are actually implemented as simple lists, @emph{not} using
929 the @code{ring} package; thus the following functions won't work on
932 @defun make-ring size
933 This returns a new ring capable of holding @var{size} objects.
934 @var{size} should be an integer.
938 This returns @code{t} if @var{object} is a ring, @code{nil} otherwise.
941 @defun ring-size ring
942 This returns the maximum capacity of the @var{ring}.
945 @defun ring-length ring
946 This returns the number of objects that @var{ring} currently contains.
947 The value will never exceed that returned by @code{ring-size}.
950 @defun ring-elements ring
951 This returns a list of the objects in @var{ring}, in order, newest first.
954 @defun ring-copy ring
955 This returns a new ring which is a copy of @var{ring}.
956 The new ring contains the same (@code{eq}) objects as @var{ring}.
959 @defun ring-empty-p ring
960 This returns @code{t} if @var{ring} is empty, @code{nil} otherwise.
963 The newest element in the ring always has index 0. Higher indices
964 correspond to older elements. Indices are computed modulo the ring
965 length. Index @minus{}1 corresponds to the oldest element, @minus{}2
966 to the next-oldest, and so forth.
968 @defun ring-ref ring index
969 This returns the object in @var{ring} found at index @var{index}.
970 @var{index} may be negative or greater than the ring length. If
971 @var{ring} is empty, @code{ring-ref} signals an error.
974 @defun ring-insert ring object
975 This inserts @var{object} into @var{ring}, making it the newest
976 element, and returns @var{object}.
978 If the ring is full, insertion removes the oldest element to
979 make room for the new element.
982 @defun ring-remove ring &optional index
983 Remove an object from @var{ring}, and return that object. The
984 argument @var{index} specifies which item to remove; if it is
985 @code{nil}, that means to remove the oldest item. If @var{ring} is
986 empty, @code{ring-remove} signals an error.
989 @defun ring-insert-at-beginning ring object
990 This inserts @var{object} into @var{ring}, treating it as the oldest
991 element. The return value is not significant.
993 If the ring is full, this function removes the newest element to make
994 room for the inserted element.
997 @cindex fifo data structure
998 If you are careful not to exceed the ring size, you can
999 use the ring as a first-in-first-out queue. For example:
1002 (let ((fifo (make-ring 5)))
1003 (mapc (lambda (obj) (ring-insert fifo obj))
1005 (list (ring-remove fifo) t
1006 (ring-remove fifo) t
1007 (ring-remove fifo)))
1008 @result{} (0 t one t "two")