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