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