comparison lispref/sequences.texi @ 21007:66d807bdc5b4

*** empty log message ***
author Richard M. Stallman <rms@gnu.org>
date Sat, 28 Feb 1998 01:53:53 +0000
parents a6eb5f12b0f3
children 90da2489c498
comparison
equal deleted inserted replaced
21006:00022857f529 21007:66d807bdc5b4
1 @c -*-texinfo-*- 1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual. 2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. 3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions. 4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../info/sequences 5 @setfilename ../info/sequences
6 @node Sequences Arrays Vectors, Symbols, Lists, Top 6 @node Sequences Arrays Vectors, Symbols, Lists, Top
7 @chapter Sequences, Arrays, and Vectors 7 @chapter Sequences, Arrays, and Vectors
8 @cindex sequence 8 @cindex sequence
50 * Sequence Functions:: Functions that accept any kind of sequence. 50 * Sequence Functions:: Functions that accept any kind of sequence.
51 * Arrays:: Characteristics of arrays in Emacs Lisp. 51 * Arrays:: Characteristics of arrays in Emacs Lisp.
52 * Array Functions:: Functions specifically for arrays. 52 * Array Functions:: Functions specifically for arrays.
53 * Vectors:: Special characteristics of Emacs Lisp vectors. 53 * Vectors:: Special characteristics of Emacs Lisp vectors.
54 * Vector Functions:: Functions specifically for vectors. 54 * Vector Functions:: Functions specifically for vectors.
55 * Char-Tables:: How to work with char-tables.
56 * Bool-Vectors:: How to work with bool-vectors.
55 @end menu 57 @end menu
56 58
57 @node Sequence Functions 59 @node Sequence Functions
58 @section Sequences 60 @section Sequences
59 61
138 @cindex sequence length 140 @cindex sequence length
139 Returns the number of elements in @var{sequence}. If @var{sequence} is 141 Returns the number of elements in @var{sequence}. If @var{sequence} is
140 a cons cell that is not a list (because the final @sc{cdr} is not 142 a cons cell that is not a list (because the final @sc{cdr} is not
141 @code{nil}), a @code{wrong-type-argument} error is signaled. 143 @code{nil}), a @code{wrong-type-argument} error is signaled.
142 144
145 @xref{List Elements}, for the related function @code{safe-list}.
146
143 @example 147 @example
144 @group 148 @group
145 (length '(1 2 3)) 149 (length '(1 2 3))
146 @result{} 3 150 @result{} 3
147 @end group 151 @end group
239 @item 243 @item
240 They occupy one-fourth the space of a vector of the same elements. 244 They occupy one-fourth the space of a vector of the same elements.
241 245
242 @item 246 @item
243 Strings are printed in a way that shows the contents more clearly 247 Strings are printed in a way that shows the contents more clearly
244 as characters. 248 as text.
245 249
246 @item 250 @item
247 Strings can hold text properties. @xref{Text Properties}. 251 Strings can hold text properties. @xref{Text Properties}.
248 252
249 @item 253 @item
258 Sequence Input}. 262 Sequence Input}.
259 263
260 @node Array Functions 264 @node Array Functions
261 @section Functions that Operate on Arrays 265 @section Functions that Operate on Arrays
262 266
263 In this section, we describe the functions that accept both strings 267 In this section, we describe the functions that accept all types of
264 and vectors. 268 arrays.
265 269
266 @defun arrayp object 270 @defun arrayp object
267 This function returns @code{t} if @var{object} is an array (i.e., either a 271 This function returns @code{t} if @var{object} is an array (i.e., a
268 vector or a string). 272 vector, a string, a bool-vector or a char-table).
269 273
270 @example 274 @example
271 @group 275 @group
272 (arrayp [a]) 276 (arrayp [a])
273 @result{} t 277 @result{} t
324 @result{} "asdZasfd" 328 @result{} "asdZasfd"
325 @end group 329 @end group
326 @end example 330 @end example
327 331
328 If @var{array} is a string and @var{object} is not a character, a 332 If @var{array} is a string and @var{object} is not a character, a
329 @code{wrong-type-argument} error results. 333 @code{wrong-type-argument} error results. If @var{array} is a string
334 and @var{object} is character, but @var{object} does not use the same
335 number of bytes as the character currently stored in @code{(aref
336 @var{object} @var{index})}, that is also an error. @xref{Chars and
337 Bytes}.
330 @end defun 338 @end defun
331 339
332 @defun fillarray array object 340 @defun fillarray array object
333 This function fills the array @var{array} with @var{object}, so that 341 This function fills the array @var{array} with @var{object}, so that
334 each element of @var{array} is @var{object}. It returns @var{array}. 342 each element of @var{array} is @var{object}. It returns @var{array}.
361 @section Vectors 369 @section Vectors
362 @cindex vector 370 @cindex vector
363 371
364 Arrays in Lisp, like arrays in most languages, are blocks of memory 372 Arrays in Lisp, like arrays in most languages, are blocks of memory
365 whose elements can be accessed in constant time. A @dfn{vector} is a 373 whose elements can be accessed in constant time. A @dfn{vector} is a
366 general-purpose array; its elements can be any Lisp objects. (The other 374 general-purpose array; its elements can be any Lisp objects. (By
367 kind of array in Emacs Lisp is the @dfn{string}, whose elements must be 375 contrast, a string can hold only characters as elements.) Vectors in
368 characters.) Vectors in Emacs serve as syntax tables (vectors of 376 Emacs are used for obarrays (vectors of symbols), and as part of keymaps
369 integers), as obarrays (vectors of symbols), and in keymaps (vectors of 377 (vectors of commands). They are also used internally as part of the
370 commands). They are also used internally as part of the representation 378 representation of a byte-compiled function; if you print such a
371 of a byte-compiled function; if you print such a function, you will see 379 function, you will see a vector in it.
372 a vector in it.
373 380
374 In Emacs Lisp, the indices of the elements of a vector start from zero 381 In Emacs Lisp, the indices of the elements of a vector start from zero
375 and count up from there. 382 and count up from there.
376 383
377 Vectors are printed with square brackets surrounding the elements. 384 Vectors are printed with square brackets surrounding the elements.
382 A vector, like a string or a number, is considered a constant for 389 A vector, like a string or a number, is considered a constant for
383 evaluation: the result of evaluating it is the same vector. This does 390 evaluation: the result of evaluating it is the same vector. This does
384 not evaluate or even examine the elements of the vector. 391 not evaluate or even examine the elements of the vector.
385 @xref{Self-Evaluating Forms}. 392 @xref{Self-Evaluating Forms}.
386 393
387 Here are examples of these principles: 394 Here are examples illustrating these principles:
388 395
389 @example 396 @example
390 @group 397 @group
391 (setq avector [1 two '(three) "four" [five]]) 398 (setq avector [1 two '(three) "four" [five]])
392 @result{} [1 two (quote (three)) "four" [five]] 399 @result{} [1 two (quote (three)) "four" [five]]
442 @end defun 449 @end defun
443 450
444 @defun vconcat &rest sequences 451 @defun vconcat &rest sequences
445 @cindex copying vectors 452 @cindex copying vectors
446 This function returns a new vector containing all the elements of the 453 This function returns a new vector containing all the elements of the
447 @var{sequences}. The arguments @var{sequences} may be lists, vectors, 454 @var{sequences}. The arguments @var{sequences} may be any kind of
448 or strings. If no @var{sequences} are given, an empty vector is 455 arrays, including lists, vectors, or strings. If no @var{sequences} are
449 returned. 456 given, an empty vector is returned.
450 457
451 The value is a newly constructed vector that is not @code{eq} to any 458 The value is a newly constructed vector that is not @code{eq} to any
452 existing vector. 459 existing vector.
453 460
454 @example 461 @example
489 @result{} [1 two (quote (three)) "four" [five]] 496 @result{} [1 two (quote (three)) "four" [five]]
490 (append avector nil) 497 (append avector nil)
491 @result{} (1 two (quote (three)) "four" [five]) 498 @result{} (1 two (quote (three)) "four" [five])
492 @end group 499 @end group
493 @end example 500 @end example
501
502 @node Char-Tables
503 @section Char-Tables
504 @cindex char-tables
505
506 A char-table is much like a vector, except that it is indexed by
507 character codes. Any valid character code, without modifiers, can be
508 used as an index in a char-table. You can access a char-table with
509 @code{aref} and @code{aset}, just like a vector.
510
511 @cindex extra slots of char-table
512 @cindex subtype of char-table
513 Each char-table has a @dfn{subtype} which is a symbol. In order to be
514 a valid subtype, a symbol must have a @code{char-table-extra-slots}
515 property which is an integer between 0 and 10. This integer specifies
516 the number of @dfn{extra slots} in the char-table.
517
518 @cindex parent of char-table
519 A char-table can have a @dfn{parent}. which is another char-table. If
520 it does, then whenever the char-table specifies @code{nil} for a
521 particular character @var{c}, it inherits the value specified in the
522 parent. In other words, @code{(aref @var{char-table} @var{c})} returns
523 the value from the parent of @var{char-table} if @var{char-table} itself
524 specifies @code{nil}.
525
526 @cindex default value of char-table
527 A char-table can also have a @dfn{default value}. If so, then
528 @code{(aref @var{char-table} @var{c})} returns the default value
529 whenever the char-table does not specify any other non-@code{nil} value.
530
531 @tindex make-char-table
532 @defun make-char-table subtype &optional init
533 Return a newly created char-table, with subtype @var{subtype}. Each
534 element is initialized to @var{init}, which defaults to @code{nil}. You
535 cannot alter the subtype of a char-table after the char-table is
536 created.
537 @end defun
538
539 @tindex char-table-p
540 @defun char-table-p object
541 This function returns @code{t} if @code{object} is a char-table,
542 otherwise @code{nil}.
543 @end defun
544
545 @tindex char-table-subtype
546 @defun char-table-subtype char-table
547 This function returns the subtype symbol of @var{char-table}.
548 @end defun
549
550 @tindex set-char-table-default
551 @defun set-char-table-default char-table new-default
552 This function sets the default value of @var{char-table} to
553 @var{new-default}.
554
555 There is no special function to access the default value of a char-table.
556 To do that, use @code{(char-table-range @var{char-table} nil)}.
557 @end defun
558
559 @tindex char-table-parent
560 @defun char-table-parent char-table
561 This function returns the parent of @var{char-table}. The parent is
562 always either @code{nil} or another char-table.
563 @end defun
564
565 @tindex set-char-table-parent
566 @defun set-char-table-parent char-table new-parent
567 This function sets the parent of @var{char-table} to @var{new-parent}.
568 @end defun
569
570 @tindex char-table-extra-slot
571 @defun char-table-extra-slot char-table n
572 This function returns the contents of extra slot @var{n} of
573 @var{char-table}. The number of extra slots in a char-table is
574 determined by its subtype.
575 @end defun
576
577 @tindex set-char-table-extra-slot
578 @defun set-char-table-extra-slot char-table n value
579 This function stores @var{value} in extra slot @var{n} of
580 @var{char-table}.
581 @end defun
582
583 A char-table can specify an element value for a single character code;
584 it can also specify a value for an entire character set.
585
586 @tindex char-table-range
587 @defun char-table-range char-table range
588 This returns the value specified in @var{char-table} for a range of
589 characters @var{range}. Here @var{range} may be
590
591 @table @asis
592 @item @code{nil}
593 Refers to the default value.
594
595 @item @var{char}
596 Refers to the element for character @var{char}.
597
598 @item @var{charset}
599 Refers to the value specified for the whole character set
600 @var{charset} (@pxref{Character Sets}).
601 @end table
602 @end defun
603
604 @tindex set-char-table-range
605 @defun set-char-table-range char-table range value
606 This function set the value in @var{char-table} for a range of
607 characters @var{range}. Here @var{range} may be
608
609 @table @asis
610 @item @code{nil}
611 Refers to the default value.
612
613 @item @code{t}
614 Refers to the whole range of character codes.
615
616 @item @var{char}
617 Refers to the element for character @var{char}.
618
619 @item @var{charset}
620 Refers to the value specified for the whole character set
621 @var{charset} (@pxref{Character Sets}).
622 @end table
623 @end defun
624
625 @tindex map-char-table
626 @defun map-char-table function char-table
627 This function calls @var{function} for each element of @var{char-table}.
628 @var{function} is called with two arguments, a key and a value. The key
629 is a possible @var{range} argument for @code{char-table-range}, and the
630 value is @code{(char-table-range @var{char-table} @var{key})}. Invalid
631 character codes are never used as the key.
632
633 Overall, the keys-value pairs passed to @var{function} describe all the
634 values stored in @var{char-table}.
635 @end defun
636
637 @node Bool-Vectors
638 @section Bool-vectors
639 @cindex Bool-vectors
640
641 A bool-vector is much like a vector, except that it stores only the
642 values @code{t} and @code{nil}. If you try to store any non-@code{nil}
643 value into an element of the bool-vector, that actually stores @code{t}
644 there.
645
646 There are two special functions for working with bool-vectors; aside
647 from that, you manipulate them with same functions used for other kinds
648 of arrays.
649
650 @tindex make-bool-vector
651 @defun make-bool-vector length initial
652 Return a new book-vector of @var{length} elements,
653 each one initialized to @var{initial}.
654 @end defun
655
656 @defun bool-vector-p object
657 This returns @code{t} if @var{object} is a bool-vector,
658 and @code{nil} otherwise.
659 @end defun
660