(recentf): Added version tag to the defgroup of
[bpt/emacs.git] / lispref / sequences.texi
CommitLineData
4672ee8f
RS
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
fd897522
GM
3@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999
4@c Free Software Foundation, Inc.
4672ee8f
RS
5@c See the file elisp.texi for copying conditions.
6@setfilename ../info/sequences
8241495d 7@node Sequences Arrays Vectors, Hash Tables, Lists, Top
4672ee8f
RS
8@chapter Sequences, Arrays, and Vectors
9@cindex sequence
10
969fe9b5
RS
11 Recall that the @dfn{sequence} type is the union of two other Lisp
12types: lists and arrays. In other words, any list is a sequence, and
13any array is a sequence. The common property that all sequences have is
14that each is an ordered collection of elements.
4672ee8f 15
79d11238 16 An @dfn{array} is a single primitive object that has a slot for each
969fe9b5
RS
17of its elements. All the elements are accessible in constant time, but
18the length of an existing array cannot be changed. Strings, vectors,
19char-tables and bool-vectors are the four types of arrays.
79d11238
RS
20
21 A list is a sequence of elements, but it is not a single primitive
22object; it is made of cons cells, one cell per element. Finding the
23@var{n}th element requires looking through @var{n} cons cells, so
24elements farther from the beginning of the list take longer to access.
25But it is possible to add elements to the list, or remove elements.
4672ee8f
RS
26
27 The following diagram shows the relationship between these types:
28
29@example
30@group
969fe9b5
RS
31 _____________________________________________
32 | |
33 | Sequence |
34 | ______ ________________________________ |
35 | | | | | |
36 | | List | | Array | |
37 | | | | ________ ________ | |
38 | |______| | | | | | | |
39 | | | Vector | | String | | |
40 | | |________| |________| | |
41 | | ____________ _____________ | |
42 | | | | | | | |
43 | | | Char-table | | Bool-vector | | |
44 | | |____________| |_____________| | |
45 | |________________________________| |
46 |_____________________________________________|
4672ee8f
RS
47@end group
48@end example
49
50 The elements of vectors and lists may be any Lisp objects. The
51elements of strings are all characters.
52
53@menu
54* Sequence Functions:: Functions that accept any kind of sequence.
55* Arrays:: Characteristics of arrays in Emacs Lisp.
56* Array Functions:: Functions specifically for arrays.
79d11238
RS
57* Vectors:: Special characteristics of Emacs Lisp vectors.
58* Vector Functions:: Functions specifically for vectors.
f9f59935
RS
59* Char-Tables:: How to work with char-tables.
60* Bool-Vectors:: How to work with bool-vectors.
4672ee8f
RS
61@end menu
62
63@node Sequence Functions
64@section Sequences
65
969fe9b5
RS
66 In Emacs Lisp, a @dfn{sequence} is either a list or an array. The
67common property of all sequences is that they are ordered collections of
68elements. This section describes functions that accept any kind of
69sequence.
4672ee8f
RS
70
71@defun sequencep object
72Returns @code{t} if @var{object} is a list, vector, or
73string, @code{nil} otherwise.
74@end defun
75
969fe9b5
RS
76@defun length sequence
77@cindex string length
78@cindex list length
79@cindex vector length
80@cindex sequence length
81This function returns the number of elements in @var{sequence}. If
82@var{sequence} is a cons cell that is not a list (because the final
83@sc{cdr} is not @code{nil}), a @code{wrong-type-argument} error is
84signaled.
85
a9f0a989 86@xref{List Elements}, for the related function @code{safe-length}.
969fe9b5
RS
87
88@example
89@group
90(length '(1 2 3))
91 @result{} 3
92@end group
93@group
94(length ())
95 @result{} 0
96@end group
97@group
98(length "foobar")
99 @result{} 6
100@end group
101@group
102(length [1 2 3])
103 @result{} 3
104@end group
105@group
106(length (make-bool-vector 5 nil))
107 @result{} 5
108@end group
109@end example
110@end defun
111
112@defun elt sequence index
113@cindex elements of sequences
114This function returns the element of @var{sequence} indexed by
115@var{index}. Legitimate values of @var{index} are integers ranging from
1160 up to one less than the length of @var{sequence}. If @var{sequence}
117is a list, then out-of-range values of @var{index} return @code{nil};
118otherwise, they trigger an @code{args-out-of-range} error.
119
120@example
121@group
122(elt [1 2 3 4] 2)
123 @result{} 3
124@end group
125@group
126(elt '(1 2 3 4) 2)
127 @result{} 3
128@end group
129@group
130;; @r{We use @code{string} to show clearly which character @code{elt} returns.}
131(string (elt "1234" 2))
132 @result{} "3"
133@end group
134@group
135(elt [1 2 3 4] 4)
a9f0a989 136 @error{} Args out of range: [1 2 3 4], 4
969fe9b5
RS
137@end group
138@group
139(elt [1 2 3 4] -1)
a9f0a989 140 @error{} Args out of range: [1 2 3 4], -1
969fe9b5
RS
141@end group
142@end example
143
144This function generalizes @code{aref} (@pxref{Array Functions}) and
145@code{nth} (@pxref{List Elements}).
146@end defun
147
4672ee8f
RS
148@defun copy-sequence sequence
149@cindex copying sequences
150Returns a copy of @var{sequence}. The copy is the same type of object
151as the original sequence, and it has the same elements in the same order.
152
153Storing a new element into the copy does not affect the original
154@var{sequence}, and vice versa. However, the elements of the new
155sequence are not copies; they are identical (@code{eq}) to the elements
156of the original. Therefore, changes made within these elements, as
157found via the copied sequence, are also visible in the original
158sequence.
159
160If the sequence is a string with text properties, the property list in
161the copy is itself a copy, not shared with the original's property
162list. However, the actual values of the properties are shared.
163@xref{Text Properties}.
164
165See also @code{append} in @ref{Building Lists}, @code{concat} in
d699a7ad 166@ref{Creating Strings}, and @code{vconcat} in @ref{Vectors}, for other
4672ee8f
RS
167ways to copy sequences.
168
169@example
170@group
171(setq bar '(1 2))
172 @result{} (1 2)
173@end group
174@group
175(setq x (vector 'foo bar))
176 @result{} [foo (1 2)]
177@end group
178@group
179(setq y (copy-sequence x))
180 @result{} [foo (1 2)]
181@end group
182
183@group
184(eq x y)
185 @result{} nil
186@end group
187@group
188(equal x y)
189 @result{} t
190@end group
191@group
192(eq (elt x 1) (elt y 1))
193 @result{} t
194@end group
195
196@group
197;; @r{Replacing an element of one sequence.}
198(aset x 0 'quux)
199x @result{} [quux (1 2)]
200y @result{} [foo (1 2)]
201@end group
202
203@group
204;; @r{Modifying the inside of a shared element.}
205(setcar (aref x 1) 69)
206x @result{} [quux (69 2)]
207y @result{} [foo (69 2)]
208@end group
209@end example
210@end defun
211
4672ee8f
RS
212@node Arrays
213@section Arrays
214@cindex array
215
79d11238 216 An @dfn{array} object has slots that hold a number of other Lisp
4672ee8f
RS
217objects, called the elements of the array. Any element of an array may
218be accessed in constant time. In contrast, an element of a list
219requires access time that is proportional to the position of the element
220in the list.
221
a9f0a989
RS
222 Emacs defines four types of array, all one-dimensional: @dfn{strings},
223@dfn{vectors}, @dfn{bool-vectors} and @dfn{char-tables}. A vector is a
224general array; its elements can be any Lisp objects. A string is a
2bdedac1
GM
225specialized array; its elements must be characters. Each type of array
226has its own read syntax.
a9f0a989 227@xref{String Type}, and @ref{Vector Type}.
4672ee8f 228
969fe9b5 229 All four kinds of array share these characteristics:
4672ee8f
RS
230
231@itemize @bullet
232@item
233The first element of an array has index zero, the second element has
234index 1, and so on. This is called @dfn{zero-origin} indexing. For
235example, an array of four elements has indices 0, 1, 2, @w{and 3}.
236
969fe9b5
RS
237@item
238The length of the array is fixed once you create it; you cannot
239change the length of an existing array.
240
241@item
242The array is a constant, for evaluation---in other words, it evaluates
243to itself.
244
4672ee8f
RS
245@item
246The elements of an array may be referenced or changed with the functions
247@code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
248@end itemize
249
969fe9b5
RS
250 When you create an array, other than a char-table, you must specify
251its length. You cannot specify the length of a char-table, because that
252is determined by the range of character codes.
253
254 In principle, if you want an array of text characters, you could use
255either a string or a vector. In practice, we always choose strings for
256such applications, for four reasons:
4672ee8f
RS
257
258@itemize @bullet
259@item
260They occupy one-fourth the space of a vector of the same elements.
261
262@item
263Strings are printed in a way that shows the contents more clearly
f9f59935 264as text.
4672ee8f
RS
265
266@item
267Strings can hold text properties. @xref{Text Properties}.
268
269@item
270Many of the specialized editing and I/O facilities of Emacs accept only
271strings. For example, you cannot insert a vector of characters into a
272buffer the way you can insert a string. @xref{Strings and Characters}.
273@end itemize
274
bfe721d1
KH
275 By contrast, for an array of keyboard input characters (such as a key
276sequence), a vector may be necessary, because many keyboard input
277characters are outside the range that will fit in a string. @xref{Key
278Sequence Input}.
279
4672ee8f
RS
280@node Array Functions
281@section Functions that Operate on Arrays
282
f9f59935
RS
283 In this section, we describe the functions that accept all types of
284arrays.
4672ee8f
RS
285
286@defun arrayp object
f9f59935
RS
287This function returns @code{t} if @var{object} is an array (i.e., a
288vector, a string, a bool-vector or a char-table).
4672ee8f
RS
289
290@example
291@group
292(arrayp [a])
969fe9b5 293 @result{} t
4672ee8f 294(arrayp "asdf")
969fe9b5
RS
295 @result{} t
296(arrayp (syntax-table)) ;; @r{A char-table.}
297 @result{} t
4672ee8f
RS
298@end group
299@end example
300@end defun
301
302@defun aref array index
303@cindex array elements
304This function returns the @var{index}th element of @var{array}. The
305first element is at index zero.
306
307@example
308@group
309(setq primes [2 3 5 7 11 13])
310 @result{} [2 3 5 7 11 13]
311(aref primes 4)
312 @result{} 11
4672ee8f 313@end group
4672ee8f
RS
314@group
315(aref "abcdefg" 1)
8241495d 316 @result{} 98 ; @r{@samp{b} is @sc{ascii} code 98.}
4672ee8f
RS
317@end group
318@end example
319
320See also the function @code{elt}, in @ref{Sequence Functions}.
321@end defun
322
323@defun aset array index object
324This function sets the @var{index}th element of @var{array} to be
325@var{object}. It returns @var{object}.
326
327@example
328@group
329(setq w [foo bar baz])
330 @result{} [foo bar baz]
331(aset w 0 'fu)
332 @result{} fu
333w
334 @result{} [fu bar baz]
335@end group
336
337@group
338(setq x "asdfasfd")
339 @result{} "asdfasfd"
340(aset x 3 ?Z)
341 @result{} 90
342x
343 @result{} "asdZasfd"
344@end group
345@end example
346
347If @var{array} is a string and @var{object} is not a character, a
f9f59935
RS
348@code{wrong-type-argument} error results. If @var{array} is a string
349and @var{object} is character, but @var{object} does not use the same
350number of bytes as the character currently stored in @code{(aref
a9f0a989
RS
351@var{object} @var{index})}, that is also an error. @xref{Splitting
352Characters}.
4672ee8f
RS
353@end defun
354
355@defun fillarray array object
79d11238
RS
356This function fills the array @var{array} with @var{object}, so that
357each element of @var{array} is @var{object}. It returns @var{array}.
4672ee8f
RS
358
359@example
360@group
361(setq a [a b c d e f g])
362 @result{} [a b c d e f g]
363(fillarray a 0)
364 @result{} [0 0 0 0 0 0 0]
365a
366 @result{} [0 0 0 0 0 0 0]
367@end group
368@group
369(setq s "When in the course")
370 @result{} "When in the course"
371(fillarray s ?-)
372 @result{} "------------------"
373@end group
374@end example
375
376If @var{array} is a string and @var{object} is not a character, a
377@code{wrong-type-argument} error results.
378@end defun
379
380The general sequence functions @code{copy-sequence} and @code{length}
381are often useful for objects known to be arrays. @xref{Sequence Functions}.
382
383@node Vectors
384@section Vectors
385@cindex vector
386
387 Arrays in Lisp, like arrays in most languages, are blocks of memory
388whose elements can be accessed in constant time. A @dfn{vector} is a
969fe9b5
RS
389general-purpose array of specified length; its elements can be any Lisp
390objects. (By contrast, a string can hold only characters as elements.)
391Vectors in Emacs are used for obarrays (vectors of symbols), and as part
392of keymaps (vectors of commands). They are also used internally as part
393of the representation of a byte-compiled function; if you print such a
f9f59935 394function, you will see a vector in it.
4672ee8f
RS
395
396 In Emacs Lisp, the indices of the elements of a vector start from zero
397and count up from there.
398
79d11238
RS
399 Vectors are printed with square brackets surrounding the elements.
400Thus, a vector whose elements are the symbols @code{a}, @code{b} and
401@code{a} is printed as @code{[a b a]}. You can write vectors in the
402same way in Lisp input.
4672ee8f
RS
403
404 A vector, like a string or a number, is considered a constant for
405evaluation: the result of evaluating it is the same vector. This does
406not evaluate or even examine the elements of the vector.
407@xref{Self-Evaluating Forms}.
408
f9f59935 409 Here are examples illustrating these principles:
4672ee8f
RS
410
411@example
412@group
413(setq avector [1 two '(three) "four" [five]])
414 @result{} [1 two (quote (three)) "four" [five]]
415(eval avector)
416 @result{} [1 two (quote (three)) "four" [five]]
417(eq avector (eval avector))
418 @result{} t
419@end group
420@end example
421
79d11238 422@node Vector Functions
969fe9b5 423@section Functions for Vectors
79d11238 424
4672ee8f
RS
425 Here are some functions that relate to vectors:
426
427@defun vectorp object
428This function returns @code{t} if @var{object} is a vector.
429
430@example
431@group
432(vectorp [a])
433 @result{} t
434(vectorp "asdf")
435 @result{} nil
436@end group
437@end example
438@end defun
439
440@defun vector &rest objects
441This function creates and returns a vector whose elements are the
442arguments, @var{objects}.
443
444@example
445@group
446(vector 'foo 23 [bar baz] "rats")
447 @result{} [foo 23 [bar baz] "rats"]
448(vector)
449 @result{} []
450@end group
451@end example
452@end defun
453
454@defun make-vector length object
455This function returns a new vector consisting of @var{length} elements,
456each initialized to @var{object}.
457
458@example
459@group
460(setq sleepy (make-vector 9 'Z))
461 @result{} [Z Z Z Z Z Z Z Z Z]
462@end group
463@end example
464@end defun
465
466@defun vconcat &rest sequences
467@cindex copying vectors
468This function returns a new vector containing all the elements of the
f9f59935
RS
469@var{sequences}. The arguments @var{sequences} may be any kind of
470arrays, including lists, vectors, or strings. If no @var{sequences} are
471given, an empty vector is returned.
4672ee8f
RS
472
473The value is a newly constructed vector that is not @code{eq} to any
474existing vector.
475
476@example
477@group
478(setq a (vconcat '(A B C) '(D E F)))
479 @result{} [A B C D E F]
480(eq a (vconcat a))
481 @result{} nil
482@end group
483@group
484(vconcat)
485 @result{} []
486(vconcat [A B C] "aa" '(foo (6 7)))
487 @result{} [A B C 97 97 foo (6 7)]
488@end group
489@end example
490
969fe9b5
RS
491The @code{vconcat} function also allows byte-code function objects as
492arguments. This is a special feature to make it easy to access the entire
493contents of a byte-code function object. @xref{Byte-Code Objects}.
494
22697dac
KH
495The @code{vconcat} function also allows integers as arguments. It
496converts them to strings of digits, making up the decimal print
497representation of the integer, and then uses the strings instead of the
498original integers. @strong{Don't use this feature; we plan to eliminate
499it. If you already use this feature, change your programs now!} The
500proper way to convert an integer to a decimal number in this way is with
4672ee8f
RS
501@code{format} (@pxref{Formatting Strings}) or @code{number-to-string}
502(@pxref{String Conversion}).
503
504For other concatenation functions, see @code{mapconcat} in @ref{Mapping
505Functions}, @code{concat} in @ref{Creating Strings}, and @code{append}
506in @ref{Building Lists}.
507@end defun
508
509 The @code{append} function provides a way to convert a vector into a
510list with the same elements (@pxref{Building Lists}):
511
512@example
513@group
514(setq avector [1 two (quote (three)) "four" [five]])
515 @result{} [1 two (quote (three)) "four" [five]]
516(append avector nil)
517 @result{} (1 two (quote (three)) "four" [five])
518@end group
519@end example
f9f59935
RS
520
521@node Char-Tables
522@section Char-Tables
523@cindex char-tables
a9f0a989 524@cindex extra slots of char-table
f9f59935
RS
525
526 A char-table is much like a vector, except that it is indexed by
527character codes. Any valid character code, without modifiers, can be
969fe9b5 528used as an index in a char-table. You can access a char-table's
a9f0a989
RS
529elements with @code{aref} and @code{aset}, as with any array. In
530addition, a char-table can have @dfn{extra slots} to hold additional
531data not associated with particular character codes. Char-tables are
532constants when evaluated.
f9f59935 533
f9f59935 534@cindex subtype of char-table
a9f0a989
RS
535 Each char-table has a @dfn{subtype} which is a symbol. The subtype
536has two purposes: to distinguish char-tables meant for different uses,
537and to control the number of extra slots. For example, display tables
538are char-tables with @code{display-table} as the subtype, and syntax
539tables are char-tables with @code{syntax-table} as the subtype. A valid
540subtype must have a @code{char-table-extra-slots} property which is an
541integer between 0 and 10. This integer specifies the number of
542@dfn{extra slots} in the char-table.
f9f59935
RS
543
544@cindex parent of char-table
545 A char-table can have a @dfn{parent}. which is another char-table. If
546it does, then whenever the char-table specifies @code{nil} for a
547particular character @var{c}, it inherits the value specified in the
548parent. In other words, @code{(aref @var{char-table} @var{c})} returns
549the value from the parent of @var{char-table} if @var{char-table} itself
550specifies @code{nil}.
551
552@cindex default value of char-table
553 A char-table can also have a @dfn{default value}. If so, then
554@code{(aref @var{char-table} @var{c})} returns the default value
555whenever the char-table does not specify any other non-@code{nil} value.
556
f9f59935
RS
557@defun make-char-table subtype &optional init
558Return a newly created char-table, with subtype @var{subtype}. Each
559element is initialized to @var{init}, which defaults to @code{nil}. You
560cannot alter the subtype of a char-table after the char-table is
561created.
969fe9b5
RS
562
563There is no argument to specify the length of the char-table, because
564all char-tables have room for any valid character code as an index.
f9f59935
RS
565@end defun
566
f9f59935 567@defun char-table-p object
969fe9b5 568This function returns @code{t} if @var{object} is a char-table,
f9f59935
RS
569otherwise @code{nil}.
570@end defun
571
f9f59935
RS
572@defun char-table-subtype char-table
573This function returns the subtype symbol of @var{char-table}.
574@end defun
575
f9f59935
RS
576@defun set-char-table-default char-table new-default
577This function sets the default value of @var{char-table} to
578@var{new-default}.
579
580There is no special function to access the default value of a char-table.
581To do that, use @code{(char-table-range @var{char-table} nil)}.
582@end defun
583
f9f59935
RS
584@defun char-table-parent char-table
585This function returns the parent of @var{char-table}. The parent is
586always either @code{nil} or another char-table.
587@end defun
588
f9f59935
RS
589@defun set-char-table-parent char-table new-parent
590This function sets the parent of @var{char-table} to @var{new-parent}.
591@end defun
592
f9f59935
RS
593@defun char-table-extra-slot char-table n
594This function returns the contents of extra slot @var{n} of
595@var{char-table}. The number of extra slots in a char-table is
596determined by its subtype.
597@end defun
598
f9f59935
RS
599@defun set-char-table-extra-slot char-table n value
600This function stores @var{value} in extra slot @var{n} of
601@var{char-table}.
602@end defun
603
604 A char-table can specify an element value for a single character code;
605it can also specify a value for an entire character set.
606
f9f59935
RS
607@defun char-table-range char-table range
608This returns the value specified in @var{char-table} for a range of
a9f0a989 609characters @var{range}. Here are the possibilities for @var{range}:
f9f59935
RS
610
611@table @asis
612@item @code{nil}
613Refers to the default value.
614
615@item @var{char}
a9f0a989
RS
616Refers to the element for character @var{char}
617(supposing @var{char} is a valid character code).
f9f59935
RS
618
619@item @var{charset}
620Refers to the value specified for the whole character set
621@var{charset} (@pxref{Character Sets}).
a9f0a989
RS
622
623@item @var{generic-char}
624A generic character stands for a character set; specifying the generic
625character as argument is equivalent to specifying the character set
626name. @xref{Splitting Characters}, for a description of generic characters.
f9f59935
RS
627@end table
628@end defun
629
f9f59935 630@defun set-char-table-range char-table range value
1911e6e5 631This function sets the value in @var{char-table} for a range of
a9f0a989 632characters @var{range}. Here are the possibilities for @var{range}:
f9f59935
RS
633
634@table @asis
635@item @code{nil}
636Refers to the default value.
637
638@item @code{t}
639Refers to the whole range of character codes.
640
641@item @var{char}
a9f0a989
RS
642Refers to the element for character @var{char}
643(supposing @var{char} is a valid character code).
f9f59935
RS
644
645@item @var{charset}
646Refers to the value specified for the whole character set
647@var{charset} (@pxref{Character Sets}).
a9f0a989
RS
648
649@item @var{generic-char}
650A generic character stands for a character set; specifying the generic
651character as argument is equivalent to specifying the character set
652name. @xref{Splitting Characters}, for a description of generic characters.
f9f59935
RS
653@end table
654@end defun
655
f9f59935
RS
656@defun map-char-table function char-table
657This function calls @var{function} for each element of @var{char-table}.
658@var{function} is called with two arguments, a key and a value. The key
a9f0a989
RS
659is a possible @var{range} argument for @code{char-table-range}---either
660a valid character or a generic character---and the value is
661@code{(char-table-range @var{char-table} @var{key})}.
f9f59935 662
969fe9b5 663Overall, the key-value pairs passed to @var{function} describe all the
f9f59935 664values stored in @var{char-table}.
a9f0a989
RS
665
666The return value is always @code{nil}; to make this function useful,
667@var{function} should have side effects. For example,
668here is how to examine each element of the syntax table:
669
670@example
1911e6e5
RS
671(let (accumulator)
672 (map-char-table
673 #'(lambda (key value)
674 (setq accumulator
675 (cons (list key value) accumulator)))
676 (syntax-table))
677 accumulator)
a9f0a989
RS
678@result{}
679((475008 nil) (474880 nil) (474752 nil) (474624 nil)
680 ... (5 (3)) (4 (3)) (3 (3)) (2 (3)) (1 (3)) (0 (3)))
681@end example
f9f59935
RS
682@end defun
683
684@node Bool-Vectors
685@section Bool-vectors
686@cindex Bool-vectors
687
688 A bool-vector is much like a vector, except that it stores only the
689values @code{t} and @code{nil}. If you try to store any non-@code{nil}
969fe9b5
RS
690value into an element of the bool-vector, the effect is to store
691@code{t} there. As with all arrays, bool-vector indices start from 0,
692and the length cannot be changed once the bool-vector is created.
693Bool-vectors are constants when evaluated.
f9f59935
RS
694
695 There are two special functions for working with bool-vectors; aside
696from that, you manipulate them with same functions used for other kinds
697of arrays.
698
f9f59935
RS
699@defun make-bool-vector length initial
700Return a new book-vector of @var{length} elements,
701each one initialized to @var{initial}.
702@end defun
703
704@defun bool-vector-p object
705This returns @code{t} if @var{object} is a bool-vector,
706and @code{nil} otherwise.
707@end defun
708