2 @c This is part of the XEmacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 @c Copyright (C) 1996 Ben Wing.
5 @c See the file lispref.texi for copying conditions.
6 @setfilename ../../info/sequences.info
7 @node Sequences Arrays Vectors, Symbols, Lists, Top
8 @chapter Sequences, Arrays, and Vectors
11 Recall that the @dfn{sequence} type is the union of four other Lisp
12 types: lists, vectors, bit vectors, and strings. In other words, any
13 list is a sequence, any vector is a sequence, any bit vector is a
14 sequence, and any string is a sequence. The common property that all
15 sequences have is that each is an ordered collection of elements.
17 An @dfn{array} is a single primitive object that has a slot for each
18 elements. All the elements are accessible in constant time, but the
19 length of an existing array cannot be changed. Strings, vectors, and
20 bit vectors are the three types of arrays.
22 A list is a sequence of elements, but it is not a single primitive
23 object; it is made of cons cells, one cell per element. Finding the
24 @var{n}th element requires looking through @var{n} cons cells, so
25 elements farther from the beginning of the list take longer to access.
26 But it is possible to add elements to the list, or remove elements.
28 The following diagram shows the relationship between these types:
32 ___________________________________
35 | ______ ______________________ |
37 | | List | | Array | |
38 | | | | ________ _______ | |
39 | |______| | | | | | | |
40 | | | Vector | | String| | |
41 | | |________| |_______| | |
42 | | __________________ | |
44 | | | Bit Vector | | |
45 | | |__________________| | |
46 | |______________________| |
47 |___________________________________|
51 The elements of vectors and lists may be any Lisp objects. The
52 elements of strings are all characters. The elements of bit vectors
53 are the numbers 0 and 1.
56 * Sequence Functions:: Functions that accept any kind of sequence.
57 * Arrays:: Characteristics of arrays in XEmacs Lisp.
58 * Array Functions:: Functions specifically for arrays.
59 * Vectors:: Special characteristics of XEmacs Lisp vectors.
60 * Vector Functions:: Functions specifically for vectors.
61 * Bit Vectors:: Special characteristics of XEmacs Lisp bit vectors.
62 * Bit Vector Functions:: Functions specifically for bit vectors.
65 @node Sequence Functions
68 In XEmacs Lisp, a @dfn{sequence} is either a list, a vector, a bit
69 vector, or a string. The common property that all sequences have is
70 that each is an ordered collection of elements. This section describes
71 functions that accept any kind of sequence.
73 @defun sequencep object
74 Returns @code{t} if @var{object} is a list, vector, bit vector, or
75 string, @code{nil} otherwise.
78 @defun copy-sequence sequence
79 @cindex copying sequences
80 Returns a copy of @var{sequence}. The copy is the same type of object
81 as the original sequence, and it has the same elements in the same order.
83 Storing a new element into the copy does not affect the original
84 @var{sequence}, and vice versa. However, the elements of the new
85 sequence are not copies; they are identical (@code{eq}) to the elements
86 of the original. Therefore, changes made within these elements, as
87 found via the copied sequence, are also visible in the original
90 If the sequence is a string with extents or text properties, the extents
91 and text properties in the copy are also copied, not shared with the
92 original. (This means that modifying the extents or text properties of
93 the original will not affect the copy.) However, the actual values of
94 the properties are shared. @xref{Extents}, @xref{Text Properties}.
96 See also @code{append} in @ref{Building Lists}, @code{concat} in
97 @ref{Creating Strings}, @code{vconcat} in @ref{Vectors}, and
98 @code{bvconcat} in @ref{Bit Vectors}, for other ways to copy sequences.
106 (setq x (vector 'foo bar))
107 @result{} [foo (1 2)]
110 (setq y (copy-sequence x))
111 @result{} [foo (1 2)]
123 (eq (elt x 1) (elt y 1))
128 ;; @r{Replacing an element of one sequence.}
130 x @result{} [quux (1 2)]
131 y @result{} [foo (1 2)]
135 ;; @r{Modifying the inside of a shared element.}
136 (setcar (aref x 1) 69)
137 x @result{} [quux (69 2)]
138 y @result{} [foo (69 2)]
142 ;; @r{Creating a bit vector.}
143 (bit-vector 1 0 1 1 0 1 0 0)
149 @defun length sequence
150 @cindex string length
152 @cindex vector length
153 @cindex bit vector length
154 @cindex sequence length
155 Returns the number of elements in @var{sequence}. If @var{sequence} is
156 a cons cell that is not a list (because the final @sc{cdr} is not
157 @code{nil}), a @code{wrong-type-argument} error is signaled.
183 @defun elt sequence index
184 @cindex elements of sequences
185 This function returns the element of @var{sequence} indexed by
186 @var{index}. Legitimate values of @var{index} are integers ranging from
187 0 up to one less than the length of @var{sequence}. If @var{sequence}
188 is a list, then out-of-range values of @var{index} return @code{nil};
189 otherwise, they trigger an @code{args-out-of-range} error.
201 (char-to-string (elt "1234" 2))
210 @error{}Args out of range: [1 2 3 4], 4
214 @error{}Args out of range: [1 2 3 4], -1
218 This function generalizes @code{aref} (@pxref{Array Functions}) and
219 @code{nth} (@pxref{List Elements}).
226 An @dfn{array} object has slots that hold a number of other Lisp
227 objects, called the elements of the array. Any element of an array may
228 be accessed in constant time. In contrast, an element of a list
229 requires access time that is proportional to the position of the element
232 When you create an array, you must specify how many elements it has.
233 The amount of space allocated depends on the number of elements.
234 Therefore, it is impossible to change the size of an array once it is
235 created; you cannot add or remove elements. However, you can replace an
236 element with a different value.
238 XEmacs defines three types of array, all of which are one-dimensional:
239 @dfn{strings}, @dfn{vectors}, and @dfn{bit vectors}. A vector is a
240 general array; its elements can be any Lisp objects. A string is a
241 specialized array; its elements must be characters. A bit vector is
242 another specialized array; its elements must be bits (an integer, either
243 0 or 1). Each type of array has its own read syntax. @xref{String
244 Type}, @ref{Vector Type}, and @ref{Bit Vector Type}.
246 All kinds of array share these characteristics:
250 The first element of an array has index zero, the second element has
251 index 1, and so on. This is called @dfn{zero-origin} indexing. For
252 example, an array of four elements has indices 0, 1, 2, @w{and 3}.
255 The elements of an array may be referenced or changed with the functions
256 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
259 In principle, if you wish to have an array of text characters, you
260 could use either a string or a vector. In practice, we always choose
261 strings for such applications, for four reasons:
265 They usually occupy one-fourth the space of a vector of the same
266 elements. (This is one-eighth the space for 64-bit machines such as the
267 DEC Alpha, and may also be different when @sc{mule} support is compiled
271 Strings are printed in a way that shows the contents more clearly
275 Strings can hold extent and text properties. @xref{Extents}, @xref{Text
279 Many of the specialized editing and I/O facilities of XEmacs accept only
280 strings. For example, you cannot insert a vector of characters into a
281 buffer the way you can insert a string. @xref{Strings and Characters}.
284 By contrast, for an array of keyboard input characters (such as a key
285 sequence), a vector may be necessary, because many keyboard input
286 characters are non-printable and are represented with symbols rather than
287 with characters. @xref{Key Sequence Input}.
289 Similarly, when representing an array of bits, a bit vector has
290 the following advantages over a regular vector:
294 They occupy 1/32nd the space of a vector of the same elements.
295 (1/64th on 64-bit machines such as the DEC Alpha.)
298 Bit vectors are printed in a way that shows the contents more clearly
302 @node Array Functions
303 @section Functions that Operate on Arrays
305 In this section, we describe the functions that accept strings, vectors,
309 This function returns @code{t} if @var{object} is an array (i.e., a
310 string, vector, or bit vector).
324 @defun aref array index
325 @cindex array elements
326 This function returns the @var{index}th element of @var{array}. The
327 first element is at index zero.
331 (setq primes [2 3 5 7 11 13])
332 @result{} [2 3 5 7 11 13]
350 See also the function @code{elt}, in @ref{Sequence Functions}.
353 @defun aset array index object
354 This function sets the @var{index}th element of @var{array} to be
355 @var{object}. It returns @var{object}.
359 (setq w [foo bar baz])
360 @result{} [foo bar baz]
364 @result{} [fu bar baz]
386 If @var{array} is a string and @var{object} is not a character, a
387 @code{wrong-type-argument} error results.
390 @defun fillarray array object
391 This function fills the array @var{array} with @var{object}, so that
392 each element of @var{array} is @var{object}. It returns @var{array}.
396 (setq a [a b c d e f g])
397 @result{} [a b c d e f g]
399 @result{} [0 0 0 0 0 0 0]
401 @result{} [0 0 0 0 0 0 0]
405 (setq s "When in the course")
406 @result{} "When in the course"
408 @result{} "------------------"
419 If @var{array} is a string and @var{object} is not a character, a
420 @code{wrong-type-argument} error results.
423 The general sequence functions @code{copy-sequence} and @code{length}
424 are often useful for objects known to be arrays. @xref{Sequence Functions}.
430 Arrays in Lisp, like arrays in most languages, are blocks of memory
431 whose elements can be accessed in constant time. A @dfn{vector} is a
432 general-purpose array; its elements can be any Lisp objects. (The other
433 kind of array in XEmacs Lisp is the @dfn{string}, whose elements must be
434 characters.) Vectors in XEmacs serve as obarrays (vectors of symbols),
435 although this is a shortcoming that should be fixed. They are also used
436 internally as part of the representation of a byte-compiled function; if
437 you print such a function, you will see a vector in it.
439 In XEmacs Lisp, the indices of the elements of a vector start from zero
440 and count up from there.
442 Vectors are printed with square brackets surrounding the elements.
443 Thus, a vector whose elements are the symbols @code{a}, @code{b} and
444 @code{a} is printed as @code{[a b a]}. You can write vectors in the
445 same way in Lisp input.
447 A vector, like a string or a number, is considered a constant for
448 evaluation: the result of evaluating it is the same vector. This does
449 not evaluate or even examine the elements of the vector.
450 @xref{Self-Evaluating Forms}.
452 Here are examples of these principles:
456 (setq avector [1 two '(three) "four" [five]])
457 @result{} [1 two (quote (three)) "four" [five]]
459 @result{} [1 two (quote (three)) "four" [five]]
460 (eq avector (eval avector))
465 @node Vector Functions
466 @section Functions That Operate on Vectors
468 Here are some functions that relate to vectors:
470 @defun vectorp object
471 This function returns @code{t} if @var{object} is a vector.
483 @defun vector &rest objects
484 This function creates and returns a vector whose elements are the
485 arguments, @var{objects}.
489 (vector 'foo 23 [bar baz] "rats")
490 @result{} [foo 23 [bar baz] "rats"]
497 @defun make-vector length object
498 This function returns a new vector consisting of @var{length} elements,
499 each initialized to @var{object}.
503 (setq sleepy (make-vector 9 'Z))
504 @result{} [Z Z Z Z Z Z Z Z Z]
509 @defun vconcat &rest sequences
510 @cindex copying vectors
511 This function returns a new vector containing all the elements of the
512 @var{sequences}. The arguments @var{sequences} may be lists, vectors,
513 or strings. If no @var{sequences} are given, an empty vector is
516 The value is a newly constructed vector that is not @code{eq} to any
521 (setq a (vconcat '(A B C) '(D E F)))
522 @result{} [A B C D E F]
529 (vconcat [A B C] "aa" '(foo (6 7)))
530 @result{} [A B C 97 97 foo (6 7)]
534 The @code{vconcat} function also allows integers as arguments. It
535 converts them to strings of digits, making up the decimal print
536 representation of the integer, and then uses the strings instead of the
537 original integers. @strong{Don't use this feature; we plan to eliminate
538 it. If you already use this feature, change your programs now!} The
539 proper way to convert an integer to a decimal number in this way is with
540 @code{format} (@pxref{Formatting Strings}) or @code{number-to-string}
541 (@pxref{String Conversion}).
543 For other concatenation functions, see @code{mapconcat} in @ref{Mapping
544 Functions}, @code{concat} in @ref{Creating Strings}, @code{append}
545 in @ref{Building Lists}, and @code{bvconcat} in @ref{Bit Vector Functions}.
548 The @code{append} function provides a way to convert a vector into a
549 list with the same elements (@pxref{Building Lists}):
553 (setq avector [1 two (quote (three)) "four" [five]])
554 @result{} [1 two (quote (three)) "four" [five]]
556 @result{} (1 two (quote (three)) "four" [five])
564 Bit vectors are specialized vectors that can only represent arrays
565 of 1's and 0's. Bit vectors have a very efficient representation
566 and are useful for representing sets of boolean (true or false) values.
568 There is no limit on the size of a bit vector. You could, for example,
569 create a bit vector with 100,000 elements if you really wanted to.
571 Bit vectors have a special printed representation consisting of
572 @samp{#*} followed by the bits of the vector. For example, a bit vector
573 whose elements are 0, 1, 1, 0, and 1, respectively, is printed as
579 Bit vectors are considered constants for evaluation, like vectors,
580 strings, and numbers. @xref{Self-Evaluating Forms}.
582 @node Bit Vector Functions
583 @section Functions That Operate on Bit Vectors
585 Here are some functions that relate to bit vectors:
587 @defun bit-vector-p object
588 This function returns @code{t} if @var{object} is a bit vector.
603 This function returns @code{t} if @var{object} is either 0 or 1.
606 @defun bit-vector &rest bits
607 This function creates and returns a bit vector whose elements are the
608 arguments @var{bits}. Each argument must be a bit, i.e. one of the two
613 (bit-vector 0 0 0 1 0 0 0 0 1 0)
614 @result{} #*0001000010
621 @defun make-bit-vector length bit
622 This function creates and returns a bit vector consisting of
623 @var{length} elements, each initialized to @var{bit}, which must be
624 one of the two integers 0 or 1.
628 (setq picket-fence (make-bit-vector 9 1))
629 @result{} #*111111111
634 @defun bvconcat &rest sequences
635 @cindex copying bit vectors
636 This function returns a new bit vector containing all the elements of
637 the @var{sequences}. The arguments @var{sequences} may be lists,
638 vectors, or bit vectors, all of whose elements are the integers 0 or 1.
639 If no @var{sequences} are given, an empty bit vector is returned.
641 The value is a newly constructed bit vector that is not @code{eq} to any
646 (setq a (bvconcat '(1 1 0) '(0 0 1)))
654 (bvconcat [1 0 0 0 0] #*111 '(0 0 0 0 1))
655 @result{} #*1000011100001
659 For other concatenation functions, see @code{mapconcat} in @ref{Mapping
660 Functions}, @code{concat} in @ref{Creating Strings}, @code{vconcat} in
661 @ref{Vector Functions}, and @code{append} in @ref{Building Lists}.
664 The @code{append} function provides a way to convert a bit vector into a
665 list with the same elements (@pxref{Building Lists}):
672 @result{} (0 0 0 0 1 1 1 0)