annotate lispref/sequences.texi @ 11376:44f6ea27bac4

(create_process): Set PROCESS's tty_name to Qnil when !PTY_FLAG, so Fprocess_tty_name returns nil for pipes as its docstring says.
author Roland McGrath <roland@gnu.org>
date Tue, 11 Apr 1995 22:46:18 +0000
parents 19bd3bfc57de
children 73dc8205d259
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6374
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1 @c -*-texinfo-*-
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2 @c This is part of the GNU Emacs Lisp Reference Manual.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
4 @c See the file elisp.texi for copying conditions.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5 @setfilename ../info/sequences
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
6 @node Sequences Arrays Vectors, Symbols, Lists, Top
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
7 @chapter Sequences, Arrays, and Vectors
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8 @cindex sequence
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
9
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10 Recall that the @dfn{sequence} type is the union of three other Lisp
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
11 types: lists, vectors, and strings. In other words, any list is a
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
12 sequence, any vector is a sequence, and any string is a sequence. The
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
13 common property that all sequences have is that each is an ordered
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
14 collection of elements.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15
7119
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
16 An @dfn{array} is a single primitive object that has a slot for each
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
17 elements. All the elements are accessible in constant time, but the
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
18 length of an existing array cannot be changed. Both strings and vectors
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
19 are arrays.
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
20
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
21 A list is a sequence of elements, but it is not a single primitive
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
22 object; it is made of cons cells, one cell per element. Finding the
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
23 @var{n}th element requires looking through @var{n} cons cells, so
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
24 elements farther from the beginning of the list take longer to access.
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
25 But it is possible to add elements to the list, or remove elements.
6374
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
26
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27 The following diagram shows the relationship between these types:
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
28
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
29 @example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
30 @group
7119
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
31 ___________________________________
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
32 | |
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
33 | Sequence |
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
34 | ______ ______________________ |
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
35 | | | | | |
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
36 | | List | | Array | |
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
37 | | | | ________ _______ | |
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
38 | |______| | | | | | | |
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
39 | | | String | | Vector| | |
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
40 | | |________| |_______| | |
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
41 | |______________________| |
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
42 |___________________________________|
6374
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44 @end example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46 The elements of vectors and lists may be any Lisp objects. The
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47 elements of strings are all characters.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49 @menu
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
50 * Sequence Functions:: Functions that accept any kind of sequence.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51 * Arrays:: Characteristics of arrays in Emacs Lisp.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 * Array Functions:: Functions specifically for arrays.
7119
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
53 * Vectors:: Special characteristics of Emacs Lisp vectors.
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
54 * Vector Functions:: Functions specifically for vectors.
6374
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
55 @end menu
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
56
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57 @node Sequence Functions
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58 @section Sequences
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60 In Emacs Lisp, a @dfn{sequence} is either a list, a vector or a
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61 string. The common property that all sequences have is that each is an
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62 ordered collection of elements. This section describes functions that
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63 accept any kind of sequence.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
64
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
65 @defun sequencep object
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 Returns @code{t} if @var{object} is a list, vector, or
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
67 string, @code{nil} otherwise.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68 @end defun
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
69
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
70 @defun copy-sequence sequence
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
71 @cindex copying sequences
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
72 Returns a copy of @var{sequence}. The copy is the same type of object
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
73 as the original sequence, and it has the same elements in the same order.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
74
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75 Storing a new element into the copy does not affect the original
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
76 @var{sequence}, and vice versa. However, the elements of the new
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
77 sequence are not copies; they are identical (@code{eq}) to the elements
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
78 of the original. Therefore, changes made within these elements, as
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
79 found via the copied sequence, are also visible in the original
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
80 sequence.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
81
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
82 If the sequence is a string with text properties, the property list in
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
83 the copy is itself a copy, not shared with the original's property
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
84 list. However, the actual values of the properties are shared.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
85 @xref{Text Properties}.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
86
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
87 See also @code{append} in @ref{Building Lists}, @code{concat} in
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
88 @ref{Creating Strings}, and @code{vconcat} in @ref{Vectors}, for others
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
89 ways to copy sequences.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
90
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
91 @example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
92 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
93 (setq bar '(1 2))
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
94 @result{} (1 2)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
95 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
96 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
97 (setq x (vector 'foo bar))
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
98 @result{} [foo (1 2)]
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
99 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
100 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
101 (setq y (copy-sequence x))
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102 @result{} [foo (1 2)]
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
103 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
104
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
106 (eq x y)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
107 @result{} nil
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
108 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
110 (equal x y)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
111 @result{} t
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
112 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
113 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
114 (eq (elt x 1) (elt y 1))
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
115 @result{} t
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
116 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
117
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
118 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
119 ;; @r{Replacing an element of one sequence.}
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
120 (aset x 0 'quux)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
121 x @result{} [quux (1 2)]
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
122 y @result{} [foo (1 2)]
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
123 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
125 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
126 ;; @r{Modifying the inside of a shared element.}
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
127 (setcar (aref x 1) 69)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
128 x @result{} [quux (69 2)]
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
129 y @result{} [foo (69 2)]
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
130 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
131 @end example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
132 @end defun
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
133
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
134 @defun length sequence
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
135 @cindex string length
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
136 @cindex list length
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
137 @cindex vector length
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
138 @cindex sequence length
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
139 Returns the number of elements in @var{sequence}. If @var{sequence} is
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
140 a cons cell that is not a list (because the final @sc{cdr} is not
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
141 @code{nil}), a @code{wrong-type-argument} error is signaled.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
142
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 @example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
144 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145 (length '(1 2 3))
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146 @result{} 3
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
148 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149 (length ())
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150 @result{} 0
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153 (length "foobar")
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 @result{} 6
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157 (length [1 2 3])
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 @result{} 3
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 @end example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
161 @end defun
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
162
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
163 @defun elt sequence index
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
164 @cindex elements of sequences
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
165 This function returns the element of @var{sequence} indexed by
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166 @var{index}. Legitimate values of @var{index} are integers ranging from
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167 0 up to one less than the length of @var{sequence}. If @var{sequence}
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168 is a list, then out-of-range values of @var{index} return @code{nil};
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169 otherwise, they trigger an @code{args-out-of-range} error.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
171 @example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
172 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
173 (elt [1 2 3 4] 2)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
174 @result{} 3
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
175 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
176 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
177 (elt '(1 2 3 4) 2)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
178 @result{} 3
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
180 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
181 (char-to-string (elt "1234" 2))
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
182 @result{} "3"
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
183 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
184 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
185 (elt [1 2 3 4] 4)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
186 @error{}Args out of range: [1 2 3 4], 4
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
187 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
188 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
189 (elt [1 2 3 4] -1)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
190 @error{}Args out of range: [1 2 3 4], -1
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
191 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
192 @end example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
193
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
194 This function duplicates @code{aref} (@pxref{Array Functions}) and
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195 @code{nth} (@pxref{List Elements}), except that it works for any kind of
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
196 sequence.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
197 @end defun
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
198
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
199 @node Arrays
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
200 @section Arrays
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
201 @cindex array
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
202
7119
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
203 An @dfn{array} object has slots that hold a number of other Lisp
6374
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
204 objects, called the elements of the array. Any element of an array may
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205 be accessed in constant time. In contrast, an element of a list
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
206 requires access time that is proportional to the position of the element
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
207 in the list.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
208
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
209 When you create an array, you must specify how many elements it has.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
210 The amount of space allocated depends on the number of elements.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
211 Therefore, it is impossible to change the size of an array once it is
7119
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
212 created; you cannot add or remove elements. However, you can replace an
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
213 element with a different value.
6374
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
214
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
215 Emacs defines two types of array, both of which are one-dimensional:
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
216 @dfn{strings} and @dfn{vectors}. A vector is a general array; its
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
217 elements can be any Lisp objects. A string is a specialized array; its
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
218 elements must be characters (i.e., integers between 0 and 255). Each
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
219 type of array has its own read syntax. @xref{String Type}, and
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
220 @ref{Vector Type}.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
221
7119
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
222 Both kinds of array share these characteristics:
6374
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
223
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
224 @itemize @bullet
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
225 @item
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
226 The first element of an array has index zero, the second element has
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
227 index 1, and so on. This is called @dfn{zero-origin} indexing. For
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
228 example, an array of four elements has indices 0, 1, 2, @w{and 3}.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
229
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
230 @item
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
231 The elements of an array may be referenced or changed with the functions
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
232 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
233 @end itemize
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
234
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
235 In principle, if you wish to have an array of characters, you could use
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
236 either a string or a vector. In practice, we always choose strings for
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
237 such applications, for four reasons:
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
238
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
239 @itemize @bullet
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
240 @item
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
241 They occupy one-fourth the space of a vector of the same elements.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
242
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
243 @item
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
244 Strings are printed in a way that shows the contents more clearly
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
245 as characters.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
246
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
247 @item
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
248 Strings can hold text properties. @xref{Text Properties}.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
249
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
250 @item
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
251 Many of the specialized editing and I/O facilities of Emacs accept only
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
252 strings. For example, you cannot insert a vector of characters into a
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
253 buffer the way you can insert a string. @xref{Strings and Characters}.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
254 @end itemize
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
255
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
256 @node Array Functions
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
257 @section Functions that Operate on Arrays
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
258
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
259 In this section, we describe the functions that accept both strings
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
260 and vectors.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
261
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
262 @defun arrayp object
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
263 This function returns @code{t} if @var{object} is an array (i.e., either a
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
264 vector or a string).
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
265
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
266 @example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
267 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
268 (arrayp [a])
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
269 @result{} t
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
270 (arrayp "asdf")
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
271 @result{} t
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
272 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
273 @end example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
274 @end defun
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
275
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
276 @defun aref array index
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
277 @cindex array elements
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
278 This function returns the @var{index}th element of @var{array}. The
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
279 first element is at index zero.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
280
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
281 @example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
282 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
283 (setq primes [2 3 5 7 11 13])
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
284 @result{} [2 3 5 7 11 13]
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
285 (aref primes 4)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
286 @result{} 11
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
287 (elt primes 4)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
288 @result{} 11
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
289 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
290
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
291 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
292 (aref "abcdefg" 1)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
293 @result{} 98 ; @r{@samp{b} is @sc{ASCII} code 98.}
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
294 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
295 @end example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
296
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
297 See also the function @code{elt}, in @ref{Sequence Functions}.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
298 @end defun
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
299
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
300 @defun aset array index object
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
301 This function sets the @var{index}th element of @var{array} to be
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
302 @var{object}. It returns @var{object}.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
303
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
304 @example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
305 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
306 (setq w [foo bar baz])
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
307 @result{} [foo bar baz]
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
308 (aset w 0 'fu)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
309 @result{} fu
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
310 w
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
311 @result{} [fu bar baz]
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
312 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
313
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
314 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
315 (setq x "asdfasfd")
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
316 @result{} "asdfasfd"
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
317 (aset x 3 ?Z)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
318 @result{} 90
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
319 x
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
320 @result{} "asdZasfd"
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
321 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
322 @end example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
323
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
324 If @var{array} is a string and @var{object} is not a character, a
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
325 @code{wrong-type-argument} error results.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
326 @end defun
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
327
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
328 @defun fillarray array object
7119
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
329 This function fills the array @var{array} with @var{object}, so that
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
330 each element of @var{array} is @var{object}. It returns @var{array}.
6374
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
331
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
332 @example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
333 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
334 (setq a [a b c d e f g])
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
335 @result{} [a b c d e f g]
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
336 (fillarray a 0)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
337 @result{} [0 0 0 0 0 0 0]
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
338 a
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
339 @result{} [0 0 0 0 0 0 0]
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
340 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
341 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
342 (setq s "When in the course")
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
343 @result{} "When in the course"
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
344 (fillarray s ?-)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
345 @result{} "------------------"
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
346 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
347 @end example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
348
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
349 If @var{array} is a string and @var{object} is not a character, a
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
350 @code{wrong-type-argument} error results.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
351 @end defun
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
352
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
353 The general sequence functions @code{copy-sequence} and @code{length}
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
354 are often useful for objects known to be arrays. @xref{Sequence Functions}.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
355
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
356 @node Vectors
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
357 @section Vectors
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
358 @cindex vector
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
359
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
360 Arrays in Lisp, like arrays in most languages, are blocks of memory
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
361 whose elements can be accessed in constant time. A @dfn{vector} is a
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
362 general-purpose array; its elements can be any Lisp objects. (The other
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
363 kind of array in Emacs Lisp is the @dfn{string}, whose elements must be
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
364 characters.) Vectors in Emacs serve as syntax tables (vectors of
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
365 integers), as obarrays (vectors of symbols), and in keymaps (vectors of
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
366 commands). They are also used internally as part of the representation
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
367 of a byte-compiled function; if you print such a function, you will see
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
368 a vector in it.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
369
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
370 In Emacs Lisp, the indices of the elements of a vector start from zero
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
371 and count up from there.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
372
7119
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
373 Vectors are printed with square brackets surrounding the elements.
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
374 Thus, a vector whose elements are the symbols @code{a}, @code{b} and
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
375 @code{a} is printed as @code{[a b a]}. You can write vectors in the
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
376 same way in Lisp input.
6374
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
377
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
378 A vector, like a string or a number, is considered a constant for
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
379 evaluation: the result of evaluating it is the same vector. This does
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
380 not evaluate or even examine the elements of the vector.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
381 @xref{Self-Evaluating Forms}.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
382
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
383 Here are examples of these principles:
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
384
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
385 @example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
386 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
387 (setq avector [1 two '(three) "four" [five]])
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
388 @result{} [1 two (quote (three)) "four" [five]]
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
389 (eval avector)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
390 @result{} [1 two (quote (three)) "four" [five]]
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
391 (eq avector (eval avector))
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
392 @result{} t
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
393 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
394 @end example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
395
7119
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
396 @node Vector Functions
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
397 @section Functions That Operate on Vectors
19bd3bfc57de entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 6374
diff changeset
398
6374
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
399 Here are some functions that relate to vectors:
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
400
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
401 @defun vectorp object
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
402 This function returns @code{t} if @var{object} is a vector.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
403
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
404 @example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
405 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
406 (vectorp [a])
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
407 @result{} t
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
408 (vectorp "asdf")
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
409 @result{} nil
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
410 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
411 @end example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
412 @end defun
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
413
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
414 @defun vector &rest objects
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
415 This function creates and returns a vector whose elements are the
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
416 arguments, @var{objects}.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
417
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
418 @example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
419 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
420 (vector 'foo 23 [bar baz] "rats")
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
421 @result{} [foo 23 [bar baz] "rats"]
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
422 (vector)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
423 @result{} []
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
424 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
425 @end example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
426 @end defun
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
427
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
428 @defun make-vector length object
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
429 This function returns a new vector consisting of @var{length} elements,
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
430 each initialized to @var{object}.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
431
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
432 @example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
433 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
434 (setq sleepy (make-vector 9 'Z))
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
435 @result{} [Z Z Z Z Z Z Z Z Z]
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
436 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
437 @end example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
438 @end defun
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
439
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
440 @defun vconcat &rest sequences
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
441 @cindex copying vectors
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
442 This function returns a new vector containing all the elements of the
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
443 @var{sequences}. The arguments @var{sequences} may be lists, vectors,
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
444 or strings. If no @var{sequences} are given, an empty vector is
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
445 returned.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
446
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
447 The value is a newly constructed vector that is not @code{eq} to any
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
448 existing vector.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
449
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
450 @example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
451 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
452 (setq a (vconcat '(A B C) '(D E F)))
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
453 @result{} [A B C D E F]
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
454 (eq a (vconcat a))
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
455 @result{} nil
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
456 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
457 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
458 (vconcat)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
459 @result{} []
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
460 (vconcat [A B C] "aa" '(foo (6 7)))
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
461 @result{} [A B C 97 97 foo (6 7)]
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
462 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
463 @end example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
464
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
465 When an argument is an integer (not a sequence of integers), it is
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
466 converted to a string of digits making up the decimal printed
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
467 representation of the integer. This special case exists for
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
468 compatibility with Mocklisp, and we don't recommend you take advantage
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
469 of it. If you want to convert an integer to digits in this way, use
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
470 @code{format} (@pxref{Formatting Strings}) or @code{number-to-string}
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
471 (@pxref{String Conversion}).
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
472
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
473 For other concatenation functions, see @code{mapconcat} in @ref{Mapping
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
474 Functions}, @code{concat} in @ref{Creating Strings}, and @code{append}
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
475 in @ref{Building Lists}.
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
476 @end defun
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
477
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
478 The @code{append} function provides a way to convert a vector into a
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
479 list with the same elements (@pxref{Building Lists}):
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
480
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
481 @example
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
482 @group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
483 (setq avector [1 two (quote (three)) "four" [five]])
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
484 @result{} [1 two (quote (three)) "four" [five]]
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
485 (append avector nil)
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
486 @result{} (1 two (quote (three)) "four" [five])
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
487 @end group
71e61b314fe9 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
488 @end example