Emacs Lisp: List vs Vector

By Xah Lee. Date: . Last updated: .

Here's a chart of elisp's list-like datatypes.

     _____________________________________________
    |                                             |
    |          Sequence (not actual)              |
    |  ______   ________________________________  |
    | |      | |                                | |
    | | List | |        Array (not actual)      | |
    | |      | |    ________       ________     | |
    | |______| |   |        |     |        |    | |
    |          |   | Vector |     | String |    | |
    |          |   |________|     |________|    | |
    |          |  ____________   _____________  | |
    |          | |            | |             | | |
    |          | | Char-table | | Bool-vector | | |
    |          | |____________| |_____________| | |
    |          |________________________________| |
    |_____________________________________________|

Note: Sequence is not a actual datatype in elisp. Array is not a actual datatype in elisp. They are just grouping names.

Lisp's list and vector are considered to be both “sequences”. Many functions, such as {mapcar, length, elt} , can work with list or vector.

Here's their primary differences:

  1. Vector: Access time to any element is constant.
  2. Vector's length cannot change. (if you create a new copy of a vector, the time required is proportional to the vector's length)
  1. List: Access time to a element is proportional to the distance of the element's position in the list.
  2. List's length can grow by prepending with cons, and can have the first element dropped by cdr. These operations have constant time.

〔►see Emacs Lisp: Vector

〔►see Emacs Lisp: List

You can nest list/vector in any way. Example:

;; mixed nested list/vector

[ '(3 4) '(5 8) [4 2]]

(list [8 7] '(4 1))

(info "(elisp) Sequences Arrays Vectors")

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs