2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2017 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 @anchor{Definition of length}
76 This function returns the number of elements in @var{sequence}. If
77 @var{sequence} is a dotted list, a @code{wrong-type-argument} error is
78 signaled. Circular lists may cause an infinite loop. For a
79 char-table, the value returned is always one more than the maximum
82 @xref{Definition of safe-length}, for the related function @code{safe-length}.
102 (length (make-bool-vector 5 nil))
109 See also @code{string-bytes}, in @ref{Text Representations}.
111 If you need to compute the width of a string on display, you should use
112 @code{string-width} (@pxref{Size of Displayed Text}), not @code{length},
113 since @code{length} only counts the number of characters, but does not
114 account for the display width of each character.
116 @defun elt sequence index
117 @anchor{Definition of elt}
118 @cindex elements of sequences
119 This function returns the element of @var{sequence} indexed by
120 @var{index}. Legitimate values of @var{index} are integers ranging
121 from 0 up to one less than the length of @var{sequence}. If
122 @var{sequence} is a list, out-of-range values behave as for
123 @code{nth}. @xref{Definition of nth}. Otherwise, out-of-range values
124 trigger an @code{args-out-of-range} error.
136 ;; @r{We use @code{string} to show clearly which character @code{elt} returns.}
137 (string (elt "1234" 2))
142 @error{} Args out of range: [1 2 3 4], 4
146 @error{} Args out of range: [1 2 3 4], -1
150 This function generalizes @code{aref} (@pxref{Array Functions}) and
151 @code{nth} (@pxref{Definition of nth}).
154 @defun copy-sequence seqr
155 @cindex copying sequences
156 This function returns a copy of @var{seqr}, which should be either a
157 sequence or a record. The copy is the same type of object as the
158 original, and it has the same elements in the same order. However, if
159 @var{seqr} is empty, like a string or a vector of zero length, the
160 value returned by this function might not be a copy, but an empty
161 object of the same type and identical to @var{seqr}.
163 Storing a new element into the copy does not affect the original
164 @var{seqr}, and vice versa. However, the elements of the copy
165 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 copy, are also visible in the original.
169 If the argument is a string with text properties, the property list in
170 the copy is itself a copy, not shared with the original's property
171 list. However, the actual values of the properties are shared.
172 @xref{Text Properties}.
174 This function does not work for dotted lists. Trying to copy a
175 circular list may cause an infinite loop.
177 See also @code{append} in @ref{Building Lists}, @code{concat} in
178 @ref{Creating Strings}, and @code{vconcat} in @ref{Vector Functions},
179 for other ways to copy sequences.
187 (setq x (vector 'foo bar))
188 @result{} [foo (1 2)]
191 (setq y (copy-sequence x))
192 @result{} [foo (1 2)]
204 (eq (elt x 1) (elt y 1))
209 ;; @r{Replacing an element of one sequence.}
211 x @result{} [quux (1 2)]
212 y @result{} [foo (1 2)]
216 ;; @r{Modifying the inside of a shared element.}
217 (setcar (aref x 1) 69)
218 x @result{} [quux (69 2)]
219 y @result{} [foo (69 2)]
224 @defun reverse sequence
225 @cindex string reverse
227 @cindex vector reverse
228 @cindex sequence reverse
229 This function creates a new sequence whose elements are the elements
230 of @var{sequence}, but in reverse order. The original argument @var{sequence}
231 is @emph{not} altered. Note that char-tables cannot be reversed.
267 @defun nreverse sequence
268 @cindex reversing a string
269 @cindex reversing a list
270 @cindex reversing a vector
271 This function reverses the order of the elements of @var{sequence}.
272 Unlike @code{reverse} the original @var{sequence} may be modified.
288 ;; @r{The cons cell that was first is now last.}
294 To avoid confusion, we usually store the result of @code{nreverse}
295 back in the same variable which held the original list:
298 (setq x (nreverse x))
301 Here is the @code{nreverse} of our favorite example, @code{(a b c)},
302 presented graphically:
306 @r{Original list head:} @r{Reversed list:}
307 ------------- ------------- ------------
308 | car | cdr | | car | cdr | | car | cdr |
309 | a | nil |<-- | b | o |<-- | c | o |
310 | | | | | | | | | | | | |
311 ------------- | --------- | - | -------- | -
313 ------------- ------------
317 For the vector, it is even simpler because you don't need setq:
328 Note that unlike @code{reverse}, this function doesn't work with strings.
329 Although you can alter string data by using @code{aset}, it is strongly
330 encouraged to treat strings as immutable.
334 @defun sort sequence predicate
336 @cindex sorting lists
337 @cindex sorting vectors
338 This function sorts @var{sequence} stably. Note that this function doesn't work
339 for all sequences; it may be used only for lists and vectors. If @var{sequence}
340 is a list, it is modified destructively. This functions returns the sorted
341 @var{sequence} and compares elements using @var{predicate}. A stable sort is
342 one in which elements with equal sort keys maintain their relative order before
343 and after the sort. Stability is important when successive sorts are used to
344 order elements according to different criteria.
346 The argument @var{predicate} must be a function that accepts two
347 arguments. It is called with two elements of @var{sequence}. To get an
348 increasing order sort, the @var{predicate} should return non-@code{nil} if the
349 first element is ``less'' than the second, or @code{nil} if not.
351 The comparison function @var{predicate} must give reliable results for
352 any given pair of arguments, at least within a single call to
353 @code{sort}. It must be @dfn{antisymmetric}; that is, if @var{a} is
354 less than @var{b}, @var{b} must not be less than @var{a}. It must be
355 @dfn{transitive}---that is, if @var{a} is less than @var{b}, and @var{b}
356 is less than @var{c}, then @var{a} must be less than @var{c}. If you
357 use a comparison function which does not meet these requirements, the
358 result of @code{sort} is unpredictable.
360 The destructive aspect of @code{sort} for lists is that it rearranges the
361 cons cells forming @var{sequence} by changing @sc{cdr}s. A nondestructive
362 sort function would create new cons cells to store the elements in their
363 sorted order. If you wish to make a sorted copy without destroying the
364 original, copy it first with @code{copy-sequence} and then sort.
366 Sorting does not change the @sc{car}s of the cons cells in @var{sequence};
367 the cons cell that originally contained the element @code{a} in
368 @var{sequence} still has @code{a} in its @sc{car} after sorting, but it now
369 appears in a different position in the list due to the change of
370 @sc{cdr}s. For example:
374 (setq nums '(1 3 2 6 5 4 0))
375 @result{} (1 3 2 6 5 4 0)
379 @result{} (0 1 2 3 4 5 6)
383 @result{} (1 2 3 4 5 6)
388 @strong{Warning}: Note that the list in @code{nums} no longer contains
389 0; this is the same cons cell that it was before, but it is no longer
390 the first one in the list. Don't assume a variable that formerly held
391 the argument now holds the entire sorted list! Instead, save the result
392 of @code{sort} and use that. Most often we store the result back into
393 the variable that held the original list:
396 (setq nums (sort nums '<))
399 For the better understanding of what stable sort is, consider the following
400 vector example. After sorting, all items whose @code{car} is 8 are grouped
401 at the beginning of @code{vector}, but their relative order is preserved.
402 All items whose @code{car} is 9 are grouped at the end of @code{vector},
403 but their relative order is also preserved:
409 (vector '(8 . "xxx") '(9 . "aaa") '(8 . "bbb") '(9 . "zzz")
410 '(9 . "ppp") '(8 . "ttt") '(8 . "eee") '(9 . "fff")))
411 @result{} [(8 . "xxx") (9 . "aaa") (8 . "bbb") (9 . "zzz")
412 (9 . "ppp") (8 . "ttt") (8 . "eee") (9 . "fff")]
415 (sort vector (lambda (x y) (< (car x) (car y))))
416 @result{} [(8 . "xxx") (8 . "bbb") (8 . "ttt") (8 . "eee")
417 (9 . "aaa") (9 . "zzz") (9 . "ppp") (9 . "fff")]
421 @xref{Sorting}, for more functions that perform sorting.
422 See @code{documentation} in @ref{Accessing Documentation}, for a
423 useful example of @code{sort}.
426 @cindex sequence functions in seq
428 The @file{seq.el} library provides the following additional sequence
429 manipulation macros and functions, prefixed with @code{seq-}. To use
430 them, you must first load the @file{seq} library.
432 All functions defined in this library are free of side-effects;
433 i.e., they do not modify any sequence (list, vector, or string) that
434 you pass as an argument. Unless otherwise stated, the result is a
435 sequence of the same type as the input. For those functions that take
436 a predicate, this should be a function of one argument.
438 The @file{seq.el} library can be extended to work with additional
439 types of sequential data-structures. For that purpose, all functions
440 are defined using @code{cl-defgeneric}. @xref{Generic Functions}, for
441 more details about using @code{cl-defgeneric} for adding extensions.
443 @defun seq-elt sequence index
444 This function returns the element of @var{sequence} at the specified
445 @var{index}, which is an integer whose valid value range is zero to
446 one less than the length of @var{sequence}. For out-of-range values
447 on built-in sequence types, @code{seq-elt} behaves like @code{elt}.
448 For the details, see @ref{Definition of elt}.
452 (seq-elt [1 2 3 4] 2)
457 @code{seq-elt} returns places settable using @code{setf}
458 (@pxref{Setting Generalized Variables}).
463 (setf (seq-elt vec 2) 5)
470 @defun seq-length sequence
471 This function returns the number of elements in @var{sequence}. For
472 built-in sequence types, @code{seq-length} behaves like @code{length}.
473 @xref{Definition of length}.
477 This function returns non-@code{nil} if @var{sequence} is a sequence
478 (a list or array), or any additional type of sequence defined via
479 @file{seq.el} generic functions.
493 @defun seq-drop sequence n
494 This function returns all but the first @var{n} (an integer)
495 elements of @var{sequence}. If @var{n} is negative or zero,
496 the result is @var{sequence}.
500 (seq-drop [1 2 3 4 5 6] 3)
504 (seq-drop "hello world" -4)
505 @result{} "hello world"
510 @defun seq-take sequence n
511 This function returns the first @var{n} (an integer) elements of
512 @var{sequence}. If @var{n} is negative or zero, the result
517 (seq-take '(1 2 3 4) 3)
521 (seq-take [1 2 3 4] 0)
527 @defun seq-take-while predicate sequence
528 This function returns the members of @var{sequence} in order,
529 stopping before the first one for which @var{predicate} returns @code{nil}.
533 (seq-take-while (lambda (elt) (> elt 0)) '(1 2 3 -1 -2))
537 (seq-take-while (lambda (elt) (> elt 0)) [-1 4 6])
543 @defun seq-drop-while predicate sequence
544 This function returns the members of @var{sequence} in order,
545 starting from the first one for which @var{predicate} returns @code{nil}.
549 (seq-drop-while (lambda (elt) (> elt 0)) '(1 2 3 -1 -2))
553 (seq-drop-while (lambda (elt) (< elt 0)) [1 4 6])
559 @defun seq-do function sequence
560 This function applies @var{function} to each element of
561 @var{sequence} in turn (presumably for side effects), and returns
565 @defun seq-map function sequence
566 This function returns the result of applying @var{function} to each
567 element of @var{sequence}. The returned value is a list.
571 (seq-map #'1+ '(2 4 6))
575 (seq-map #'symbol-name [foo bar])
576 @result{} ("foo" "bar")
581 @defun seq-map-indexed function sequence
582 This function returns the result of applying @var{function} to each
583 element of @var{sequence} and its index within @var{seq}. The
584 returned value is a list.
588 (seq-map-indexed (lambda (elt idx)
591 @result{} ((0 a) (b 1) (c 2))
596 @defun seq-mapn function &rest sequences
597 This function returns the result of applying @var{function} to each
598 element of @var{sequences}. The arity (@pxref{What Is a Function,
599 sub-arity}) of @var{function} must match the number of sequences.
600 Mapping stops at the end of the shortest sequence, and the returned
605 (seq-mapn #'+ '(2 4 6) '(20 40 60))
609 (seq-mapn #'concat '("moskito" "bite") ["bee" "sting"])
610 @result{} ("moskitobee" "bitesting")
615 @defun seq-filter predicate sequence
616 @cindex filtering sequences
617 This function returns a list of all the elements in @var{sequence}
618 for which @var{predicate} returns non-@code{nil}.
622 (seq-filter (lambda (elt) (> elt 0)) [1 -1 3 -3 5])
626 (seq-filter (lambda (elt) (> elt 0)) '(-1 -3 -5))
632 @defun seq-remove predicate sequence
633 @cindex removing from sequences
634 This function returns a list of all the elements in @var{sequence}
635 for which @var{predicate} returns @code{nil}.
639 (seq-remove (lambda (elt) (> elt 0)) [1 -1 3 -3 5])
643 (seq-remove (lambda (elt) (< elt 0)) '(-1 -3 -5))
649 @defun seq-reduce function sequence initial-value
650 @cindex reducing sequences
651 This function returns the result of calling @var{function} with
652 @var{initial-value} and the first element of @var{sequence}, then calling
653 @var{function} with that result and the second element of @var{sequence},
654 then with that result and the third element of @var{sequence}, etc.
655 @var{function} should be a function of two arguments. If
656 @var{sequence} is empty, this returns @var{initial-value} without
657 calling @var{function}.
661 (seq-reduce #'+ [1 2 3 4] 0)
665 (seq-reduce #'+ '(1 2 3 4) 5)
669 (seq-reduce #'+ '() 3)
675 @defun seq-some predicate sequence
676 This function returns the first non-@code{nil} value returned by
677 applying @var{predicate} to each element of @var{sequence} in turn.
681 (seq-some #'numberp ["abc" 1 nil])
685 (seq-some #'numberp ["abc" "def"])
689 (seq-some #'null ["abc" 1 nil])
693 (seq-some #'1+ [2 4 6])
699 @defun seq-find predicate sequence &optional default
700 This function returns the first element in @var{sequence} for which
701 @var{predicate} returns non-@code{nil}. If no element matches
702 @var{predicate}, the function returns @var{default}.
704 Note that this function has an ambiguity if the found element is
705 identical to @var{default}, as in that case it cannot be known whether
706 an element was found or not.
710 (seq-find #'numberp ["abc" 1 nil])
714 (seq-find #'numberp ["abc" "def"])
720 @defun seq-every-p predicate sequence
721 This function returns non-@code{nil} if applying @var{predicate}
722 to every element of @var{sequence} returns non-@code{nil}.
726 (seq-every-p #'numberp [2 4 6])
730 (seq-some #'numberp [2 4 "6"])
736 @defun seq-empty-p sequence
737 This function returns non-@code{nil} if @var{sequence} is empty.
741 (seq-empty-p "not empty")
751 @defun seq-count predicate sequence
752 This function returns the number of elements in @var{sequence} for which
753 @var{predicate} returns non-@code{nil}.
756 (seq-count (lambda (elt) (> elt 0)) [-1 2 0 3 -2])
761 @cindex sorting sequences
762 @defun seq-sort function sequence
763 This function returns a copy of @var{sequence} that is sorted
764 according to @var{function}, a function of two arguments that returns
765 non-@code{nil} if the first argument should sort before the second.
768 @defun seq-sort-by function predicate sequence
769 This function is similar to @code{seq-sort}, but the elements of
770 @var{sequence} are transformed by applying @var{function} on them
771 before being sorted. @var{function} is a function of one argument.
774 (seq-sort-by #'seq-length #'> ["a" "ab" "abc"])
775 @result{} ["abc" "ab" "a"]
780 @defun seq-contains sequence elt &optional function
781 This function returns the first element in @var{sequence} that is equal to
782 @var{elt}. If the optional argument @var{function} is non-@code{nil},
783 it is a function of two arguments to use instead of the default @code{equal}.
787 (seq-contains '(symbol1 symbol2) 'symbol1)
791 (seq-contains '(symbol1 symbol2) 'symbol3)
798 @defun seq-set-equal-p sequence1 sequence2 &optional testfn
799 This function checks whether @var{sequence1} and @var{sequence2}
800 contain the same elements, regardless of the order. If the optional
801 argument @var{testfn} is non-@code{nil}, it is a function of two
802 arguments to use instead of the default @code{equal}.
806 (seq-set-equal-p '(a b c) '(c b a))
810 (seq-set-equal-p '(a b c) '(c b))
814 (seq-set-equal-p '("a" "b" "c") '("c" "b" "a"))
818 (seq-set-equal-p '("a" "b" "c") '("c" "b" "a") #'eq)
825 @defun seq-position sequence elt &optional function
826 This function returns the index of the first element in
827 @var{sequence} that is equal to @var{elt}. If the optional argument
828 @var{function} is non-@code{nil}, it is a function of two arguments to
829 use instead of the default @code{equal}.
833 (seq-position '(a b c) 'b)
837 (seq-position '(a b c) 'd)
844 @defun seq-uniq sequence &optional function
845 This function returns a list of the elements of @var{sequence} with
846 duplicates removed. If the optional argument @var{function} is non-@code{nil},
847 it is a function of two arguments to use instead of the default @code{equal}.
851 (seq-uniq '(1 2 2 1 3))
855 (seq-uniq '(1 2 2.0 1.0) #'=)
861 @defun seq-subseq sequence start &optional end
862 This function returns a subset of @var{sequence} from @var{start}
863 to @var{end}, both integers (@var{end} defaults to the last element).
864 If @var{start} or @var{end} is negative, it counts from the end of
869 (seq-subseq '(1 2 3 4 5) 1)
873 (seq-subseq '[1 2 3 4 5] 1 3)
877 (seq-subseq '[1 2 3 4 5] -3 -1)
883 @defun seq-concatenate type &rest sequences
884 This function returns a sequence of type @var{type} made of the
885 concatenation of @var{sequences}. @var{type} may be: @code{vector},
886 @code{list} or @code{string}.
890 (seq-concatenate 'list '(1 2) '(3 4) [5 6])
891 @result{} (1 2 3 4 5 6)
894 (seq-concatenate 'string "Hello " "world")
895 @result{} "Hello world"
900 @defun seq-mapcat function sequence &optional type
901 This function returns the result of applying @code{seq-concatenate}
902 to the result of applying @var{function} to each element of
903 @var{sequence}. The result is a sequence of type @var{type}, or a
904 list if @var{type} is @code{nil}.
908 (seq-mapcat #'seq-reverse '((3 2 1) (6 5 4)))
909 @result{} (1 2 3 4 5 6)
914 @defun seq-partition sequence n
915 This function returns a list of the elements of @var{sequence}
916 grouped into sub-sequences of length @var{n}. The last sequence may
917 contain less elements than @var{n}. @var{n} must be an integer. If
918 @var{n} is a negative integer or 0, the return value is @code{nil}.
922 (seq-partition '(0 1 2 3 4 5 6 7) 3)
923 @result{} ((0 1 2) (3 4 5) (6 7))
928 @defun seq-intersection sequence1 sequence2 &optional function
929 This function returns a list of the elements that appear both in
930 @var{sequence1} and @var{sequence2}. If the optional argument
931 @var{function} is non-@code{nil}, it is a function of two arguments to
932 use to compare elements instead of the default @code{equal}.
936 (seq-intersection [2 3 4 5] [1 3 5 6 7])
943 @defun seq-difference sequence1 sequence2 &optional function
944 This function returns a list of the elements that appear in
945 @var{sequence1} but not in @var{sequence2}. If the optional argument
946 @var{function} is non-@code{nil}, it is a function of two arguments to
947 use to compare elements instead of the default @code{equal}.
951 (seq-difference '(2 3 4 5) [1 3 5 6 7])
957 @defun seq-group-by function sequence
958 This function separates the elements of @var{sequence} into an alist
959 whose keys are the result of applying @var{function} to each element
960 of @var{sequence}. Keys are compared using @code{equal}.
964 (seq-group-by #'integerp '(1 2.1 3 2 3.2))
965 @result{} ((t 1 3 2) (nil 2.1 3.2))
968 (seq-group-by #'car '((a 1) (b 2) (a 3) (c 4)))
969 @result{} ((b (b 2)) (a (a 1) (a 3)) (c (c 4)))
974 @defun seq-into sequence type
975 This function converts the sequence @var{sequence} into a sequence
976 of type @var{type}. @var{type} can be one of the following symbols:
977 @code{vector}, @code{string} or @code{list}.
981 (seq-into [1 2 3] 'list)
985 (seq-into nil 'vector)
989 (seq-into "hello" 'vector)
990 @result{} [104 101 108 108 111]
995 @defun seq-min sequence
996 This function returns the smallest element of @var{sequence}. The
997 elements of @var{sequence} must be numbers or markers
1012 @defun seq-max sequence
1013 This function returns the largest element of @var{sequence}. The
1014 elements of @var{sequence} must be numbers or markers.
1028 @defmac seq-doseq (var sequence) body@dots{}
1029 @cindex sequence iteration
1030 This macro is like @code{dolist} (@pxref{Iteration, dolist}), except
1031 that @var{sequence} can be a list, vector or string. This is
1032 primarily useful for side-effects.
1035 @defmac seq-let arguments sequence body@dots{}
1036 @cindex sequence destructuring
1037 This macro binds the variables defined in @var{arguments} to the
1038 elements of @var{sequence}. @var{arguments} can themselves include
1039 sequences, allowing for nested destructuring.
1041 The @var{arguments} sequence can also include the @code{&rest} marker
1042 followed by a variable name to be bound to the rest of
1047 (seq-let [first second] [1 2 3 4]
1048 (list first second))
1052 (seq-let (_ a _ b) '(1 2 3 4)
1057 (seq-let [a [b [c]]] [1 [2 [3]]]
1062 (seq-let [a b &rest others] [1 2 3 4]
1069 @defun seq-random-elt sequence
1070 This function returns an element of @var{sequence} taken at random.
1074 (seq-random-elt [1 2 3 4])
1076 (seq-random-elt [1 2 3 4])
1078 (seq-random-elt [1 2 3 4])
1080 (seq-random-elt [1 2 3 4])
1082 (seq-random-elt [1 2 3 4])
1087 If @var{sequence} is empty, this function signals an error.
1094 An @dfn{array} object has slots that hold a number of other Lisp
1095 objects, called the elements of the array. Any element of an array
1096 may be accessed in constant time. In contrast, the time to access an
1097 element of a list is proportional to the position of that element in
1100 Emacs defines four types of array, all one-dimensional:
1101 @dfn{strings} (@pxref{String Type}), @dfn{vectors} (@pxref{Vector
1102 Type}), @dfn{bool-vectors} (@pxref{Bool-Vector Type}), and
1103 @dfn{char-tables} (@pxref{Char-Table Type}). Vectors and char-tables
1104 can hold elements of any type, but strings can only hold characters,
1105 and bool-vectors can only hold @code{t} and @code{nil}.
1107 All four kinds of array share these characteristics:
1111 The first element of an array has index zero, the second element has
1112 index 1, and so on. This is called @dfn{zero-origin} indexing. For
1113 example, an array of four elements has indices 0, 1, 2, @w{and 3}.
1116 The length of the array is fixed once you create it; you cannot
1117 change the length of an existing array.
1120 For purposes of evaluation, the array is a constant---i.e.,
1121 it evaluates to itself.
1124 The elements of an array may be referenced or changed with the functions
1125 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
1128 When you create an array, other than a char-table, you must specify
1129 its length. You cannot specify the length of a char-table, because that
1130 is determined by the range of character codes.
1132 In principle, if you want an array of text characters, you could use
1133 either a string or a vector. In practice, we always choose strings for
1134 such applications, for four reasons:
1138 They occupy one-fourth the space of a vector of the same elements.
1141 Strings are printed in a way that shows the contents more clearly
1145 Strings can hold text properties. @xref{Text Properties}.
1148 Many of the specialized editing and I/O facilities of Emacs accept only
1149 strings. For example, you cannot insert a vector of characters into a
1150 buffer the way you can insert a string. @xref{Strings and Characters}.
1153 By contrast, for an array of keyboard input characters (such as a key
1154 sequence), a vector may be necessary, because many keyboard input
1155 characters are outside the range that will fit in a string. @xref{Key
1158 @node Array Functions
1159 @section Functions that Operate on Arrays
1161 In this section, we describe the functions that accept all types of
1164 @defun arrayp object
1165 This function returns @code{t} if @var{object} is an array (i.e., a
1166 vector, a string, a bool-vector or a char-table).
1174 (arrayp (syntax-table)) ;; @r{A char-table.}
1180 @defun aref arr index
1181 @cindex array elements
1182 This function returns the @var{index}th element of the array or record
1183 @var{arr}. The first element is at index zero.
1187 (setq primes [2 3 5 7 11 13])
1188 @result{} [2 3 5 7 11 13]
1194 @result{} 98 ; @r{@samp{b} is @acronym{ASCII} code 98.}
1198 See also the function @code{elt}, in @ref{Sequence Functions}.
1201 @defun aset array index object
1202 This function sets the @var{index}th element of @var{array} to be
1203 @var{object}. It returns @var{object}.
1207 (setq w [foo bar baz])
1208 @result{} [foo bar baz]
1212 @result{} [fu bar baz]
1217 @result{} "asdfasfd"
1221 @result{} "asdZasfd"
1225 If @var{array} is a string and @var{object} is not a character, a
1226 @code{wrong-type-argument} error results. The function converts a
1227 unibyte string to multibyte if necessary to insert a character.
1230 @defun fillarray array object
1231 This function fills the array @var{array} with @var{object}, so that
1232 each element of @var{array} is @var{object}. It returns @var{array}.
1236 (setq a [a b c d e f g])
1237 @result{} [a b c d e f g]
1239 @result{} [0 0 0 0 0 0 0]
1241 @result{} [0 0 0 0 0 0 0]
1244 (setq s "When in the course")
1245 @result{} "When in the course"
1247 @result{} "------------------"
1251 If @var{array} is a string and @var{object} is not a character, a
1252 @code{wrong-type-argument} error results.
1255 The general sequence functions @code{copy-sequence} and @code{length}
1256 are often useful for objects known to be arrays. @xref{Sequence Functions}.
1260 @cindex vector (type)
1262 A @dfn{vector} is a general-purpose array whose elements can be any
1263 Lisp objects. (By contrast, the elements of a string can only be
1264 characters. @xref{Strings and Characters}.) Vectors are used in
1265 Emacs for many purposes: as key sequences (@pxref{Key Sequences}), as
1266 symbol-lookup tables (@pxref{Creating Symbols}), as part of the
1267 representation of a byte-compiled function (@pxref{Byte Compilation}),
1270 Like other arrays, vectors use zero-origin indexing: the first
1271 element has index 0.
1273 Vectors are printed with square brackets surrounding the elements.
1274 Thus, a vector whose elements are the symbols @code{a}, @code{b} and
1275 @code{a} is printed as @code{[a b a]}. You can write vectors in the
1276 same way in Lisp input.
1278 A vector, like a string or a number, is considered a constant for
1279 evaluation: the result of evaluating it is the same vector. This does
1280 not evaluate or even examine the elements of the vector.
1281 @xref{Self-Evaluating Forms}.
1283 Here are examples illustrating these principles:
1287 (setq avector [1 two '(three) "four" [five]])
1288 @result{} [1 two (quote (three)) "four" [five]]
1290 @result{} [1 two (quote (three)) "four" [five]]
1291 (eq avector (eval avector))
1296 @node Vector Functions
1297 @section Functions for Vectors
1299 Here are some functions that relate to vectors:
1301 @defun vectorp object
1302 This function returns @code{t} if @var{object} is a vector.
1314 @defun vector &rest objects
1315 This function creates and returns a vector whose elements are the
1316 arguments, @var{objects}.
1320 (vector 'foo 23 [bar baz] "rats")
1321 @result{} [foo 23 [bar baz] "rats"]
1328 @defun make-vector length object
1329 This function returns a new vector consisting of @var{length} elements,
1330 each initialized to @var{object}.
1334 (setq sleepy (make-vector 9 'Z))
1335 @result{} [Z Z Z Z Z Z Z Z Z]
1340 @defun vconcat &rest sequences
1341 @cindex copying vectors
1342 This function returns a new vector containing all the elements of
1343 @var{sequences}. The arguments @var{sequences} may be true lists,
1344 vectors, strings or bool-vectors. If no @var{sequences} are given,
1345 the empty vector is returned.
1347 The value is either the empty vector, or is a newly constructed
1348 nonempty vector that is not @code{eq} to any existing vector.
1352 (setq a (vconcat '(A B C) '(D E F)))
1353 @result{} [A B C D E F]
1360 (vconcat [A B C] "aa" '(foo (6 7)))
1361 @result{} [A B C 97 97 foo (6 7)]
1365 The @code{vconcat} function also allows byte-code function objects as
1366 arguments. This is a special feature to make it easy to access the entire
1367 contents of a byte-code function object. @xref{Byte-Code Objects}.
1369 For other concatenation functions, see @code{mapconcat} in @ref{Mapping
1370 Functions}, @code{concat} in @ref{Creating Strings}, and @code{append}
1371 in @ref{Building Lists}.
1374 The @code{append} function also provides a way to convert a vector into a
1375 list with the same elements:
1379 (setq avector [1 two (quote (three)) "four" [five]])
1380 @result{} [1 two (quote (three)) "four" [five]]
1381 (append avector nil)
1382 @result{} (1 two (quote (three)) "four" [five])
1387 @section Char-Tables
1389 @cindex extra slots of char-table
1391 A char-table is much like a vector, except that it is indexed by
1392 character codes. Any valid character code, without modifiers, can be
1393 used as an index in a char-table. You can access a char-table's
1394 elements with @code{aref} and @code{aset}, as with any array. In
1395 addition, a char-table can have @dfn{extra slots} to hold additional
1396 data not associated with particular character codes. Like vectors,
1397 char-tables are constants when evaluated, and can hold elements of any
1400 @cindex subtype of char-table
1401 Each char-table has a @dfn{subtype}, a symbol, which serves two
1406 The subtype provides an easy way to tell what the char-table is for.
1407 For instance, display tables are char-tables with @code{display-table}
1408 as the subtype, and syntax tables are char-tables with
1409 @code{syntax-table} as the subtype. The subtype can be queried using
1410 the function @code{char-table-subtype}, described below.
1413 The subtype controls the number of @dfn{extra slots} in the
1414 char-table. This number is specified by the subtype's
1415 @code{char-table-extra-slots} symbol property (@pxref{Symbol
1416 Properties}), whose value should be an integer between 0 and 10. If
1417 the subtype has no such symbol property, the char-table has no extra
1421 @cindex parent of char-table
1422 A char-table can have a @dfn{parent}, which is another char-table. If
1423 it does, then whenever the char-table specifies @code{nil} for a
1424 particular character @var{c}, it inherits the value specified in the
1425 parent. In other words, @code{(aref @var{char-table} @var{c})} returns
1426 the value from the parent of @var{char-table} if @var{char-table} itself
1427 specifies @code{nil}.
1429 @cindex default value of char-table
1430 A char-table can also have a @dfn{default value}. If so, then
1431 @code{(aref @var{char-table} @var{c})} returns the default value
1432 whenever the char-table does not specify any other non-@code{nil} value.
1434 @defun make-char-table subtype &optional init
1435 Return a newly-created char-table, with subtype @var{subtype} (a
1436 symbol). Each element is initialized to @var{init}, which defaults to
1437 @code{nil}. You cannot alter the subtype of a char-table after the
1438 char-table is created.
1440 There is no argument to specify the length of the char-table, because
1441 all char-tables have room for any valid character code as an index.
1443 If @var{subtype} has the @code{char-table-extra-slots} symbol
1444 property, that specifies the number of extra slots in the char-table.
1445 This should be an integer between 0 and 10; otherwise,
1446 @code{make-char-table} raises an error. If @var{subtype} has no
1447 @code{char-table-extra-slots} symbol property (@pxref{Property
1448 Lists}), the char-table has no extra slots.
1451 @defun char-table-p object
1452 This function returns @code{t} if @var{object} is a char-table, and
1453 @code{nil} otherwise.
1456 @defun char-table-subtype char-table
1457 This function returns the subtype symbol of @var{char-table}.
1460 There is no special function to access default values in a char-table.
1461 To do that, use @code{char-table-range} (see below).
1463 @defun char-table-parent char-table
1464 This function returns the parent of @var{char-table}. The parent is
1465 always either @code{nil} or another char-table.
1468 @defun set-char-table-parent char-table new-parent
1469 This function sets the parent of @var{char-table} to @var{new-parent}.
1472 @defun char-table-extra-slot char-table n
1473 This function returns the contents of extra slot @var{n} (zero based)
1474 of @var{char-table}. The number of extra slots in a char-table is
1475 determined by its subtype.
1478 @defun set-char-table-extra-slot char-table n value
1479 This function stores @var{value} in extra slot @var{n} (zero based) of
1483 A char-table can specify an element value for a single character code;
1484 it can also specify a value for an entire character set.
1486 @defun char-table-range char-table range
1487 This returns the value specified in @var{char-table} for a range of
1488 characters @var{range}. Here are the possibilities for @var{range}:
1492 Refers to the default value.
1495 Refers to the element for character @var{char}
1496 (supposing @var{char} is a valid character code).
1498 @item @code{(@var{from} . @var{to})}
1499 A cons cell refers to all the characters in the inclusive range
1500 @samp{[@var{from}..@var{to}]}.
1504 @defun set-char-table-range char-table range value
1505 This function sets the value in @var{char-table} for a range of
1506 characters @var{range}. Here are the possibilities for @var{range}:
1510 Refers to the default value.
1513 Refers to the whole range of character codes.
1516 Refers to the element for character @var{char}
1517 (supposing @var{char} is a valid character code).
1519 @item @code{(@var{from} . @var{to})}
1520 A cons cell refers to all the characters in the inclusive range
1521 @samp{[@var{from}..@var{to}]}.
1525 @defun map-char-table function char-table
1526 This function calls its argument @var{function} for each element of
1527 @var{char-table} that has a non-@code{nil} value. The call to
1528 @var{function} is with two arguments, a key and a value. The key
1529 is a possible @var{range} argument for @code{char-table-range}---either
1530 a valid character or a cons cell @code{(@var{from} . @var{to})},
1531 specifying a range of characters that share the same value. The value is
1532 what @code{(char-table-range @var{char-table} @var{key})} returns.
1534 Overall, the key-value pairs passed to @var{function} describe all the
1535 values stored in @var{char-table}.
1537 The return value is always @code{nil}; to make calls to
1538 @code{map-char-table} useful, @var{function} should have side effects.
1539 For example, here is how to examine the elements of the syntax table:
1544 #'(lambda (key value)
1548 (list (car key) (cdr key))
1555 (((2597602 4194303) (2)) ((2597523 2597601) (3))
1556 ... (65379 (5 . 65378)) (65378 (4 . 65379)) (65377 (1))
1557 ... (12 (0)) (11 (3)) (10 (12)) (9 (0)) ((0 8) (3)))
1562 @section Bool-vectors
1563 @cindex Bool-vectors
1565 A bool-vector is much like a vector, except that it stores only the
1566 values @code{t} and @code{nil}. If you try to store any non-@code{nil}
1567 value into an element of the bool-vector, the effect is to store
1568 @code{t} there. As with all arrays, bool-vector indices start from 0,
1569 and the length cannot be changed once the bool-vector is created.
1570 Bool-vectors are constants when evaluated.
1572 Several functions work specifically with bool-vectors; aside
1573 from that, you manipulate them with same functions used for other kinds
1576 @defun make-bool-vector length initial
1577 Return a new bool-vector of @var{length} elements,
1578 each one initialized to @var{initial}.
1581 @defun bool-vector &rest objects
1582 This function creates and returns a bool-vector whose elements are the
1583 arguments, @var{objects}.
1586 @defun bool-vector-p object
1587 This returns @code{t} if @var{object} is a bool-vector,
1588 and @code{nil} otherwise.
1591 There are also some bool-vector set operation functions, described below:
1593 @defun bool-vector-exclusive-or a b &optional c
1594 Return @dfn{bitwise exclusive or} of bool vectors @var{a} and @var{b}.
1595 If optional argument @var{c} is given, the result of this operation is
1596 stored into @var{c}. All arguments should be bool vectors of the same length.
1599 @defun bool-vector-union a b &optional c
1600 Return @dfn{bitwise or} of bool vectors @var{a} and @var{b}. If
1601 optional argument @var{c} is given, the result of this operation is
1602 stored into @var{c}. All arguments should be bool vectors of the same length.
1605 @defun bool-vector-intersection a b &optional c
1606 Return @dfn{bitwise and} of bool vectors @var{a} and @var{b}. If
1607 optional argument @var{c} is given, the result of this operation is
1608 stored into @var{c}. All arguments should be bool vectors of the same length.
1611 @defun bool-vector-set-difference a b &optional c
1612 Return @dfn{set difference} of bool vectors @var{a} and @var{b}. If
1613 optional argument @var{c} is given, the result of this operation is
1614 stored into @var{c}. All arguments should be bool vectors of the same length.
1617 @defun bool-vector-not a &optional b
1618 Return @dfn{set complement} of bool vector @var{a}. If optional
1619 argument @var{b} is given, the result of this operation is stored into
1620 @var{b}. All arguments should be bool vectors of the same length.
1623 @defun bool-vector-subsetp a b
1624 Return @code{t} if every @code{t} value in @var{a} is also t in
1625 @var{b}, @code{nil} otherwise. All arguments should be bool vectors of the
1629 @defun bool-vector-count-consecutive a b i
1630 Return the number of consecutive elements in @var{a} equal @var{b}
1631 starting at @var{i}. @code{a} is a bool vector, @var{b} is @code{t}
1632 or @code{nil}, and @var{i} is an index into @code{a}.
1635 @defun bool-vector-count-population a
1636 Return the number of elements that are @code{t} in bool vector @var{a}.
1639 The printed form represents up to 8 boolean values as a single
1644 (bool-vector t nil t nil)
1651 You can use @code{vconcat} to print a bool-vector like other vectors:
1655 (vconcat (bool-vector nil t nil t))
1656 @result{} [nil t nil t]
1660 Here is another example of creating, examining, and updating a
1664 (setq bv (make-bool-vector 5 t))
1675 These results make sense because the binary codes for control-_ and
1676 control-W are 11111 and 10111, respectively.
1679 @section Managing a Fixed-Size Ring of Objects
1681 @cindex ring data structure
1682 A @dfn{ring} is a fixed-size data structure that supports insertion,
1683 deletion, rotation, and modulo-indexed reference and traversal. An
1684 efficient ring data structure is implemented by the @code{ring}
1685 package. It provides the functions listed in this section.
1687 Note that several rings in Emacs, like the kill ring and the
1688 mark ring, are actually implemented as simple lists, @emph{not} using
1689 the @code{ring} package; thus the following functions won't work on
1692 @defun make-ring size
1693 This returns a new ring capable of holding @var{size} objects.
1694 @var{size} should be an integer.
1697 @defun ring-p object
1698 This returns @code{t} if @var{object} is a ring, @code{nil} otherwise.
1701 @defun ring-size ring
1702 This returns the maximum capacity of the @var{ring}.
1705 @defun ring-length ring
1706 This returns the number of objects that @var{ring} currently contains.
1707 The value will never exceed that returned by @code{ring-size}.
1710 @defun ring-elements ring
1711 This returns a list of the objects in @var{ring}, in order, newest first.
1714 @defun ring-copy ring
1715 This returns a new ring which is a copy of @var{ring}.
1716 The new ring contains the same (@code{eq}) objects as @var{ring}.
1719 @defun ring-empty-p ring
1720 This returns @code{t} if @var{ring} is empty, @code{nil} otherwise.
1723 The newest element in the ring always has index 0. Higher indices
1724 correspond to older elements. Indices are computed modulo the ring
1725 length. Index @minus{}1 corresponds to the oldest element, @minus{}2
1726 to the next-oldest, and so forth.
1728 @defun ring-ref ring index
1729 This returns the object in @var{ring} found at index @var{index}.
1730 @var{index} may be negative or greater than the ring length. If
1731 @var{ring} is empty, @code{ring-ref} signals an error.
1734 @defun ring-insert ring object
1735 This inserts @var{object} into @var{ring}, making it the newest
1736 element, and returns @var{object}.
1738 If the ring is full, insertion removes the oldest element to
1739 make room for the new element.
1742 @defun ring-remove ring &optional index
1743 Remove an object from @var{ring}, and return that object. The
1744 argument @var{index} specifies which item to remove; if it is
1745 @code{nil}, that means to remove the oldest item. If @var{ring} is
1746 empty, @code{ring-remove} signals an error.
1749 @defun ring-insert-at-beginning ring object
1750 This inserts @var{object} into @var{ring}, treating it as the oldest
1751 element. The return value is not significant.
1753 If the ring is full, this function removes the newest element to make
1754 room for the inserted element.
1757 @cindex fifo data structure
1758 If you are careful not to exceed the ring size, you can
1759 use the ring as a first-in-first-out queue. For example:
1762 (let ((fifo (make-ring 5)))
1763 (mapc (lambda (obj) (ring-insert fifo obj))
1765 (list (ring-remove fifo) t
1766 (ring-remove fifo) t
1767 (ring-remove fifo)))
1768 @result{} (0 t one t "two")