(lispref.info)Vectors


Prev: Array Functions Up: Sequences Arrays Vectors

Vectors
=======

   Arrays in Lisp, like arrays in most languages, are blocks of memory
whose elements can be accessed in constant time.  A "vector" is a
general-purpose array; its elements can be any Lisp objects.  (The other
kind of array provided in Emacs Lisp is the "string", whose elements
must be characters.)  The main uses of vectors in Emacs are as syntax
tables (vectors of integers) and keymaps (vectors of commands).  They
are also used internally as part of the representation of a
byte-compiled function; if you print such a function, you will see a
vector in it.

   The indices of the elements of a vector are numbered starting with
zero in Emacs Lisp.

   Vectors are printed with square brackets surrounding the elements in
their order.  Thus, a vector containing the symbols `a', `b' and `c' is
printed as `[a b c]'.  You can write vectors in the same way in Lisp
input.

   A vector, like a string or a number, is considered a constant for
evaluation: the result of evaluating it is the same vector.  The
elements of the vector are not evaluated.  Note: Self-Evaluating
Forms.

   Here are examples of these principles:

     (setq avector [1 two '(three) "four" [five]])
          => [1 two (quote (three)) "four" [five]]
     (eval avector)
          => [1 two (quote (three)) "four" [five]]
     (eq avector (eval avector))
          => t

   Here are some functions that relate to vectors:

 - Function: vectorp OBJECT
     This function returns `t' if OBJECT is a vector.

          (vectorp [a])
               => t
          (vectorp "asdf")
               => nil

 - Function: vector &rest OBJECTS
     This function creates and returns a vector whose elements are the
     arguments, OBJECTS.

          (vector 'foo 23 [bar baz] "rats")
               => [foo 23 [bar baz] "rats"]
          (vector)
               => []

 - Function: make-vector INTEGER OBJECT
     This function returns a new vector consisting of INTEGER elements,
     each initialized to OBJECT.

          (setq sleepy (make-vector 9 'Z))
               => [Z Z Z Z Z Z Z Z Z]

 - Function: vconcat &rest SEQUENCES
     This function returns a new vector containing all the elements of
     the SEQUENCES.  The arguments SEQUENCES may be lists, vectors, or
     strings.  If no SEQUENCES are given, an empty vector is returned.

     The value is a newly constructed vector that is not `eq' to any
     existing vector.

          (setq a (vconcat '(A B C) '(D E F)))
               => [A B C D E F]
          (eq a (vconcat a))
               => nil
          (vconcat)
               => []
          (vconcat [A B C] "aa" '(foo (6 7)))
               => [A B C 97 97 foo (6 7)]

     When an argument is an integer (not a sequence of integers), it is
     converted to a string of digits making up the decimal printed
     representation of the integer.  This special case exists for
     compatibility with Mocklisp, and we don't recommend you take
     advantage of it.  If you want to convert an integer in this way,
     use `format' (Note: Formatting Strings.) or `int-to-string'
     (Note: String Conversion.).

     For other concatenation functions, see `mapconcat' in *Note
     Mapping Functions::, `concat' in Note: Creating Strings, and
     `append' in Note: Building Lists.

   The `append' function may be used to convert a vector into a list
with the same elements (Note: Building Lists.):

     (setq avector [1 two (quote (three)) "four" [five]])
          => [1 two (quote (three)) "four" [five]]
     (append avector nil)
          => (1 two (quote (three)) "four" [five])


automatically generated by info2www