home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / elisp / elisp-5 < prev    next >
Encoding:
GNU Info File  |  1993-05-31  |  48.2 KB  |  1,320 lines

  1. This is Info file elisp, produced by Makeinfo-1.55 from the input file
  2. elisp.texi.
  3.  
  4.    This is edition 2.0 of the GNU Emacs Lisp Reference Manual, for
  5. Emacs Version 19.
  6.  
  7.    Published by the Free Software Foundation, 675 Massachusetts Avenue,
  8. Cambridge, MA 02139 USA
  9.  
  10.    Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  11.  
  12.    Permission is granted to make and distribute verbatim copies of this
  13. manual provided the copyright notice and this permission notice are
  14. preserved on all copies.
  15.  
  16.    Permission is granted to copy and distribute modified versions of
  17. this manual under the conditions for verbatim copying, provided that
  18. the entire resulting derived work is distributed under the terms of a
  19. permission notice identical to this one.
  20.  
  21.    Permission is granted to copy and distribute translations of this
  22. manual into another language, under the above conditions for modified
  23. versions, except that this permission notice may be stated in a
  24. translation approved by the Foundation.
  25.  
  26. 
  27. File: elisp,  Node: Case Table,  Prev: Character Case,  Up: Strings and Characters
  28.  
  29. The Case Table
  30. ==============
  31.  
  32.    You can customize case conversion by installing a special "case
  33. table".  A case table specifies the mapping between upper case and lower
  34. case letters.  It affects both the string and character case conversion
  35. functions (see the previous section) and those that apply to text in the
  36. buffer (*note Case Changes::.).  Use case table if you are using a
  37. language which has letters that are not the standard ASCII letters.
  38.  
  39.    A case table is a list of this form:
  40.  
  41.      (DOWNCASE UPCASE CANONICALIZE EQUIVALENCES)
  42.  
  43. where each element is either `nil' or a string of length 256.  The
  44. element DOWNCASE says how to map each character to its lower-case
  45. equivalent.  The element UPCASE maps each character to its upper-case
  46. equivalent.  If lower and upper case characters are in one-to-one
  47. correspondence, use `nil' for UPCASE; then Emacs deduces the upcase
  48. table from DOWNCASE.
  49.  
  50.    For some languages, upper and lower case letters are not in
  51. one-to-one correspondence.  There may be two different lower case
  52. letters with the same upper case equivalent.  In these cases, you need
  53. to specify the maps for both directions.
  54.  
  55.    The element CANONICALIZE maps each character to a canonical
  56. equivalent; any two characters that are related by case-conversion have
  57. the same canonical equivalent character.
  58.  
  59.    The element EQUIVALENCES is a map that cyclicly permutes each
  60. equivalence class (of characters with the same canonical equivalent).
  61. (For ordinary ASCII, this would map `a' into `A' and `A' into `a', and
  62. likewise for each set of equivalent characters.)
  63.  
  64.    You can provide `nil' for both CANONICALIZE and EQUIVALENCES, in
  65. which case both are deduced from DOWNCASE and UPCASE.  Normally, that's
  66. what you should do, when you construct a case table.  But when you look
  67. at the case table that's in use, you will find non-`nil' values for
  68. those components.
  69.  
  70.    Each buffer has a case table.  Emacs also has a "standard case
  71. table" which is copied into each buffer when you create the buffer.
  72. (Changing the standard case table doesn't affect any existing buffers.)
  73.  
  74.    Here are the functions for working with case tables:
  75.  
  76.  - Function: case-table-p OBJECT
  77.      This predicate returns non-`nil' if OBJECT is a valid case table.
  78.  
  79.  - Function: set-standard-case-table TABLE
  80.      This function makes TABLE the standard case table, so that it will
  81.      apply to any buffers created subsequently.
  82.  
  83.  - Function: standard-case-table
  84.      This returns the standard case table.
  85.  
  86.  - Function: current-case-table
  87.      This function returns the current buffer's case table.
  88.  
  89.  - Function: set-case-table TABLE
  90.      This sets the current buffer's case table to TABLE.
  91.  
  92.    The following three functions are convenient subroutines for packages
  93. that define non-ASCII character sets.  They modify a string
  94. DOWNCASE-TABLE provided as an argument; this should be a string to be
  95. used as the DOWNCASE part of a case table.  They also modify two syntax
  96. tables, the standard syntax table and the Text mode syntax table.
  97. (*Note Syntax Tables::.)
  98.  
  99.  - Function: set-case-syntax-pair UC LC DOWNCASE-TABLE
  100.      This function specifies a pair of corresponding letters, one upper
  101.      case and one lower case.
  102.  
  103.  - Function: set-case-syntax-delims L R DOWNCASE-TABLE
  104.      This function makes characters L and R a matching pair of
  105.      case-invariant delimiters.
  106.  
  107.  - Function: set-case-syntax CHAR SYNTAX DOWNCASE-TABLE
  108.      This function makes CHAR case-invariant, with syntax SYNTAX.
  109.  
  110.  - Command: describe-buffer-case-table
  111.      This command displays a description of the contents of the current
  112.      buffer's case table.
  113.  
  114.    You can load the library `iso-syntax' to set up the syntax and case
  115. table for the 256 bit ISO Latin 1 character set.
  116.  
  117. 
  118. File: elisp,  Node: Lists,  Next: Sequences Arrays Vectors,  Prev: Strings and Characters,  Up: Top
  119.  
  120. Lists
  121. *****
  122.  
  123.    A "list" represents a sequence of zero or more elements (which may
  124. be any Lisp objects).  The important difference between lists and
  125. vectors is that two or more lists can share part of their structure; in
  126. addition, you can insert or delete elements in a list without copying
  127. the whole list.
  128.  
  129. * Menu:
  130.  
  131. * Cons Cells::          How lists are made out of cons cells.
  132. * Lists as Boxes::                 Graphical notation to explain lists.
  133. * List-related Predicates::        Is this object a list?  Comparing two lists.
  134. * List Elements::       Extracting the pieces of a list.
  135. * Building Lists::      Creating list structure.
  136. * Modifying Lists::     Storing new pieces into an existing list.
  137. * Sets And Lists::      A list can represent a finite mathematical set.
  138. * Association Lists::   A list can represent a finite relation or mapping.
  139.  
  140. 
  141. File: elisp,  Node: Cons Cells,  Next: Lists as Boxes,  Prev: Lists,  Up: Lists
  142.  
  143. Lists and Cons Cells
  144. ====================
  145.  
  146.    Lists in Lisp are not a primitive data type; they are built up from
  147. "cons cells".  A cons cell is a data object which represents an ordered
  148. pair.  It records two Lisp objects, one labeled as the CAR, and the
  149. other labeled as the CDR.  (These names are traditional.)
  150.  
  151.    A list is made by chaining cons cells together, one cons cell per
  152. element.  By convention, the CARs of the cons cells are the elements of
  153. the list, and the CDRs are used to chain the list: the CDR of each cons
  154. cell is the following cons cell.  The CDR of the last cons cell is
  155. `nil'.  This asymmetry between the CAR and the CDR is entirely a matter
  156. of convention; at the level of cons cells, the CAR and CDR slots have
  157. the same characteristics.
  158.  
  159.    The symbol `nil' is considered a list as well as a symbol; it is the
  160. list with no elements.  For convenience, the symbol `nil' is considered
  161. to have `nil' as its CDR (and also as its CAR).
  162.  
  163.    The CDR of any nonempty list L is a list containing all the elements
  164. of L except the first.
  165.  
  166. 
  167. File: elisp,  Node: Lists as Boxes,  Next: List-related Predicates,  Prev: Cons Cells,  Up: Lists
  168.  
  169. Lists as Linked Pairs of Boxes
  170. ==============================
  171.  
  172.    A cons cell can be illustrated as a pair of boxes.  The first box
  173. represents the CAR and the second box represents the CDR.  Here is an
  174. illustration of the two-element list, `(tulip lily)', made from two
  175. cons cells:
  176.  
  177.       ---------------         ---------------
  178.      | car   | cdr   |       | car   | cdr   |
  179.      | tulip |   o---------->| lily  |  nil  |
  180.      |       |       |       |       |       |
  181.       ---------------         ---------------
  182.  
  183.    Each pair of boxes represents a cons cell.  Each box "refers to",
  184. "points to" or "contains" a Lisp object.  (These terms are synonymous.)
  185. The first box, which is the CAR of the first cons cell, contains the
  186. symbol `tulip'.  The arrow from the CDR of the first cons cell to the
  187. second cons cell indicates that the CDR of the first cons cell points
  188. to the second cons cell.
  189.  
  190.    The same list can be illustrated in a different sort of box notation
  191. like this:
  192.  
  193.          ___ ___      ___ ___
  194.         |___|___|--> |___|___|--> nil
  195.           |            |
  196.           |            |
  197.            --> tulip    --> lily
  198.  
  199.    Here is a more complex illustration, this time of the three-element
  200. list, `((pine needles) oak maple)', the first element of which is a
  201. two-element list:
  202.  
  203.          ___ ___      ___ ___      ___ ___
  204.         |___|___|--> |___|___|--> |___|___|--> nil
  205.           |            |            |
  206.           |            |            |
  207.           |             --> oak      --> maple
  208.           |
  209.           |     ___ ___      ___ ___
  210.            --> |___|___|--> |___|___|--> nil
  211.                  |            |
  212.                  |            |
  213.                   --> pine     --> needles
  214.  
  215.    The same list is represented in the first box notation like this:
  216.  
  217.       --------------       --------------       --------------
  218.      | car   | cdr  |     | car   | cdr  |     | car   | cdr  |
  219.      |   o   |   o------->| oak   |   o------->| maple |  nil |
  220.      |   |   |      |     |       |      |     |       |      |
  221.       -- | ---------       --------------       --------------
  222.          |
  223.          |
  224.          |        --------------       ----------------
  225.          |       | car   | cdr  |     | car     | cdr  |
  226.           ------>| pine  |   o------->| needles |  nil |
  227.                  |       |      |     |         |      |
  228.                   --------------       ----------------
  229.  
  230.    *Note List Type::, for the read and print syntax of lists, and for
  231. more "box and arrow" illustrations of lists.
  232.  
  233. 
  234. File: elisp,  Node: List-related Predicates,  Next: List Elements,  Prev: Lists as Boxes,  Up: Lists
  235.  
  236. Predicates on Lists
  237. ===================
  238.  
  239.    The following predicates test whether a Lisp object is an atom, is a
  240. cons cell or is a list, or whether it is the distinguished object `nil'.
  241. (Many of these tests can be defined in terms of the others, but they are
  242. used so often that it is worth having all of them.)
  243.  
  244.  - Function: consp OBJECT
  245.      This function returns `t' if OBJECT is a cons cell, `nil'
  246.      otherwise.  `nil' is not a cons cell, although it *is* a list.
  247.  
  248.  - Function: atom OBJECT
  249.      This function returns `t' if OBJECT is an atom, `nil' otherwise.
  250.      All objects except cons cells are atoms.  The symbol `nil' is an
  251.      atom and is also a list; it is the only Lisp object which is both.
  252.  
  253.           (atom OBJECT) == (not (consp OBJECT))
  254.  
  255.  - Function: listp OBJECT
  256.      This function returns `t' if OBJECT is a cons cell or `nil'.
  257.      Otherwise, it returns `nil'.
  258.  
  259.           (listp '(1))
  260.                => t
  261.           (listp '())
  262.                => t
  263.  
  264.  - Function: nlistp OBJECT
  265.      This function is the opposite of `listp': it returns `t' if OBJECT
  266.      is not a list.  Otherwise, it returns `nil'.
  267.  
  268.           (listp OBJECT) == (not (nlistp OBJECT))
  269.  
  270.  - Function: null OBJECT
  271.      This function returns `t' if OBJECT is `nil', and returns `nil'
  272.      otherwise.  This function is identical to `not', but as a matter
  273.      of clarity we use `null' when OBJECT is considered a list and
  274.      `not' when it is considered a truth value (see `not' in *Note
  275.      Combining Conditions::).
  276.  
  277.           (null '(1))
  278.                => nil
  279.           (null '())
  280.                => t
  281.  
  282. 
  283. File: elisp,  Node: List Elements,  Next: Building Lists,  Prev: List-related Predicates,  Up: Lists
  284.  
  285. Accessing Elements of Lists
  286. ===========================
  287.  
  288.  - Function: car CONS-CELL
  289.      This function returns the value pointed to by the first pointer of
  290.      the cons cell CONS-CELL.  Expressed another way, this function
  291.      returns the CAR of CONS-CELL.
  292.  
  293.      As a special case, if CONS-CELL is `nil', then `car' is defined to
  294.      return `nil'; therefore, any list is a valid argument for `car'.
  295.      An error is signaled if the argument is not a cons cell or `nil'.
  296.  
  297.           (car '(a b c))
  298.                => a
  299.           (car '())
  300.                => nil
  301.  
  302.  - Function: cdr CONS-CELL
  303.      This function returns the value pointed to by the second pointer of
  304.      the cons cell CONS-CELL.  Expressed another way, this function
  305.      returns the CDR of CONS-CELL.
  306.  
  307.      As a special case, if CONS-CELL is `nil', then `cdr' is defined to
  308.      return `nil'; therefore, any list is a valid argument for `cdr'.
  309.      An error is signaled if the argument is not a cons cell or `nil'.
  310.  
  311.           (cdr '(a b c))
  312.                => (b c)
  313.           (cdr '())
  314.                => nil
  315.  
  316.  - Function: car-safe OBJECT
  317.      This function lets you take the CAR of a cons cell while avoiding
  318.      errors for other data types.  It returns the CAR of OBJECT if
  319.      OBJECT is a cons cell, `nil' otherwise.  This is in contrast to
  320.      `car', which signals an error if OBJECT is not a list.
  321.  
  322.           (car-safe OBJECT)
  323.           ==
  324.           (let ((x OBJECT))
  325.             (if (consp x)
  326.                 (car x)
  327.               nil))
  328.  
  329.  - Function: cdr-safe OBJECT
  330.      This function lets you take the CDR of a cons cell while avoiding
  331.      errors for other data types.  It returns the CDR of OBJECT if
  332.      OBJECT is a cons cell, `nil' otherwise.  This is in contrast to
  333.      `cdr', which signals an error if OBJECT is not a list.
  334.  
  335.           (cdr-safe OBJECT)
  336.           ==
  337.           (let ((x OBJECT))
  338.             (if (consp x)
  339.                 (cdr x)
  340.               nil))
  341.  
  342.  - Function: nth N LIST
  343.      This function returns the Nth element of LIST.  Elements are
  344.      numbered starting with zero, so the CAR of LIST is element number
  345.      zero.  If the length of LIST is N or less, the value is `nil'.
  346.  
  347.      If N is less than zero, then the first element is returned.
  348.  
  349.           (nth 2 '(1 2 3 4))
  350.                => 3
  351.           (nth 10 '(1 2 3 4))
  352.                => nil
  353.           (nth -3 '(1 2 3 4))
  354.                => 1
  355.           
  356.           (nth n x) == (car (nthcdr n x))
  357.  
  358.  - Function: nthcdr N LIST
  359.      This function returns the Nth cdr of LIST.  In other words, it
  360.      removes the first N links of LIST and returns what follows.
  361.  
  362.      If N is less than or equal to zero, then all of LIST is returned.
  363.      If the length of LIST is N or less, the value is `nil'.
  364.  
  365.           (nthcdr 1 '(1 2 3 4))
  366.                => (2 3 4)
  367.           (nthcdr 10 '(1 2 3 4))
  368.                => nil
  369.           (nthcdr -3 '(1 2 3 4))
  370.                => (1 2 3 4)
  371.  
  372. 
  373. File: elisp,  Node: Building Lists,  Next: Modifying Lists,  Prev: List Elements,  Up: Lists
  374.  
  375. Building Cons Cells and Lists
  376. =============================
  377.  
  378.    Many functions build lists, as lists reside at the very heart of
  379. Lisp.  `cons' is the fundamental list-building function; however, it is
  380. interesting to note that `list' is used more times in the source code
  381. for Emacs than `cons'.
  382.  
  383.  - Function: cons OBJECT1 OBJECT2
  384.      This function is the fundamental function used to build new list
  385.      structure.  It creates a new cons cell, making OBJECT1 the CAR,
  386.      and OBJECT2 the CDR.  It then returns the new cons cell.  The
  387.      arguments OBJECT1 and OBJECT2 may be any Lisp objects, but most
  388.      often OBJECT2 is a list.
  389.  
  390.           (cons 1 '(2))
  391.                => (1 2)
  392.           (cons 1 '())
  393.                => (1)
  394.           (cons 1 2)
  395.                => (1 . 2)
  396.  
  397.      `cons' is often used to add a single element to the front of a
  398.      list.  This is called "consing the element onto the list".  For
  399.      example:
  400.  
  401.           (setq list (cons newelt list))
  402.  
  403.      Note that there is no conflict between the variable named `list'
  404.      used in this example and the function named `list' described below;
  405.      any symbol can serve both functions.
  406.  
  407.  - Function: list &rest OBJECTS
  408.      This function creates a list with OBJECTS as its elements.  The
  409.      resulting list is always `nil'-terminated.  If no OBJECTS are
  410.      given, the empty list is returned.
  411.  
  412.           (list 1 2 3 4 5)
  413.                => (1 2 3 4 5)
  414.           (list 1 2 '(3 4 5) 'foo)
  415.                => (1 2 (3 4 5) foo)
  416.           (list)
  417.                => nil
  418.  
  419.  - Function: make-list LENGTH OBJECT
  420.      This function creates a list of length LENGTH, in which all the
  421.      elements have the identical value OBJECT.  Compare `make-list'
  422.      with `make-string' (*note Creating Strings::.).
  423.  
  424.           (make-list 3 'pigs)
  425.                => (pigs pigs pigs)
  426.           (make-list 0 'pigs)
  427.                => nil
  428.  
  429.  - Function: append &rest SEQUENCES
  430.      This function returns a list containing all the elements of
  431.      SEQUENCES.  The SEQUENCES may be lists, vectors, strings, or
  432.      integers.  All arguments except the last one are copied, so none
  433.      of them are altered.
  434.  
  435.      The final argument to `append' may be any object but it is
  436.      typically a list.  The final argument is not copied or converted;
  437.      it becomes part of the structure of the new list.
  438.  
  439.      Here is an example:
  440.  
  441.           (setq trees '(pine oak))
  442.                => (pine oak)
  443.           (setq more-trees (append '(maple birch) trees))
  444.                => (maple birch pine oak)
  445.           
  446.           trees
  447.                => (pine oak)
  448.           more-trees
  449.                => (maple birch pine oak)
  450.           (eq trees (cdr (cdr more-trees)))
  451.                => t
  452.  
  453.      You can see what happens by looking at a box diagram.  The variable
  454.      `trees' is set to the list `(pine oak)' and then the variable
  455.      `more-trees' is set to the list `(maple birch pine oak)'.
  456.      However, the variable `trees' continues to refer to the original
  457.      list:
  458.  
  459.           more-trees                trees
  460.           |                           |
  461.           |     ___ ___      ___ ___   -> ___ ___      ___ ___
  462.            --> |___|___|--> |___|___|--> |___|___|--> |___|___|--> nil
  463.                  |            |            |            |
  464.                  |            |            |            |
  465.                   --> maple    -->birch     --> pine     --> oak
  466.  
  467.      An empty sequence contributes nothing to the value returned by
  468.      `append'.  As a consequence of this, a final `nil' argument forces
  469.      a copy of the previous argument.
  470.  
  471.           trees
  472.                => (pine oak)
  473.           (setq wood (append trees ()))
  474.                => (pine oak)
  475.           wood
  476.                => (pine oak)
  477.           (eq wood trees)
  478.                => nil
  479.  
  480.      This once was the standard way to copy a list, before the function
  481.      `copy-sequence' was invented.  *Note Sequences Arrays Vectors::.
  482.  
  483.      With the help of `apply', we can append all the lists in a list of
  484.      lists:
  485.  
  486.           (apply 'append '((a b c) nil (x y z) nil))
  487.                => (a b c x y z)
  488.  
  489.      If no SEQUENCES are given, `nil' is returned:
  490.  
  491.           (append)
  492.                => nil
  493.  
  494.      In the special case where one of the SEQUENCES is an integer (not
  495.      a sequence of integers), it is first converted to a string of
  496.      digits making up the decimal print representation of the integer.
  497.      This special case exists for compatibility with Mocklisp, and we
  498.      don't recommend you take advantage of it.  If you want to convert
  499.      an integer in this way, use `format' (*note Formatting Strings::.)
  500.      or `number-to-string' (*note String Conversion::.).
  501.  
  502.           (setq trees '(pine oak))
  503.                => (pine oak)
  504.           (char-to-string 54)
  505.                => "6"
  506.           (setq longer-list (append trees 6 '(spruce)))
  507.                => (pine oak 54 spruce)
  508.           (setq x-list (append trees 6 6))
  509.                => (pine oak 54 . 6)
  510.  
  511.      See `nconc' in *Note Rearrangement::, for another way to join lists
  512.      without copying.
  513.  
  514.  - Function: reverse LIST
  515.      This function creates a new list whose elements are the elements of
  516.      LIST, but in reverse order.  The original argument LIST is *not*
  517.      altered.
  518.  
  519.           (setq x '(1 2 3 4))
  520.                => (1 2 3 4)
  521.           (reverse x)
  522.                => (4 3 2 1)
  523.           x
  524.                => (1 2 3 4)
  525.  
  526. 
  527. File: elisp,  Node: Modifying Lists,  Next: Sets And Lists,  Prev: Building Lists,  Up: Lists
  528.  
  529. Modifying Existing List Structure
  530. =================================
  531.  
  532.    You can modify the CAR and CDR contents of a cons cell with the
  533. primitives `setcar' and `setcdr'.
  534.  
  535.      Common Lisp note: Common Lisp uses functions `rplaca' and `rplacd'
  536.      to alter list structure; they change structure the same way as
  537.      `setcar' and `setcdr', but the Common Lisp functions return the
  538.      cons cell while `setcar' and `setcdr' return the new CAR or CDR.
  539.  
  540. * Menu:
  541.  
  542. * Setcar::          Replacing an element in a list.
  543. * Setcdr::          Replacing part of the list backbone.
  544.                       This can be used to remove or add elements.
  545. * Rearrangement::   Reordering the elements in a list; combining lists.
  546.  
  547. 
  548. File: elisp,  Node: Setcar,  Next: Setcdr,  Prev: Modifying Lists,  Up: Modifying Lists
  549.  
  550. Altering List Elements with `setcar'
  551. ------------------------------------
  552.  
  553.    Changing the CAR of a cons cell is done with `setcar' and replaces
  554. one element of a list with a different element.
  555.  
  556.  - Function: setcar CONS OBJECT
  557.      This function stores OBJECT as the new CAR of CONS, replacing its
  558.      previous CAR.  It returns the value OBJECT.  For example:
  559.  
  560.           (setq x '(1 2))
  561.                => (1 2)
  562.           (setcar x '4)
  563.                => 4
  564.           x
  565.                => (4 2)
  566.  
  567.    When a cons cell is part of the shared structure of several lists,
  568. storing a new CAR into the cons changes one element of each of these
  569. lists.  Here is an example:
  570.  
  571.      ;; Create two lists that are partly shared.
  572.      (setq x1 '(a b c))
  573.           => (a b c)
  574.      (setq x2 (cons 'z (cdr x1)))
  575.           => (z b c)
  576.      
  577.      ;; Replace the CAR of a shared link.
  578.      (setcar (cdr x1) 'foo)
  579.           => foo
  580.      x1                           ; Both lists are changed.
  581.           => (a foo c)
  582.      x2
  583.           => (z foo c)
  584.      
  585.      ;; Replace the CAR of a link that is not shared.
  586.      (setcar x1 'baz)
  587.           => baz
  588.      x1                           ; Only one list is changed.
  589.           => (baz foo c)
  590.      x2
  591.           => (z foo c)
  592.  
  593.    Here is a graphical depiction of the shared structure of the two
  594. lists X1 and X2, showing why replacing `b' changes them both:
  595.  
  596.              ___ ___        ___ ___      ___ ___
  597.      x1---> |___|___|----> |___|___|--> |___|___|--> nil
  598.               |        -->   |            |
  599.               |       |      |            |
  600.                --> a  |       --> b        --> c
  601.                       |
  602.             ___ ___   |
  603.      x2--> |___|___|--
  604.              |
  605.              |
  606.               --> z
  607.  
  608.    Here is an alternative form of box diagram, showing the same
  609. relationship:
  610.  
  611.      x1:
  612.       --------------       --------------       --------------
  613.      | car   | cdr  |     | car   | cdr  |     | car   | cdr  |
  614.      |   a   |   o------->|   b   |   o------->|   c   |  nil |
  615.      |       |      |  -->|       |      |     |       |      |
  616.       --------------  |    --------------       --------------
  617.                       |
  618.      x2:              |
  619.       --------------  |
  620.      | car   | cdr  | |
  621.      |   z   |   o----
  622.      |       |      |
  623.       --------------
  624.  
  625. 
  626. File: elisp,  Node: Setcdr,  Next: Rearrangement,  Prev: Setcar,  Up: Modifying Lists
  627.  
  628. Altering the CDR of a List
  629. --------------------------
  630.  
  631.    The lowest-level primitive for modifying a CDR is `setcdr':
  632.  
  633.  - Function: setcdr CONS OBJECT
  634.      This function stores OBJECT into the cdr of CONS.  The value
  635.      returned is OBJECT, not CONS.
  636.  
  637.    Here is an example of replacing the CDR of a list with a different
  638. list.  All but the first element of the list are removed in favor of a
  639. different sequence of elements.  The first element is unchanged,
  640. because it resides in the CAR of the list, and is not reached via the
  641. CDR.
  642.  
  643.      (setq x '(1 2 3))
  644.           => (1 2 3)
  645.      (setcdr x '(4))
  646.           => (4)
  647.      x
  648.           => (1 4)
  649.  
  650.    You can delete elements from the middle of a list by altering the
  651. CDRs of the cons cells in the list.  For example, here we delete the
  652. second element, `b', from the list `(a b c)', by changing the CDR of
  653. the first cell:
  654.  
  655.      (setq x1 '(a b c))
  656.           => (a b c)
  657.      (setcdr x1 (cdr (cdr x1)))
  658.           => (c)
  659.      x1
  660.           => (a c)
  661.  
  662.    Here is the result in box notation:
  663.  
  664.                         --------------------
  665.                        |                    |
  666.       --------------   |   --------------   |    --------------
  667.      | car   | cdr  |  |  | car   | cdr  |   -->| car   | cdr  |
  668.      |   a   |   o-----   |   b   |   o-------->|   c   |  nil |
  669.      |       |      |     |       |      |      |       |      |
  670.       --------------       --------------        --------------
  671.  
  672. The second cons cell, which previously held the element `b', still
  673. exists and its CAR is still `b', but it no longer forms part of this
  674. list.
  675.  
  676.    It is equally easy to insert a new element by changing CDRs:
  677.  
  678.      (setq x1 '(a b c))
  679.           => (a b c)
  680.      (setcdr x1 (cons 'd (cdr x1)))
  681.           => (d b c)
  682.      x1
  683.           => (a d b c)
  684.  
  685.    Here is this result in box notation:
  686.  
  687.      --------------        -------------       -------------
  688.      | car  | cdr   |      | car  | cdr  |     | car  | cdr  |
  689.      |   a  |   o   |   -->|   b  |   o------->|   c  |  nil |
  690.      |      |   |   |  |   |      |      |     |      |      |
  691.       --------- | --   |    -------------       -------------
  692.                 |      |
  693.           -----         --------
  694.          |                      |
  695.          |    ---------------   |
  696.          |   | car   | cdr   |  |
  697.           -->|   d   |   o------
  698.              |       |       |
  699.               ---------------
  700.  
  701. 
  702. File: elisp,  Node: Rearrangement,  Prev: Setcdr,  Up: Modifying Lists
  703.  
  704. Functions that Rearrange Lists
  705. ------------------------------
  706.  
  707.    Here are some functions that rearrange lists "destructively" by
  708. modifying the CDRs of their component cons cells.  We call these
  709. functions "destructive" because the original lists passed as arguments
  710. to them are chewed up to produce a new list that is subsequently
  711. returned.
  712.  
  713.  - Function: nconc &rest LISTS
  714.      This function returns a list containing all the elements of LISTS.
  715.      Unlike `append' (*note Building Lists::.), the LISTS are *not*
  716.      copied.  Instead, the last CDR of each of the LISTS is changed to
  717.      refer to the following list.  The last of the LISTS is not
  718.      altered.  For example:
  719.  
  720.           (setq x '(1 2 3))
  721.                => (1 2 3)
  722.           (nconc x '(4 5))
  723.                => (1 2 3 4 5)
  724.           x
  725.                => (1 2 3 4 5)
  726.  
  727.      Since the last argument of `nconc' is not itself modified, it is
  728.      reasonable to use a constant list, such as `'(4 5)', as is done in
  729.      the above example.  For the same reason, the last argument need
  730.      not be a list:
  731.  
  732.           (setq x '(1 2 3))
  733.                => (1 2 3)
  734.           (nconc x 'z)
  735.                => (1 2 3 . z)
  736.           x
  737.                => (1 2 3 . z)
  738.  
  739.      A common pitfall is to use a quoted constant list as a non-last
  740.      argument to `nconc'.  If you do this, your program will change
  741.      each time you run it!  Here is what happens:
  742.  
  743.           (defun add-foo (x)            ; This function should add
  744.             (nconc '(foo) x))           ;   `foo' to the front of its arg.
  745.  
  746.           (symbol-function 'add-foo)
  747.                => (lambda (x) (nconc (quote (foo)) x))
  748.  
  749.           (setq xx (add-foo '(1 2)))    ; It seems to work.
  750.                => (foo 1 2)
  751.  
  752.           (setq xy (add-foo '(3 4)))    ; What happened?
  753.                => (foo 1 2 3 4)
  754.  
  755.           (eq xx xy)
  756.                => t
  757.  
  758.           (symbol-function 'add-foo)
  759.                => (lambda (x) (nconc (quote (foo 1 2 3 4) x)))
  760.  
  761.  - Function: nreverse LIST
  762.      This function reverses the order of the elements of LIST.  Unlike
  763.      `reverse', `nreverse' alters its argument destructively by
  764.      reversing the CDRs in the cons cells forming the list.  The cons
  765.      cell which used to be the last one in LIST becomes the first cell
  766.      of the value.
  767.  
  768.      For example:
  769.  
  770.           (setq x '(1 2 3 4))
  771.                => (1 2 3 4)
  772.           x
  773.                => (1 2 3 4)
  774.           (nreverse x)
  775.                => (4 3 2 1)
  776.           ;; The cell that was first is now last.
  777.           x
  778.                => (1)
  779.  
  780.      To avoid confusion, we usually store the result of `nreverse' back
  781.      in the same variable which held the original list:
  782.  
  783.           (setq x (nreverse x))
  784.  
  785.      Here is the `nreverse' of our favorite example, `(a b c)',
  786.      presented graphically:
  787.  
  788.           Original list head:                       Reversed list:
  789.            -------------        -------------        ------------
  790.           | car  | cdr  |      | car  | cdr  |      | car | cdr  |
  791.           |   a  |  nil |<--   |   b  |   o  |<--   |   c |   o  |
  792.           |      |      |   |  |      |   |  |   |  |     |   |  |
  793.            -------------    |   --------- | -    |   -------- | -
  794.                             |             |      |            |
  795.                              -------------        ------------
  796.  
  797.  - Function: sort LIST PREDICATE
  798.      This function sorts LIST stably, though destructively, and returns
  799.      the sorted list.  It compares elements using PREDICATE.  A stable
  800.      sort is one in which elements with equal sort keys maintain their
  801.      relative order before and after the sort.  Stability is important
  802.      when successive sorts are used to order elements according to
  803.      different criteria.
  804.  
  805.      The argument PREDICATE must be a function that accepts two
  806.      arguments.  It is called with two elements of LIST.  To get an
  807.      increasing order sort, the PREDICATE should return `t' if the
  808.      first element is "less than" the second, or `nil' if not.
  809.  
  810.      The destructive aspect of `sort' is that it rearranges the cons
  811.      cells forming LIST by changing CDRs.  A nondestructive sort
  812.      function would create new cons cells to store the elements in their
  813.      sorted order.  If you wish to sort a list without destroying the
  814.      original, copy it first with `copy-sequence'.
  815.  
  816.      The CARs of the cons cells are not changed; the cons cell that
  817.      originally contained the element `a' in LIST still has `a' in its
  818.      CAR after sorting, but it now appears in a different position in
  819.      the list due to the change of CDRs.  For example:
  820.  
  821.           (setq nums '(1 3 2 6 5 4 0))
  822.                => (1 3 2 6 5 4 0)
  823.           (sort nums '<)
  824.                => (0 1 2 3 4 5 6)
  825.           nums
  826.                => (1 2 3 4 5 6)
  827.  
  828.      Note that the list in `nums' no longer contains 0; this is the same
  829.      cons cell that it was before, but it is no longer the first one in
  830.      the list.  Don't assume a variable that formerly held the argument
  831.      now holds the entire sorted list!  Instead, save the result of
  832.      `sort' and use that.  Most often we store the result back into the
  833.      variable that held the original list:
  834.  
  835.           (setq nums (sort nums '<))
  836.  
  837.      *Note Sorting::, for more functions that perform sorting.  See
  838.      `documentation' in *Note Accessing Documentation::, for a useful
  839.      example of `sort'.
  840.  
  841.    See `delq', in *Note Sets And Lists::, for another function that
  842. modifies cons cells.
  843.  
  844. 
  845. File: elisp,  Node: Sets And Lists,  Next: Association Lists,  Prev: Modifying Lists,  Up: Lists
  846.  
  847. Using Lists as Sets
  848. ===================
  849.  
  850.    A list can represent an unordered mathematical set--simply consider a
  851. value an element of a set if it appears in the list, and ignore the
  852. order of the list.  To form the union of two sets, use `append' (as
  853. long as you don't mind having duplicate elements).  Other useful
  854. functions for sets include `memq' and `delq', and their `equal'
  855. versions, `member' and `delete'.
  856.  
  857.      Common Lisp note: Common Lisp has functions `union' (which avoids
  858.      duplicate elements) and `intersection' for set operations, but GNU
  859.      Emacs Lisp does not have them.  You can write them in Lisp if you
  860.      wish.
  861.  
  862.  - Function: memq OBJECT LIST
  863.      This function tests to see whether OBJECT is a member of LIST.  If
  864.      it is, `memq' returns a list starting with the first occurrence of
  865.      OBJECT.  Otherwise, it returns `nil'.  The letter `q' in `memq'
  866.      says that it uses `eq' to compare OBJECT against the elements of
  867.      the list.  For example:
  868.  
  869.           (memq 2 '(1 2 3 2 1))
  870.                => (2 3 2 1)
  871.           (memq '(2) '((1) (2)))    ; `(2)' and `(2)' are not `eq'.
  872.                => nil
  873.  
  874.  - Function: delq OBJECT LIST
  875.      This function removes all elements `eq' to OBJECT from LIST.  The
  876.      letter `q' in `delq' says that it uses `eq' to compare OBJECT
  877.      against the elements of the list, like `memq'.
  878.  
  879.    When `delq' deletes elements from the front of the list, it does so
  880. simply by advancing down the list and returning a sublist that starts
  881. after those elements:
  882.  
  883.      (delq 'a '(a b c))
  884.      ==
  885.      (cdr '(a b c))
  886.  
  887.    When an element to be deleted appears in the middle of the list,
  888. removing it involves changing the CDRs (*note Setcdr::.).
  889.  
  890.      (setq sample-list '(1 2 3 (4)))
  891.           => (1 2 3 (4))
  892.      (delq 1 sample-list)
  893.           => (2 3 (4))
  894.      sample-list
  895.           => (1 2 3 (4))
  896.      (delq 2 sample-list)
  897.           => (1 3 (4))
  898.      sample-list
  899.           => (1 3 (4))
  900.  
  901.    Note that `(delq 2 sample-list)' modifies `sample-list' to splice
  902. out the second element, but `(delq 1 sample-list)' does not splice
  903. anything--it just returns a shorter list.  Don't assume that a variable
  904. which formerly held the argument LIST now has fewer elements, or that
  905. it still holds the original list!  Instead, save the result of `delq'
  906. and use that.  Most often we store the result back into the variable
  907. that held the original list:
  908.  
  909.      (setq flowers (delq 'rose flowers))
  910.  
  911.    In the following example, the `(4)' that `delq' attempts to match
  912. and the `(4)' in the `sample-list' are not `eq':
  913.  
  914.      (delq '(4) sample-list)
  915.           => (1 3 (4))
  916.  
  917.    The following two functions are like `memq' and `delq' but use
  918. `equal' rather than `eq' to compare elements.  They are new in Emacs 19.
  919.  
  920.  - Function: member OBJECT LIST
  921.      The function `member' tests to see whether OBJECT is a member of
  922.      LIST, comparing members with OBJECT using `equal'.  If OBJECT is a
  923.      member, `memq' returns a list starting with its first occurrence
  924.      in LIST.  Otherwise, it returns `nil'.
  925.  
  926.      Compare this with `memq':
  927.  
  928.           (member '(2) '((1) (2)))  ; `(2)' and `(2)' are `equal'.
  929.                => ((2))
  930.           (memq '(2) '((1) (2)))    ; `(2)' and `(2)' are not `eq'.
  931.                => nil
  932.           ;; Two strings with the same contents are `equal'.
  933.           (member "foo" '("foo" "bar"))
  934.                => ("foo" "bar")
  935.  
  936.  - Function: delete OBJECT LIST
  937.      This function removes all elements `equal' to OBJECT from LIST.
  938.      It is to `delq' as `member' is to `memq': it uses `equal' to
  939.      compare elements with OBJECT, like `member'; when it finds an
  940.      element that matches, it removes the element just as `delq' would.
  941.      For example:
  942.  
  943.           (delete '(2) '((2) (1) (2)))
  944.                => '((1))
  945.  
  946.      Common Lisp note: The functions `member' and `delete' in GNU Emacs
  947.      Lisp are derived from Maclisp, not Common Lisp.  The Common Lisp
  948.      versions do not use `equal' to compare elements.
  949.  
  950. 
  951. File: elisp,  Node: Association Lists,  Prev: Sets And Lists,  Up: Lists
  952.  
  953. Association Lists
  954. =================
  955.  
  956.    An "association list", or "alist" for short, records a mapping from
  957. keys to values.  It is a list of cons cells called "associations": the
  958. CAR of each cell is the "key", and the CDR is the "associated value".
  959. (This usage of "key" is not related to the term "key sequence"; it
  960. means any object which can be looked up in a table.)
  961.  
  962.    Here is an example of an alist.  The key `pine' is associated with
  963. the value `cones'; the key `oak' is associated with `acorns'; and the
  964. key `maple' is associated with `seeds'.
  965.  
  966.      '((pine . cones)
  967.        (oak . acorns)
  968.        (maple . seeds))
  969.  
  970.    The associated values in an alist may be any Lisp objects; so may the
  971. keys.  For example, in the following alist, the symbol `a' is
  972. associated with the number `1', and the string `"b"' is associated with
  973. the *list* `(2 3)', which is the CDR of the alist element:
  974.  
  975.      ((a . 1) ("b" 2 3))
  976.  
  977.    Sometimes it is better to design an alist to store the associated
  978. value in the CAR of the CDR of the element.  Here is an example:
  979.  
  980.      '((rose red) (lily white) (buttercup yellow)))
  981.  
  982. Here we regard `red' as the value associated with `rose'.  One
  983. advantage of this method is that you can store other related
  984. information--even a list of other items--in the CDR of the CDR.  One
  985. disadvantage is that you cannot use `rassq' (see below) to find the
  986. element containing a given value.  When neither of these considerations
  987. is important, the choice is a matter of taste, as long as you are
  988. consistent about it for any given alist.
  989.  
  990.    Note that the same alist shown above could be regarded as having the
  991. associated value in the CDR of the element; the value associated with
  992. `rose' would be the list `(red)'.
  993.  
  994.    Association lists are often used to record information that you might
  995. otherwise keep on a stack, since new associations may be added easily to
  996. the front of the list.  When searching an association list for an
  997. association with a given key, the first one found is returned, if there
  998. is more than one.
  999.  
  1000.    In Emacs Lisp, it is *not* an error if an element of an association
  1001. list is not a cons cell.  The alist search functions simply ignore such
  1002. elements.  Many other versions of Lisp signal errors in such cases.
  1003.  
  1004.    Note that property lists are similar to association lists in several
  1005. respects.  A property list behaves like an association list in which
  1006. each key can occur only once.  *Note Property Lists::, for a comparison
  1007. of property lists and association lists.
  1008.  
  1009.  - Function: assoc KEY ALIST
  1010.      This function returns the first association for KEY in ALIST.  It
  1011.      compares KEY against the alist elements using `equal' (*note
  1012.      Equality Predicates::.).  It returns `nil' if no association in
  1013.      ALIST has a CAR `equal' to KEY.  For example:
  1014.  
  1015.           (setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
  1016.                => ((pine . cones) (oak . acorns) (maple . seeds))
  1017.           (assoc 'oak trees)
  1018.                => (oak . acorns)
  1019.           (cdr (assoc 'oak trees))
  1020.                => acorns
  1021.           (assoc 'birch trees)
  1022.                => nil
  1023.  
  1024.      Here is another example in which the keys and values are not
  1025.      symbols:
  1026.  
  1027.           (setq needles-per-cluster
  1028.                 '((2 . ("Austrian Pine" "Red Pine"))
  1029.                   (3 . "Pitch Pine")
  1030.                   (5 . "White Pine")))
  1031.           
  1032.           (cdr (assoc 3 needles-per-cluster))
  1033.                => "Pitch Pine"
  1034.           (cdr (assoc 2 needles-per-cluster))
  1035.                => ("Austrian Pine" "Red Pine")
  1036.  
  1037.  - Function: assq KEY ALIST
  1038.      This function is like `assoc' in that it returns the first
  1039.      association for KEY in ALIST, but it makes the comparison using
  1040.      `eq' instead of `equal'.  `assq' returns `nil' if no association
  1041.      in ALIST has a CAR `eq' to KEY.  This function is used more often
  1042.      than `assoc', since `eq' is faster than `equal' and most alists
  1043.      use symbols as keys.  *Note Equality Predicates::.
  1044.  
  1045.           (setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
  1046.           
  1047.           (assq 'pine trees)
  1048.                => (pine . cones)
  1049.  
  1050.      On the other hand, `assq' is not usually useful in alists where the
  1051.      keys may not be symbols:
  1052.  
  1053.           (setq leaves
  1054.                 '(("simple leaves" . oak)
  1055.                   ("compound leaves" . horsechestnut)))
  1056.           
  1057.           (assq "simple leaves" leaves)
  1058.                => nil
  1059.           (assoc "simple leaves" leaves)
  1060.                => ("simple leaves" . oak)
  1061.  
  1062.  - Function: rassq ALIST VALUE
  1063.      This function returns the first association with value VALUE in
  1064.      ALIST.  It returns `nil' if no association in ALIST has a CDR `eq'
  1065.      to VALUE.
  1066.  
  1067.      `rassq' is like `assq' except that the CDR of the ALIST
  1068.      associations is tested instead of the CAR.  You can think of this
  1069.      as "reverse `assq'", finding the key for a given value.
  1070.  
  1071.      For example:
  1072.  
  1073.           (setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
  1074.           
  1075.           (rassq 'acorns trees)
  1076.                => (oak . acorns)
  1077.           (rassq 'spores trees)
  1078.                => nil
  1079.  
  1080.      Note that `rassq' cannot be used to search for a value stored in
  1081.      the CAR of the CDR of an element:
  1082.  
  1083.           (setq colors '((rose red) (lily white) (buttercup yellow)))
  1084.           
  1085.           (rassq 'white colors)
  1086.                => nil
  1087.  
  1088.      In this case, the CDR of the association `(lily white)' is not the
  1089.      symbol `white', but rather the list `(white)'.  This can be seen
  1090.      more clearly if the association is written in dotted pair notation:
  1091.  
  1092.           (lily white) == (lily . (white))
  1093.  
  1094.  - Function: copy-alist ALIST
  1095.      This function returns a two-level deep copy of ALIST: it creates a
  1096.      new copy of each association, so that you can alter the
  1097.      associations of the new alist without changing the old one.
  1098.  
  1099.           (setq needles-per-cluster
  1100.                 '((2 . ("Austrian Pine" "Red Pine"))
  1101.                   (3 . "Pitch Pine")
  1102.                   (5 . "White Pine")))
  1103.           =>
  1104.           ((2 "Austrian Pine" "Red Pine")
  1105.            (3 . "Pitch Pine")
  1106.            (5 . "White Pine"))
  1107.           
  1108.           (setq copy (copy-alist needles-per-cluster))
  1109.           =>
  1110.           ((2 "Austrian Pine" "Red Pine")
  1111.            (3 . "Pitch Pine")
  1112.            (5 . "White Pine"))
  1113.           
  1114.           (eq needles-per-cluster copy)
  1115.                => nil
  1116.           (equal needles-per-cluster copy)
  1117.                => t
  1118.           (eq (car needles-per-cluster) (car copy))
  1119.                => nil
  1120.           (cdr (car (cdr needles-per-cluster)))
  1121.                => "Pitch Pine"
  1122.           (eq (cdr (car (cdr needles-per-cluster)))
  1123.               (cdr (car (cdr copy))))
  1124.                => t
  1125.  
  1126. 
  1127. File: elisp,  Node: Sequences Arrays Vectors,  Next: Symbols,  Prev: Lists,  Up: Top
  1128.  
  1129. Sequences, Arrays, and Vectors
  1130. ******************************
  1131.  
  1132.    Recall that the "sequence" type is the union of three other Lisp
  1133. types: lists, vectors, and strings.  In other words, any list is a
  1134. sequence, any vector is a sequence, and any string is a sequence.  The
  1135. common property that all sequences have is that each is an ordered
  1136. collection of elements.
  1137.  
  1138.    An "array" is a single primitive object directly containing all its
  1139. elements.  Therefore, all the elements are accessible in constant time.
  1140. The length of an existing array cannot be changed.  Both strings and
  1141. vectors are arrays.  A list is a sequence of elements, but it is not a
  1142. single primitive object; it is made of cons cells, one cell per
  1143. element.  Therefore, elements farther from the beginning of the list
  1144. take longer to access, but it is possible to add elements to the list or
  1145. remove elements.  The elements of vectors and lists may be any Lisp
  1146. objects.  The elements of strings are all characters.
  1147.  
  1148.    The following diagram shows the relationship between these types:
  1149.  
  1150.                  ___________________________________
  1151.                 |                                   |
  1152.                 |          Sequence                 |
  1153.                 |  ______   ______________________  |
  1154.                 | |      | |                      | |
  1155.                 | | List | |         Array        | |
  1156.                 | |      | |  ________   _______  | |
  1157.                 | |______| | |        | |       | | |
  1158.                 |          | | String | | Vector| | |
  1159.                 |          | |________| |_______| | |
  1160.                 |          |______________________| |
  1161.                 |___________________________________|
  1162.  
  1163.           The Relationship between Sequences, Arrays, and Vectors
  1164.  
  1165.  
  1166. * Menu:
  1167.  
  1168. * Sequence Functions::    Functions that accept any kind of sequence.
  1169. * Arrays::                Characteristics of arrays in Emacs Lisp.
  1170. * Array Functions::       Functions specifically for arrays.
  1171. * Vectors::               Functions specifically for vectors.
  1172.  
  1173. 
  1174. File: elisp,  Node: Sequence Functions,  Next: Arrays,  Prev: Sequences Arrays Vectors,  Up: Sequences Arrays Vectors
  1175.  
  1176. Sequences
  1177. =========
  1178.  
  1179.    In Emacs Lisp, a "sequence" is either a list, a vector or a string.
  1180. The common property that all sequences have is that each is an ordered
  1181. collection of elements.  This section describes functions that accept
  1182. any kind of sequence.
  1183.  
  1184.  - Function: sequencep OBJECT
  1185.      Returns `t' if OBJECT is a list, vector, or string, `nil'
  1186.      otherwise.
  1187.  
  1188.  - Function: copy-sequence SEQUENCE
  1189.      Returns a copy of SEQUENCE.  The copy is the same type of object
  1190.      as the original sequence, and it has the same elements in the same
  1191.      order.
  1192.  
  1193.      Storing a new element into the copy does not affect the original
  1194.      SEQUENCE, and vice versa.  However, the elements of the new
  1195.      sequence are not copies; they are identical (`eq') to the elements
  1196.      of the original.  Therefore, changes made within these elements, as
  1197.      found via the copied sequence, are also visible in the original
  1198.      sequence.
  1199.  
  1200.      If the sequence is a string with text properties, the property
  1201.      list in the copy is itself a copy, not shared with the original's
  1202.      property list.  However, the actual values of the properties are
  1203.      shared.  *Note Text Properties::.
  1204.  
  1205.      See also `append' in *Note Building Lists::, `concat' in *Note
  1206.      Creating Strings::, and `vconcat' in *Note Vectors::, for others
  1207.      ways to copy sequences.
  1208.  
  1209.           (setq bar '(1 2))
  1210.                => (1 2)
  1211.           (setq x (vector 'foo bar))
  1212.                => [foo (1 2)]
  1213.           (setq y (copy-sequence x))
  1214.                => [foo (1 2)]
  1215.           
  1216.           (eq x y)
  1217.                => nil
  1218.           (equal x y)
  1219.                => t
  1220.           (eq (elt x 1) (elt y 1))
  1221.                => t
  1222.           
  1223.           ;; Replacing an element of one sequence.
  1224.           (aset x 0 'quux)
  1225.           x => [quux (1 2)]
  1226.           y => [foo (1 2)]
  1227.           
  1228.           ;; Modifying the inside of a shared element.
  1229.           (setcar (aref x 1) 69)
  1230.           x => [quux (69 2)]
  1231.           y => [foo (69 2)]
  1232.  
  1233.  - Function: length SEQUENCE
  1234.      Returns the number of elements in SEQUENCE.  If SEQUENCE is a cons
  1235.      cell that is not a list (because the final CDR is not `nil'), a
  1236.      `wrong-type-argument' error is signaled.
  1237.  
  1238.           (length '(1 2 3))
  1239.               => 3
  1240.           (length ())
  1241.               => 0
  1242.           (length "foobar")
  1243.               => 6
  1244.           (length [1 2 3])
  1245.               => 3
  1246.  
  1247.  - Function: elt SEQUENCE INDEX
  1248.      This function returns the element of SEQUENCE indexed by INDEX.
  1249.      Legitimate values of INDEX are integers ranging from 0 up to one
  1250.      less than the length of SEQUENCE.  If SEQUENCE is a list, then
  1251.      out-of-range values of index return `nil'; otherwise, they produce
  1252.      an `args-out-of-range' error.
  1253.  
  1254.           (elt [1 2 3 4] 2)
  1255.                => 3
  1256.           (elt '(1 2 3 4) 2)
  1257.                => 3
  1258.           (char-to-string (elt "1234" 2))
  1259.                => "3"
  1260.           (elt [1 2 3 4] 4)
  1261.                error-->Args out of range: [1 2 3 4], 4
  1262.           (elt [1 2 3 4] -1)
  1263.                error-->Args out of range: [1 2 3 4], -1
  1264.  
  1265.      This function duplicates `aref' (*note Array Functions::.) and
  1266.      `nth' (*note List Elements::.), except that it works for any kind
  1267.      of sequence.
  1268.  
  1269. 
  1270. File: elisp,  Node: Arrays,  Next: Array Functions,  Prev: Sequence Functions,  Up: Sequences Arrays Vectors
  1271.  
  1272. Arrays
  1273. ======
  1274.  
  1275.    An "array" object refers directly to a number of other Lisp objects,
  1276. called the elements of the array.  Any element of an array may be
  1277. accessed in constant time.  In contrast, an element of a list requires
  1278. access time that is proportional to the position of the element in the
  1279. list.
  1280.  
  1281.    When you create an array, you must specify how many elements it has.
  1282. The amount of space allocated depends on the number of elements.
  1283. Therefore, it is impossible to change the size of an array once it is
  1284. created.  You cannot add or remove elements.  However, you can replace
  1285. an element with a different value.
  1286.  
  1287.    Emacs defines two types of array, both of which are one-dimensional:
  1288. "strings" and "vectors".  A vector is a general array; its elements can
  1289. be any Lisp objects.  A string is a specialized array; its elements
  1290. must be characters (i.e., integers between 0 and 255).  Each type of
  1291. array has its own read syntax.  *Note String Type::, and *Note Vector
  1292. Type::.
  1293.  
  1294.    Both kinds of arrays share these characteristics:
  1295.  
  1296.    * The first element of an array has index zero, the second element
  1297.      has index 1, and so on.  This is called "zero-origin" indexing.
  1298.      For example, an array of four elements has indices 0, 1, 2, and 3.
  1299.  
  1300.    * The elements of an array may be referenced or changed with the
  1301.      functions `aref' and `aset', respectively (*note Array
  1302.      Functions::.).
  1303.  
  1304.    In principle, if you wish to have an array of characters, you could
  1305. use either a string or a vector.  In practice, we always choose strings
  1306. for such applications, for four reasons:
  1307.  
  1308.    * They occupy one-fourth the space of a vector of the same elements.
  1309.  
  1310.    * Strings are printed in a way that shows the contents more clearly
  1311.      as characters.
  1312.  
  1313.    * Strings can hold text properties.  *Note Text Properties::.
  1314.  
  1315.    * Many of the specialized editing and I/O facilities of Emacs accept
  1316.      only strings.  For example, you cannot insert a vector of
  1317.      characters into a buffer the way you can insert a string.  *Note
  1318.      Strings and Characters::.
  1319.  
  1320.