annotate man/lispref/lists.texi @ 412:697ef44129c6 r21-2-14

Import from CVS: tag r21-2-14
author cvs
date Mon, 13 Aug 2007 11:20:41 +0200
parents 74fd4e045ea6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 @c -*-texinfo-*-
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 @c This is part of the XEmacs Lisp Reference Manual.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 @c See the file lispref.texi for copying conditions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 @setfilename ../../info/lists.info
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 @node Lists, Sequences Arrays Vectors, Strings and Characters, Top
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 @chapter Lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 @cindex list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 @cindex element (of list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 A @dfn{list} represents a sequence of zero or more elements (which may
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 be any Lisp objects). The important difference between lists and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 vectors is that two or more lists can share part of their structure; in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 addition, you can insert or delete elements in a list without copying
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 the whole list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 @menu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 * Cons Cells:: How lists are made out of cons cells.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 * Lists as Boxes:: Graphical notation to explain lists.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 * List-related Predicates:: Is this object a list? Comparing two lists.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 * List Elements:: Extracting the pieces of a list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 * Building Lists:: Creating list structure.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 * Modifying Lists:: Storing new pieces into an existing list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 * Sets And Lists:: A list can represent a finite mathematical set.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 * Association Lists:: A list can represent a finite relation or mapping.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 * Property Lists:: A different way to represent a finite mapping.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 * Weak Lists:: A list with special garbage-collection behavior.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 @end menu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 @node Cons Cells
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 @section Lists and Cons Cells
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 @cindex lists and cons cells
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 @cindex @code{nil} and lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 Lists in Lisp are not a primitive data type; they are built up from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 @dfn{cons cells}. A cons cell is a data object that represents an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ordered pair. It records two Lisp objects, one labeled as the @sc{car},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 and the other labeled as the @sc{cdr}. These names are traditional; see
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 @ref{Cons Cell Type}. @sc{cdr} is pronounced ``could-er.''
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 A list is a series of cons cells chained together, one cons cell per
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 element of the list. By convention, the @sc{car}s of the cons cells are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 the elements of the list, and the @sc{cdr}s are used to chain the list:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 the @sc{cdr} of each cons cell is the following cons cell. The @sc{cdr}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 of the last cons cell is @code{nil}. This asymmetry between the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 @sc{car} and the @sc{cdr} is entirely a matter of convention; at the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 level of cons cells, the @sc{car} and @sc{cdr} slots have the same
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 characteristics.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 @cindex list structure
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 Because most cons cells are used as part of lists, the phrase
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 @dfn{list structure} has come to mean any structure made out of cons
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 cells.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 The symbol @code{nil} is considered a list as well as a symbol; it is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 the list with no elements. For convenience, the symbol @code{nil} is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 considered to have @code{nil} as its @sc{cdr} (and also as its
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 @sc{car}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 The @sc{cdr} of any nonempty list @var{l} is a list containing all the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 elements of @var{l} except the first.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 @node Lists as Boxes
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 @section Lists as Linked Pairs of Boxes
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 @cindex box representation for lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 @cindex lists represented as boxes
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 @cindex cons cell as box
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 A cons cell can be illustrated as a pair of boxes. The first box
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 represents the @sc{car} and the second box represents the @sc{cdr}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 Here is an illustration of the two-element list, @code{(tulip lily)},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 made from two cons cells:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 --------------- ---------------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 | car | cdr | | car | cdr |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 | tulip | o---------->| lily | nil |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 | | | | | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 --------------- ---------------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 Each pair of boxes represents a cons cell. Each box ``refers to'',
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 ``points to'' or ``contains'' a Lisp object. (These terms are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 synonymous.) The first box, which is the @sc{car} of the first cons
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 cell, contains the symbol @code{tulip}. The arrow from the @sc{cdr} of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 the first cons cell to the second cons cell indicates that the @sc{cdr}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 of the first cons cell points to the second cons cell.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 The same list can be illustrated in a different sort of box notation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 like this:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 ___ ___ ___ ___
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 |___|___|--> |___|___|--> nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 --> tulip --> lily
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 Here is a more complex illustration, showing the three-element list,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 @code{((pine needles) oak maple)}, the first element of which is a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 two-element list:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 ___ ___ ___ ___ ___ ___
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 |___|___|--> |___|___|--> |___|___|--> nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 | | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 | | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 | --> oak --> maple
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 | ___ ___ ___ ___
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 --> |___|___|--> |___|___|--> nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 --> pine --> needles
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 The same list represented in the first box notation looks like this:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 -------------- -------------- --------------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 | car | cdr | | car | cdr | | car | cdr |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 | o | o------->| oak | o------->| maple | nil |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 | | | | | | | | | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 -- | --------- -------------- --------------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 | -------------- ----------------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 | | car | cdr | | car | cdr |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 ------>| pine | o------->| needles | nil |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 | | | | | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 -------------- ----------------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 @xref{Cons Cell Type}, for the read and print syntax of cons cells and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 lists, and for more ``box and arrow'' illustrations of lists.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 @node List-related Predicates
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 @section Predicates on Lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 The following predicates test whether a Lisp object is an atom, is a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 cons cell or is a list, or whether it is the distinguished object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 @code{nil}. (Many of these predicates can be defined in terms of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 others, but they are used so often that it is worth having all of them.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 @defun consp object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 This function returns @code{t} if @var{object} is a cons cell, @code{nil}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 otherwise. @code{nil} is not a cons cell, although it @emph{is} a list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 @defun atom object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 @cindex atoms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 This function returns @code{t} if @var{object} is an atom, @code{nil}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 otherwise. All objects except cons cells are atoms. The symbol
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 @code{nil} is an atom and is also a list; it is the only Lisp object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 that is both.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 (atom @var{object}) @equiv{} (not (consp @var{object}))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 @defun listp object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 This function returns @code{t} if @var{object} is a cons cell or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 @code{nil}. Otherwise, it returns @code{nil}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 (listp '(1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 @result{} t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 (listp '())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 @result{} t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 @defun nlistp object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 This function is the opposite of @code{listp}: it returns @code{t} if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 @var{object} is not a list. Otherwise, it returns @code{nil}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 (listp @var{object}) @equiv{} (not (nlistp @var{object}))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 @defun null object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 This function returns @code{t} if @var{object} is @code{nil}, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 returns @code{nil} otherwise. This function is identical to @code{not},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 but as a matter of clarity we use @code{null} when @var{object} is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 considered a list and @code{not} when it is considered a truth value
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 (see @code{not} in @ref{Combining Conditions}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 (null '(1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 @result{} nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 (null '())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 @result{} t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 @need 2000
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 @node List Elements
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 @section Accessing Elements of Lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 @cindex list elements
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 @defun car cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 This function returns the value pointed to by the first pointer of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 cons cell @var{cons-cell}. Expressed another way, this function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 returns the @sc{car} of @var{cons-cell}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 As a special case, if @var{cons-cell} is @code{nil}, then @code{car}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 is defined to return @code{nil}; therefore, any list is a valid argument
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 for @code{car}. An error is signaled if the argument is not a cons cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 or @code{nil}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 (car '(a b c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 @result{} a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 (car '())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 @result{} nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 @defun cdr cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 This function returns the value pointed to by the second pointer of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 the cons cell @var{cons-cell}. Expressed another way, this function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 returns the @sc{cdr} of @var{cons-cell}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 As a special case, if @var{cons-cell} is @code{nil}, then @code{cdr}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 is defined to return @code{nil}; therefore, any list is a valid argument
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 for @code{cdr}. An error is signaled if the argument is not a cons cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 or @code{nil}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 (cdr '(a b c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 @result{} (b c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 (cdr '())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 @result{} nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 @defun car-safe object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 This function lets you take the @sc{car} of a cons cell while avoiding
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 errors for other data types. It returns the @sc{car} of @var{object} if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 @var{object} is a cons cell, @code{nil} otherwise. This is in contrast
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 to @code{car}, which signals an error if @var{object} is not a list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 (car-safe @var{object})
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 @equiv{}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 (let ((x @var{object}))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 (if (consp x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 (car x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 @defun cdr-safe object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 This function lets you take the @sc{cdr} of a cons cell while
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 avoiding errors for other data types. It returns the @sc{cdr} of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 @var{object} if @var{object} is a cons cell, @code{nil} otherwise.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 This is in contrast to @code{cdr}, which signals an error if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 @var{object} is not a list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 (cdr-safe @var{object})
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 @equiv{}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 (let ((x @var{object}))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 (if (consp x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 (cdr x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 @defun nth n list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 This function returns the @var{n}th element of @var{list}. Elements
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 are numbered starting with zero, so the @sc{car} of @var{list} is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 element number zero. If the length of @var{list} is @var{n} or less,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 the value is @code{nil}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 If @var{n} is negative, @code{nth} returns the first element of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 @var{list}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 (nth 2 '(1 2 3 4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 @result{} 3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 (nth 10 '(1 2 3 4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 @result{} nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 (nth -3 '(1 2 3 4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 @result{} 1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 (nth n x) @equiv{} (car (nthcdr n x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 @defun nthcdr n list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 This function returns the @var{n}th @sc{cdr} of @var{list}. In other
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 words, it removes the first @var{n} links of @var{list} and returns
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 what follows.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 If @var{n} is zero or negative, @code{nthcdr} returns all of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 @var{list}. If the length of @var{list} is @var{n} or less,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 @code{nthcdr} returns @code{nil}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 (nthcdr 1 '(1 2 3 4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 @result{} (2 3 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 (nthcdr 10 '(1 2 3 4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 @result{} nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 (nthcdr -3 '(1 2 3 4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 @result{} (1 2 3 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 Many convenience functions are provided to make it easier for you to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 access particular elements in a nested list. All of these can be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 rewritten in terms of the functions just described.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 @defun caar cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 @defunx cadr cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 @defunx cdar cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 @defunx cddr cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 @defunx caaar cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 @defunx caadr cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 @defunx cadar cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 @defunx caddr cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 @defunx cdaar cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 @defunx cdadr cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 @defunx cddar cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 @defunx cdddr cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 @defunx caaaar cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 @defunx caaadr cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 @defunx caadar cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 @defunx caaddr cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 @defunx cadaar cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 @defunx cadadr cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 @defunx caddar cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 @defunx cadddr cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 @defunx cdaaar cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 @defunx cdaadr cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 @defunx cdadar cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 @defunx cdaddr cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 @defunx cddaar cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 @defunx cddadr cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 @defunx cdddar cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 @defunx cddddr cons-cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 Each of these functions is equivalent to one or more applications of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 @code{car} and/or @code{cdr}. For example,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 (cadr x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 is equivalent to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 (car (cdr x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 (cdaddr x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 is equivalent to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 (cdr (car (cdr (cdr x))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 That is to say, read the a's and d's from right to left and apply
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 a @code{car} or @code{cdr} for each a or d found, respectively.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 @defun first list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 This is equivalent to @code{(nth 0 @var{list})}, i.e. the first element
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 of @var{list}. (Note that this is also equivalent to @code{car}.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 @defun second list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 This is equivalent to @code{(nth 1 @var{list})}, i.e. the second element
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 of @var{list}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 @defun third list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 @defunx fourth list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 @defunx fifth list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 @defunx sixth list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 @defunx seventh list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 @defunx eighth list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 @defunx ninth list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 @defunx tenth list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 These are equivalent to @code{(nth 2 @var{list})} through
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 @code{(nth 9 @var{list})} respectively, i.e. the third through tenth
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 elements of @var{list}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 @node Building Lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 @section Building Cons Cells and Lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 @cindex cons cells
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 @cindex building lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 Many functions build lists, as lists reside at the very heart of Lisp.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 @code{cons} is the fundamental list-building function; however, it is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 interesting to note that @code{list} is used more times in the source
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 code for Emacs than @code{cons}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 @defun cons object1 object2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 This function is the fundamental function used to build new list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 structure. It creates a new cons cell, making @var{object1} the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 @sc{car}, and @var{object2} the @sc{cdr}. It then returns the new cons
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 cell. The arguments @var{object1} and @var{object2} may be any Lisp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 objects, but most often @var{object2} is a list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 (cons 1 '(2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 @result{} (1 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 (cons 1 '())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 @result{} (1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 (cons 1 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 @result{} (1 . 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 @cindex consing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 @code{cons} is often used to add a single element to the front of a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 list. This is called @dfn{consing the element onto the list}. For
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 example:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 (setq list (cons newelt list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 Note that there is no conflict between the variable named @code{list}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 used in this example and the function named @code{list} described below;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 any symbol can serve both purposes.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 @defun list &rest objects
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 This function creates a list with @var{objects} as its elements. The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 resulting list is always @code{nil}-terminated. If no @var{objects}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 are given, the empty list is returned.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 (list 1 2 3 4 5)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 @result{} (1 2 3 4 5)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 (list 1 2 '(3 4 5) 'foo)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 @result{} (1 2 (3 4 5) foo)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 (list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 @result{} nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 @defun make-list length object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 This function creates a list of length @var{length}, in which all the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 elements have the identical value @var{object}. Compare
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 @code{make-list} with @code{make-string} (@pxref{Creating Strings}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 (make-list 3 'pigs)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 @result{} (pigs pigs pigs)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 (make-list 0 'pigs)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 @result{} nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 @defun append &rest sequences
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 @cindex copying lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 This function returns a list containing all the elements of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 @var{sequences}. The @var{sequences} may be lists, vectors, or strings,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 but the last one should be a list. All arguments except the last one
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 are copied, so none of them are altered.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 More generally, the final argument to @code{append} may be any Lisp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 object. The final argument is not copied or converted; it becomes the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 @sc{cdr} of the last cons cell in the new list. If the final argument
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532 is itself a list, then its elements become in effect elements of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 result list. If the final element is not a list, the result is a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 ``dotted list'' since its final @sc{cdr} is not @code{nil} as required
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535 in a true list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 See @code{nconc} in @ref{Rearrangement}, for a way to join lists with no
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538 copying.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 Here is an example of using @code{append}:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 (setq trees '(pine oak))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 @result{} (pine oak)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 (setq more-trees (append '(maple birch) trees))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 @result{} (maple birch pine oak)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551 trees
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552 @result{} (pine oak)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
553 more-trees
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554 @result{} (maple birch pine oak)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557 (eq trees (cdr (cdr more-trees)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558 @result{} t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
561
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
562 You can see how @code{append} works by looking at a box diagram. The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
563 variable @code{trees} is set to the list @code{(pine oak)} and then the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564 variable @code{more-trees} is set to the list @code{(maple birch pine
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565 oak)}. However, the variable @code{trees} continues to refer to the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
566 original list:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568 @smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570 more-trees trees
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571 | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572 | ___ ___ ___ ___ -> ___ ___ ___ ___
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573 --> |___|___|--> |___|___|--> |___|___|--> |___|___|--> nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
574 | | | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575 | | | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576 --> maple -->birch --> pine --> oak
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
577 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
578 @end smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
579
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
580 An empty sequence contributes nothing to the value returned by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
581 @code{append}. As a consequence of this, a final @code{nil} argument
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582 forces a copy of the previous argument.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
585 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586 trees
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
587 @result{} (pine oak)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
588 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
589 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590 (setq wood (append trees ()))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
591 @result{} (pine oak)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594 wood
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 @result{} (pine oak)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
596 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
597 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
598 (eq wood trees)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
599 @result{} nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
600 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
601 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
602
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
603 @noindent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
604 This once was the usual way to copy a list, before the function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
605 @code{copy-sequence} was invented. @xref{Sequences Arrays Vectors}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
607 With the help of @code{apply}, we can append all the lists in a list of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
608 lists:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
610 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
611 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
612 (apply 'append '((a b c) nil (x y z) nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
613 @result{} (a b c x y z)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
614 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
615 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
616
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
617 If no @var{sequences} are given, @code{nil} is returned:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
618
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
619 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
620 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621 (append)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
622 @result{} nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
623 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
624 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
625
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
626 Here are some examples where the final argument is not a list:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
627
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
628 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
629 (append '(x y) 'z)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
630 @result{} (x y . z)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
631 (append '(x y) [z])
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
632 @result{} (x y . [z])
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
633 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
634
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
635 @noindent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
636 The second example shows that when the final argument is a sequence but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
637 not a list, the sequence's elements do not become elements of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
638 resulting list. Instead, the sequence becomes the final @sc{cdr}, like
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
639 any other non-list final argument.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
641 The @code{append} function also allows integers as arguments. It
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
642 converts them to strings of digits, making up the decimal print
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
643 representation of the integer, and then uses the strings instead of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
644 original integers. @strong{Don't use this feature; we plan to eliminate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
645 it. If you already use this feature, change your programs now!} The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
646 proper way to convert an integer to a decimal number in this way is with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
647 @code{format} (@pxref{Formatting Strings}) or @code{number-to-string}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
648 (@pxref{String Conversion}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
649 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
650
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
651 @defun reverse list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
652 This function creates a new list whose elements are the elements of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
653 @var{list}, but in reverse order. The original argument @var{list} is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
654 @emph{not} altered.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
655
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
656 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
657 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
658 (setq x '(1 2 3 4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
659 @result{} (1 2 3 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
660 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
661 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
662 (reverse x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
663 @result{} (4 3 2 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
664 x
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
665 @result{} (1 2 3 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
666 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
667 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
668 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
669
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
670 @node Modifying Lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
671 @section Modifying Existing List Structure
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
672
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
673 You can modify the @sc{car} and @sc{cdr} contents of a cons cell with the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
674 primitives @code{setcar} and @code{setcdr}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
675
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
676 @cindex CL note---@code{rplaca} vrs @code{setcar}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
677 @quotation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
678 @findex rplaca
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
679 @findex rplacd
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
680 @b{Common Lisp note:} Common Lisp uses functions @code{rplaca} and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
681 @code{rplacd} to alter list structure; they change structure the same
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
682 way as @code{setcar} and @code{setcdr}, but the Common Lisp functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
683 return the cons cell while @code{setcar} and @code{setcdr} return the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
684 new @sc{car} or @sc{cdr}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
685 @end quotation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
686
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
687 @menu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
688 * Setcar:: Replacing an element in a list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
689 * Setcdr:: Replacing part of the list backbone.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
690 This can be used to remove or add elements.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
691 * Rearrangement:: Reordering the elements in a list; combining lists.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
692 @end menu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
693
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
694 @node Setcar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
695 @subsection Altering List Elements with @code{setcar}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
696
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
697 Changing the @sc{car} of a cons cell is done with @code{setcar}. When
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
698 used on a list, @code{setcar} replaces one element of a list with a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
699 different element.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
700
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
701 @defun setcar cons object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
702 This function stores @var{object} as the new @sc{car} of @var{cons},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
703 replacing its previous @sc{car}. It returns the value @var{object}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
704 For example:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
705
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
706 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
707 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
708 (setq x '(1 2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
709 @result{} (1 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
710 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
711 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
712 (setcar x 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
713 @result{} 4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
714 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
715 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
716 x
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
717 @result{} (4 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
718 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
719 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
720 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
721
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
722 When a cons cell is part of the shared structure of several lists,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
723 storing a new @sc{car} into the cons changes one element of each of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
724 these lists. Here is an example:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
725
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
726 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
727 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
728 ;; @r{Create two lists that are partly shared.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
729 (setq x1 '(a b c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
730 @result{} (a b c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
731 (setq x2 (cons 'z (cdr x1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
732 @result{} (z b c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
733 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
734
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
735 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
736 ;; @r{Replace the @sc{car} of a shared link.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
737 (setcar (cdr x1) 'foo)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
738 @result{} foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
739 x1 ; @r{Both lists are changed.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
740 @result{} (a foo c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
741 x2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
742 @result{} (z foo c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
743 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
744
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
745 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
746 ;; @r{Replace the @sc{car} of a link that is not shared.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
747 (setcar x1 'baz)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
748 @result{} baz
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
749 x1 ; @r{Only one list is changed.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
750 @result{} (baz foo c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
751 x2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
752 @result{} (z foo c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
753 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
754 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
755
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
756 Here is a graphical depiction of the shared structure of the two lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
757 in the variables @code{x1} and @code{x2}, showing why replacing @code{b}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
758 changes them both:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
759
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
760 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
761 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
762 ___ ___ ___ ___ ___ ___
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
763 x1---> |___|___|----> |___|___|--> |___|___|--> nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
764 | --> | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
765 | | | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
766 --> a | --> b --> c
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
767 |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
768 ___ ___ |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
769 x2--> |___|___|--
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
770 |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
771 |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
772 --> z
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
773 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
774 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
775
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
776 Here is an alternative form of box diagram, showing the same relationship:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
777
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
778 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
779 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
780 x1:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
781 -------------- -------------- --------------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
782 | car | cdr | | car | cdr | | car | cdr |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
783 | a | o------->| b | o------->| c | nil |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
784 | | | -->| | | | | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
785 -------------- | -------------- --------------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
786 |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
787 x2: |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
788 -------------- |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
789 | car | cdr | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
790 | z | o----
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
791 | | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
792 --------------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
793 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
794 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
795
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
796 @node Setcdr
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
797 @subsection Altering the CDR of a List
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
798
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
799 The lowest-level primitive for modifying a @sc{cdr} is @code{setcdr}:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
800
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
801 @defun setcdr cons object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
802 This function stores @var{object} as the new @sc{cdr} of @var{cons},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
803 replacing its previous @sc{cdr}. It returns the value @var{object}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
804 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
805
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
806 Here is an example of replacing the @sc{cdr} of a list with a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
807 different list. All but the first element of the list are removed in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
808 favor of a different sequence of elements. The first element is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
809 unchanged, because it resides in the @sc{car} of the list, and is not
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
810 reached via the @sc{cdr}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
811
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
812 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
813 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
814 (setq x '(1 2 3))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
815 @result{} (1 2 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
816 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
817 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
818 (setcdr x '(4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
819 @result{} (4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
820 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
821 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
822 x
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
823 @result{} (1 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
824 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
825 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
826
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
827 You can delete elements from the middle of a list by altering the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
828 @sc{cdr}s of the cons cells in the list. For example, here we delete
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
829 the second element, @code{b}, from the list @code{(a b c)}, by changing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
830 the @sc{cdr} of the first cell:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
831
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
832 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
833 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
834 (setq x1 '(a b c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
835 @result{} (a b c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
836 (setcdr x1 (cdr (cdr x1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
837 @result{} (c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
838 x1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
839 @result{} (a c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
840 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
841 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
842
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
843 @need 4000
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
844 Here is the result in box notation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
845
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
846 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
847 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
848 --------------------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
849 | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
850 -------------- | -------------- | --------------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
851 | car | cdr | | | car | cdr | -->| car | cdr |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
852 | a | o----- | b | o-------->| c | nil |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
853 | | | | | | | | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
854 -------------- -------------- --------------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
855 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
856 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
857
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
858 @noindent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
859 The second cons cell, which previously held the element @code{b}, still
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
860 exists and its @sc{car} is still @code{b}, but it no longer forms part
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
861 of this list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
862
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
863 It is equally easy to insert a new element by changing @sc{cdr}s:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
864
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
865 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
866 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
867 (setq x1 '(a b c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
868 @result{} (a b c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
869 (setcdr x1 (cons 'd (cdr x1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
870 @result{} (d b c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
871 x1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
872 @result{} (a d b c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
873 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
874 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
875
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
876 Here is this result in box notation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
877
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
878 @smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
879 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
880 -------------- ------------- -------------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
881 | car | cdr | | car | cdr | | car | cdr |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
882 | a | o | -->| b | o------->| c | nil |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
883 | | | | | | | | | | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
884 --------- | -- | ------------- -------------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
885 | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
886 ----- --------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
887 | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
888 | --------------- |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
889 | | car | cdr | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
890 -->| d | o------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
891 | | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
892 ---------------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
893 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
894 @end smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
895
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
896 @node Rearrangement
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
897 @subsection Functions that Rearrange Lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
898 @cindex rearrangement of lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
899 @cindex modification of lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
900
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
901 Here are some functions that rearrange lists ``destructively'' by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
902 modifying the @sc{cdr}s of their component cons cells. We call these
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
903 functions ``destructive'' because they chew up the original lists passed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
904 to them as arguments, to produce a new list that is the returned value.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
905
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
906 @ifinfo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
907 See @code{delq}, in @ref{Sets And Lists}, for another function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
908 that modifies cons cells.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
909 @end ifinfo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
910 @iftex
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
911 The function @code{delq} in the following section is another example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
912 of destructive list manipulation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
913 @end iftex
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
914
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
915 @defun nconc &rest lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
916 @cindex concatenating lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
917 @cindex joining lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
918 This function returns a list containing all the elements of @var{lists}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
919 Unlike @code{append} (@pxref{Building Lists}), the @var{lists} are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
920 @emph{not} copied. Instead, the last @sc{cdr} of each of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
921 @var{lists} is changed to refer to the following list. The last of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
922 @var{lists} is not altered. For example:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
923
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
924 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
925 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
926 (setq x '(1 2 3))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
927 @result{} (1 2 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
928 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
929 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
930 (nconc x '(4 5))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
931 @result{} (1 2 3 4 5)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
932 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
933 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
934 x
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
935 @result{} (1 2 3 4 5)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
936 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
937 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
938
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
939 Since the last argument of @code{nconc} is not itself modified, it is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
940 reasonable to use a constant list, such as @code{'(4 5)}, as in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
941 above example. For the same reason, the last argument need not be a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
942 list:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
943
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
944 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
945 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
946 (setq x '(1 2 3))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
947 @result{} (1 2 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
948 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
949 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
950 (nconc x 'z)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
951 @result{} (1 2 3 . z)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
952 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
953 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
954 x
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
955 @result{} (1 2 3 . z)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
956 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
957 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
958
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
959 A common pitfall is to use a quoted constant list as a non-last
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
960 argument to @code{nconc}. If you do this, your program will change
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
961 each time you run it! Here is what happens:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
962
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
963 @smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
964 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
965 (defun add-foo (x) ; @r{We want this function to add}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
966 (nconc '(foo) x)) ; @r{@code{foo} to the front of its arg.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
967 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
968
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
969 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
970 (symbol-function 'add-foo)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
971 @result{} (lambda (x) (nconc (quote (foo)) x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
972 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
973
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
974 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
975 (setq xx (add-foo '(1 2))) ; @r{It seems to work.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
976 @result{} (foo 1 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
977 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
978 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
979 (setq xy (add-foo '(3 4))) ; @r{What happened?}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
980 @result{} (foo 1 2 3 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
981 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
982 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
983 (eq xx xy)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
984 @result{} t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
985 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
986
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
987 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
988 (symbol-function 'add-foo)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
989 @result{} (lambda (x) (nconc (quote (foo 1 2 3 4) x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
990 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
991 @end smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
992 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
993
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
994 @defun nreverse list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
995 @cindex reversing a list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
996 This function reverses the order of the elements of @var{list}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
997 Unlike @code{reverse}, @code{nreverse} alters its argument by reversing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
998 the @sc{cdr}s in the cons cells forming the list. The cons cell that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
999 used to be the last one in @var{list} becomes the first cell of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1000 value.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1001
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1002 For example:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1003
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1004 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1005 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1006 (setq x '(1 2 3 4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1007 @result{} (1 2 3 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1008 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1009 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1010 x
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1011 @result{} (1 2 3 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1012 (nreverse x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1013 @result{} (4 3 2 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1014 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1015 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1016 ;; @r{The cell that was first is now last.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1017 x
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1018 @result{} (1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1019 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1020 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1021
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1022 To avoid confusion, we usually store the result of @code{nreverse}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1023 back in the same variable which held the original list:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1024
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1025 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1026 (setq x (nreverse x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1027 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1028
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1029 Here is the @code{nreverse} of our favorite example, @code{(a b c)},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1030 presented graphically:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1031
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1032 @smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1033 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1034 @r{Original list head:} @r{Reversed list:}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1035 ------------- ------------- ------------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1036 | car | cdr | | car | cdr | | car | cdr |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1037 | a | nil |<-- | b | o |<-- | c | o |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1038 | | | | | | | | | | | | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1039 ------------- | --------- | - | -------- | -
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1040 | | | |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1041 ------------- ------------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1042 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1043 @end smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1044 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1045
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1046 @defun sort list predicate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1047 @cindex stable sort
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1048 @cindex sorting lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1049 This function sorts @var{list} stably, though destructively, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1050 returns the sorted list. It compares elements using @var{predicate}. A
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1051 stable sort is one in which elements with equal sort keys maintain their
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1052 relative order before and after the sort. Stability is important when
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1053 successive sorts are used to order elements according to different
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1054 criteria.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1055
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1056 The argument @var{predicate} must be a function that accepts two
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1057 arguments. It is called with two elements of @var{list}. To get an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1058 increasing order sort, the @var{predicate} should return @code{t} if the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1059 first element is ``less than'' the second, or @code{nil} if not.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1060
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1061 The destructive aspect of @code{sort} is that it rearranges the cons
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1062 cells forming @var{list} by changing @sc{cdr}s. A nondestructive sort
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1063 function would create new cons cells to store the elements in their
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1064 sorted order. If you wish to make a sorted copy without destroying the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1065 original, copy it first with @code{copy-sequence} and then sort.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1066
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1067 Sorting does not change the @sc{car}s of the cons cells in @var{list};
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1068 the cons cell that originally contained the element @code{a} in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1069 @var{list} still has @code{a} in its @sc{car} after sorting, but it now
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1070 appears in a different position in the list due to the change of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1071 @sc{cdr}s. For example:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1072
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1073 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1074 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1075 (setq nums '(1 3 2 6 5 4 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1076 @result{} (1 3 2 6 5 4 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1077 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1078 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1079 (sort nums '<)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1080 @result{} (0 1 2 3 4 5 6)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1081 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1082 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1083 nums
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1084 @result{} (1 2 3 4 5 6)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1085 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1086 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1087
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1088 @noindent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1089 Note that the list in @code{nums} no longer contains 0; this is the same
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1090 cons cell that it was before, but it is no longer the first one in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1091 list. Don't assume a variable that formerly held the argument now holds
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1092 the entire sorted list! Instead, save the result of @code{sort} and use
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1093 that. Most often we store the result back into the variable that held
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1094 the original list:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1095
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1096 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1097 (setq nums (sort nums '<))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1098 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1099
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1100 @xref{Sorting}, for more functions that perform sorting.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1101 See @code{documentation} in @ref{Accessing Documentation}, for a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1102 useful example of @code{sort}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1103 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1104
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1105 @node Sets And Lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1106 @section Using Lists as Sets
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1107 @cindex lists as sets
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1108 @cindex sets
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1109
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1110 A list can represent an unordered mathematical set---simply consider a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1111 value an element of a set if it appears in the list, and ignore the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1112 order of the list. To form the union of two sets, use @code{append} (as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1113 long as you don't mind having duplicate elements). Other useful
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1114 functions for sets include @code{memq} and @code{delq}, and their
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1115 @code{equal} versions, @code{member} and @code{delete}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1116
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1117 @cindex CL note---lack @code{union}, @code{set}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1118 @quotation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1119 @b{Common Lisp note:} Common Lisp has functions @code{union} (which
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1120 avoids duplicate elements) and @code{intersection} for set operations,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1121 but XEmacs Lisp does not have them. You can write them in Lisp if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1122 you wish.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1123 @end quotation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1124
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1125 @defun memq object list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1126 @cindex membership in a list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1127 This function tests to see whether @var{object} is a member of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1128 @var{list}. If it is, @code{memq} returns a list starting with the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1129 first occurrence of @var{object}. Otherwise, it returns @code{nil}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1130 The letter @samp{q} in @code{memq} says that it uses @code{eq} to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1131 compare @var{object} against the elements of the list. For example:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1132
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1133 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1134 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1135 (memq 'b '(a b c b a))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1136 @result{} (b c b a)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1137 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1138 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1139 (memq '(2) '((1) (2))) ; @r{@code{(2)} and @code{(2)} are not @code{eq}.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1140 @result{} nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1141 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1142 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1143 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1144
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1145 @defun delq object list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1146 @cindex deletion of elements
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1147 This function destructively removes all elements @code{eq} to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1148 @var{object} from @var{list}. The letter @samp{q} in @code{delq} says
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1149 that it uses @code{eq} to compare @var{object} against the elements of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1150 the list, like @code{memq}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1151 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1152
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1153 When @code{delq} deletes elements from the front of the list, it does so
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1154 simply by advancing down the list and returning a sublist that starts
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1155 after those elements:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1156
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1157 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1158 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1159 (delq 'a '(a b c)) @equiv{} (cdr '(a b c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1160 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1161 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1162
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1163 When an element to be deleted appears in the middle of the list,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1164 removing it involves changing the @sc{cdr}s (@pxref{Setcdr}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1165
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1166 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1167 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1168 (setq sample-list '(a b c (4)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1169 @result{} (a b c (4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1170 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1171 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1172 (delq 'a sample-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1173 @result{} (b c (4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1174 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1175 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1176 sample-list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1177 @result{} (a b c (4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1178 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1179 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1180 (delq 'c sample-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1181 @result{} (a b (4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1182 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1183 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1184 sample-list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1185 @result{} (a b (4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1186 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1187 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1188
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1189 Note that @code{(delq 'c sample-list)} modifies @code{sample-list} to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1190 splice out the third element, but @code{(delq 'a sample-list)} does not
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1191 splice anything---it just returns a shorter list. Don't assume that a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1192 variable which formerly held the argument @var{list} now has fewer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1193 elements, or that it still holds the original list! Instead, save the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1194 result of @code{delq} and use that. Most often we store the result back
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1195 into the variable that held the original list:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1196
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1197 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1198 (setq flowers (delq 'rose flowers))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1199 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1200
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1201 In the following example, the @code{(4)} that @code{delq} attempts to match
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1202 and the @code{(4)} in the @code{sample-list} are not @code{eq}:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1203
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1204 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1205 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1206 (delq '(4) sample-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1207 @result{} (a c (4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1208 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1209 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1210
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1211 The following two functions are like @code{memq} and @code{delq} but use
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1212 @code{equal} rather than @code{eq} to compare elements. They are new in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1213 Emacs 19.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1214
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1215 @defun member object list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1216 The function @code{member} tests to see whether @var{object} is a member
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1217 of @var{list}, comparing members with @var{object} using @code{equal}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1218 If @var{object} is a member, @code{member} returns a list starting with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1219 its first occurrence in @var{list}. Otherwise, it returns @code{nil}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1220
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1221 Compare this with @code{memq}:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1222
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1223 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1224 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1225 (member '(2) '((1) (2))) ; @r{@code{(2)} and @code{(2)} are @code{equal}.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1226 @result{} ((2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1227 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1228 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1229 (memq '(2) '((1) (2))) ; @r{@code{(2)} and @code{(2)} are not @code{eq}.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1230 @result{} nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1231 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1232 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1233 ;; @r{Two strings with the same contents are @code{equal}.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1234 (member "foo" '("foo" "bar"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1235 @result{} ("foo" "bar")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1236 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1237 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1238 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1239
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1240 @defun delete object list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1241 This function destructively removes all elements @code{equal} to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1242 @var{object} from @var{list}. It is to @code{delq} as @code{member} is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1243 to @code{memq}: it uses @code{equal} to compare elements with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1244 @var{object}, like @code{member}; when it finds an element that matches,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1245 it removes the element just as @code{delq} would. For example:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1246
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1247 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1248 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1249 (delete '(2) '((2) (1) (2)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1250 @result{} '((1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1251 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1252 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1253 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1254
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1255 @quotation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1256 @b{Common Lisp note:} The functions @code{member} and @code{delete} in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1257 XEmacs Lisp are derived from Maclisp, not Common Lisp. The Common
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1258 Lisp versions do not use @code{equal} to compare elements.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1259 @end quotation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1260
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1261 See also the function @code{add-to-list}, in @ref{Setting Variables},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1262 for another way to add an element to a list stored in a variable.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1263
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1264 @node Association Lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1265 @section Association Lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1266 @cindex association list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1267 @cindex alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1268
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1269 An @dfn{association list}, or @dfn{alist} for short, records a mapping
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1270 from keys to values. It is a list of cons cells called
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1271 @dfn{associations}: the @sc{car} of each cell is the @dfn{key}, and the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1272 @sc{cdr} is the @dfn{associated value}.@footnote{This usage of ``key''
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1273 is not related to the term ``key sequence''; it means a value used to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1274 look up an item in a table. In this case, the table is the alist, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1275 the alist associations are the items.}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1276
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1277 Here is an example of an alist. The key @code{pine} is associated with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1278 the value @code{cones}; the key @code{oak} is associated with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1279 @code{acorns}; and the key @code{maple} is associated with @code{seeds}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1280
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1281 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1282 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1283 '((pine . cones)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1284 (oak . acorns)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1285 (maple . seeds))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1286 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1287 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1288
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1289 The associated values in an alist may be any Lisp objects; so may the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1290 keys. For example, in the following alist, the symbol @code{a} is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1291 associated with the number @code{1}, and the string @code{"b"} is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1292 associated with the @emph{list} @code{(2 3)}, which is the @sc{cdr} of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1293 the alist element:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1294
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1295 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1296 ((a . 1) ("b" 2 3))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1297 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1298
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1299 Sometimes it is better to design an alist to store the associated
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1300 value in the @sc{car} of the @sc{cdr} of the element. Here is an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1301 example:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1302
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1303 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1304 '((rose red) (lily white) (buttercup yellow))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1305 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1306
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1307 @noindent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1308 Here we regard @code{red} as the value associated with @code{rose}. One
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1309 advantage of this method is that you can store other related
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1310 information---even a list of other items---in the @sc{cdr} of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1311 @sc{cdr}. One disadvantage is that you cannot use @code{rassq} (see
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1312 below) to find the element containing a given value. When neither of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1313 these considerations is important, the choice is a matter of taste, as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1314 long as you are consistent about it for any given alist.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1315
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1316 Note that the same alist shown above could be regarded as having the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1317 associated value in the @sc{cdr} of the element; the value associated
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1318 with @code{rose} would be the list @code{(red)}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1319
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1320 Association lists are often used to record information that you might
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1321 otherwise keep on a stack, since new associations may be added easily to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1322 the front of the list. When searching an association list for an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1323 association with a given key, the first one found is returned, if there
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1324 is more than one.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1325
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1326 In XEmacs Lisp, it is @emph{not} an error if an element of an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1327 association list is not a cons cell. The alist search functions simply
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1328 ignore such elements. Many other versions of Lisp signal errors in such
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1329 cases.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1330
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1331 Note that property lists are similar to association lists in several
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1332 respects. A property list behaves like an association list in which
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1333 each key can occur only once. @xref{Property Lists}, for a comparison
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1334 of property lists and association lists.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1335
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1336 @defun assoc key alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1337 This function returns the first association for @var{key} in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1338 @var{alist}. It compares @var{key} against the alist elements using
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1339 @code{equal} (@pxref{Equality Predicates}). It returns @code{nil} if no
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1340 association in @var{alist} has a @sc{car} @code{equal} to @var{key}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1341 For example:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1342
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1343 @smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1344 (setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1345 @result{} ((pine . cones) (oak . acorns) (maple . seeds))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1346 (assoc 'oak trees)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1347 @result{} (oak . acorns)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1348 (cdr (assoc 'oak trees))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1349 @result{} acorns
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1350 (assoc 'birch trees)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1351 @result{} nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1352 @end smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1353
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1354 Here is another example, in which the keys and values are not symbols:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1355
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1356 @smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1357 (setq needles-per-cluster
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1358 '((2 "Austrian Pine" "Red Pine")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1359 (3 "Pitch Pine")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1360 (5 "White Pine")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1361
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1362 (cdr (assoc 3 needles-per-cluster))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1363 @result{} ("Pitch Pine")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1364 (cdr (assoc 2 needles-per-cluster))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1365 @result{} ("Austrian Pine" "Red Pine")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1366 @end smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1367 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1368
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1369 @defun rassoc value alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1370 This function returns the first association with value @var{value} in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1371 @var{alist}. It returns @code{nil} if no association in @var{alist} has
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1372 a @sc{cdr} @code{equal} to @var{value}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1373
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1374 @code{rassoc} is like @code{assoc} except that it compares the @sc{cdr} of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1375 each @var{alist} association instead of the @sc{car}. You can think of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1376 this as ``reverse @code{assoc}'', finding the key for a given value.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1377 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1378
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1379 @defun assq key alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1380 This function is like @code{assoc} in that it returns the first
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1381 association for @var{key} in @var{alist}, but it makes the comparison
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1382 using @code{eq} instead of @code{equal}. @code{assq} returns @code{nil}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1383 if no association in @var{alist} has a @sc{car} @code{eq} to @var{key}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1384 This function is used more often than @code{assoc}, since @code{eq} is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1385 faster than @code{equal} and most alists use symbols as keys.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1386 @xref{Equality Predicates}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1387
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1388 @smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1389 (setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1390 @result{} ((pine . cones) (oak . acorns) (maple . seeds))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1391 (assq 'pine trees)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1392 @result{} (pine . cones)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1393 @end smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1394
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1395 On the other hand, @code{assq} is not usually useful in alists where the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1396 keys may not be symbols:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1397
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1398 @smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1399 (setq leaves
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1400 '(("simple leaves" . oak)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1401 ("compound leaves" . horsechestnut)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1402
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1403 (assq "simple leaves" leaves)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1404 @result{} nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1405 (assoc "simple leaves" leaves)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1406 @result{} ("simple leaves" . oak)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1407 @end smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1408 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1409
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1410 @defun rassq value alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1411 This function returns the first association with value @var{value} in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1412 @var{alist}. It returns @code{nil} if no association in @var{alist} has
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1413 a @sc{cdr} @code{eq} to @var{value}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1414
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1415 @code{rassq} is like @code{assq} except that it compares the @sc{cdr} of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1416 each @var{alist} association instead of the @sc{car}. You can think of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1417 this as ``reverse @code{assq}'', finding the key for a given value.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1418
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1419 For example:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1420
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1421 @smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1422 (setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1423
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1424 (rassq 'acorns trees)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1425 @result{} (oak . acorns)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1426 (rassq 'spores trees)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1427 @result{} nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1428 @end smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1429
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1430 Note that @code{rassq} cannot search for a value stored in the @sc{car}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1431 of the @sc{cdr} of an element:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1432
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1433 @smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1434 (setq colors '((rose red) (lily white) (buttercup yellow)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1435
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1436 (rassq 'white colors)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1437 @result{} nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1438 @end smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1439
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1440 In this case, the @sc{cdr} of the association @code{(lily white)} is not
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1441 the symbol @code{white}, but rather the list @code{(white)}. This
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1442 becomes clearer if the association is written in dotted pair notation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1443
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1444 @smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1445 (lily white) @equiv{} (lily . (white))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1446 @end smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1447 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1448
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1449 @defun remassoc key alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1450 This function deletes by side effect any associations with key @var{key}
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 398
diff changeset
1451 in @var{alist} -- i.e. it removes any elements from @var{alist} whose
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1452 @code{car} is @code{equal} to @var{key}. The modified @var{alist} is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1453 returned.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1454
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1455 If the first member of @var{alist} has a @code{car} that is @code{equal}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1456 to @var{key}, there is no way to remove it by side effect; therefore,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1457 write @code{(setq foo (remassoc key foo))} to be sure of changing the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1458 value of @code{foo}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1459 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1460
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1461 @defun remassq key alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1462 This function deletes by side effect any associations with key @var{key}
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 398
diff changeset
1463 in @var{alist} -- i.e. it removes any elements from @var{alist} whose
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1464 @code{car} is @code{eq} to @var{key}. The modified @var{alist} is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1465 returned.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1466
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1467 This function is exactly like @code{remassoc}, but comparisons between
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1468 @var{key} and keys in @var{alist} are done using @code{eq} instead of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1469 @code{equal}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1470 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1471
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1472 @defun remrassoc value alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1473 This function deletes by side effect any associations with value @var{value}
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 398
diff changeset
1474 in @var{alist} -- i.e. it removes any elements from @var{alist} whose
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1475 @code{cdr} is @code{equal} to @var{value}. The modified @var{alist} is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1476 returned.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1477
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1478 If the first member of @var{alist} has a @code{car} that is @code{equal}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1479 to @var{value}, there is no way to remove it by side effect; therefore,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1480 write @code{(setq foo (remassoc value foo))} to be sure of changing the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1481 value of @code{foo}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1482
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1483 @code{remrassoc} is like @code{remassoc} except that it compares the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1484 @sc{cdr} of each @var{alist} association instead of the @sc{car}. You
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1485 can think of this as ``reverse @code{remassoc}'', removing an association
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1486 based on its value instead of its key.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1487 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1488
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1489 @defun remrassq value alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1490 This function deletes by side effect any associations with value @var{value}
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 398
diff changeset
1491 in @var{alist} -- i.e. it removes any elements from @var{alist} whose
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1492 @code{cdr} is @code{eq} to @var{value}. The modified @var{alist} is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1493 returned.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1494
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1495 This function is exactly like @code{remrassoc}, but comparisons between
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1496 @var{value} and values in @var{alist} are done using @code{eq} instead of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1497 @code{equal}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1498 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1499
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1500 @defun copy-alist alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1501 @cindex copying alists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1502 This function returns a two-level deep copy of @var{alist}: it creates a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1503 new copy of each association, so that you can alter the associations of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1504 the new alist without changing the old one.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1505
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1506 @smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1507 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1508 (setq needles-per-cluster
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1509 '((2 . ("Austrian Pine" "Red Pine"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1510 (3 . ("Pitch Pine"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1511 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1512 (5 . ("White Pine"))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1513 @result{}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1514 ((2 "Austrian Pine" "Red Pine")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1515 (3 "Pitch Pine")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1516 (5 "White Pine"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1517
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1518 (setq copy (copy-alist needles-per-cluster))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1519 @result{}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1520 ((2 "Austrian Pine" "Red Pine")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1521 (3 "Pitch Pine")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1522 (5 "White Pine"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1523
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1524 (eq needles-per-cluster copy)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1525 @result{} nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1526 (equal needles-per-cluster copy)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1527 @result{} t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1528 (eq (car needles-per-cluster) (car copy))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1529 @result{} nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1530 (cdr (car (cdr needles-per-cluster)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1531 @result{} ("Pitch Pine")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1532 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1533 (eq (cdr (car (cdr needles-per-cluster)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1534 (cdr (car (cdr copy))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1535 @result{} t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1536 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1537 @end smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1538
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1539 This example shows how @code{copy-alist} makes it possible to change
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1540 the associations of one copy without affecting the other:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1541
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1542 @smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1543 @group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1544 (setcdr (assq 3 copy) '("Martian Vacuum Pine"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1545 (cdr (assq 3 needles-per-cluster))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1546 @result{} ("Pitch Pine")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1547 @end group
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1548 @end smallexample
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1549 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1550
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1551 @node Property Lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1552 @section Property Lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1553 @cindex property list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1554 @cindex plist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1555
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1556 A @dfn{property list} (or @dfn{plist}) is another way of representing a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1557 mapping from keys to values. Instead of the list consisting of conses
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1558 of a key and a value, the keys and values alternate as successive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1559 entries in the list. Thus, the association list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1560
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1561 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1562 ((a . 1) (b . 2) (c . 3))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1563 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1564
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1565 has the equivalent property list form
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1566
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1567 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1568 (a 1 b 2 c 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1569 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1570
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1571 Property lists are used to represent the properties associated with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1572 various sorts of objects, such as symbols, strings, frames, etc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1573 The convention is that property lists can be modified in-place,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1574 while association lists generally are not.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1575
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1576 Plists come in two varieties: @dfn{normal} plists, whose keys are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1577 compared with @code{eq}, and @dfn{lax} plists, whose keys are compared
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1578 with @code{equal},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1579
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1580 @defun valid-plist-p plist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1581 Given a plist, this function returns non-@code{nil} if its format is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1582 correct. If it returns @code{nil}, @code{check-valid-plist} will signal
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1583 an error when given the plist; that means it's a malformed or circular
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1584 plist or has non-symbols as keywords.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1585 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1586
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1587 @defun check-valid-plist plist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1588 Given a plist, this function signals an error if there is anything wrong
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1589 with it. This means that it's a malformed or circular plist.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1590 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1591
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1592 @menu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1593 * Working With Normal Plists:: Functions for normal plists.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1594 * Working With Lax Plists:: Functions for lax plists.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1595 * Converting Plists To/From Alists:: Alist to plist and vice-versa.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1596 @end menu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1597
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1598 @node Working With Normal Plists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1599 @subsection Working With Normal Plists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1600
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1601 @defun plist-get plist prop &optional default
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1602 This function extracts a value from a property list. The function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1603 returns the value corresponding to the given @var{prop}, or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1604 @var{default} if @var{prop} is not one of the properties on the list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1605 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1606
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1607 @defun plist-put plist prop val
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1608 This function changes the value in @var{plist} of @var{prop} to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1609 @var{val}. If @var{prop} is already a property on the list, its value is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1610 set to @var{val}, otherwise the new @var{prop} @var{val} pair is added.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1611 The new plist is returned; use @code{(setq x (plist-put x prop val))} to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1612 be sure to use the new value. The @var{plist} is modified by side
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1613 effects.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1614 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1615
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1616 @defun plist-remprop plist prop
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1617 This function removes from @var{plist} the property @var{prop} and its
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1618 value. The new plist is returned; use @code{(setq x (plist-remprop x
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1619 prop val))} to be sure to use the new value. The @var{plist} is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1620 modified by side effects.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1621 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1622
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1623 @defun plist-member plist prop
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1624 This function returns @code{t} if @var{prop} has a value specified in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1625 @var{plist}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1626 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1627
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1628 In the following functions, if optional arg @var{nil-means-not-present}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1629 is non-@code{nil}, then a property with a @code{nil} value is ignored or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1630 removed. This feature is a virus that has infected old Lisp
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 398
diff changeset
1631 implementations (and thus E-Lisp, due to @sc{RMS}'s enamorment with old
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1632 Lisps), but should not be used except for backward compatibility.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1633
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1634 @defun plists-eq a b &optional nil-means-not-present
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1635 This function returns non-@code{nil} if property lists A and B are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1636 @code{eq} (i.e. their values are @code{eq}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1637 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1638
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1639 @defun plists-equal a b &optional nil-means-not-present
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1640 This function returns non-@code{nil} if property lists A and B are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1641 @code{equal} (i.e. their values are @code{equal}; their keys are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1642 still compared using @code{eq}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1643 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1644
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1645 @defun canonicalize-plist plist &optional nil-means-not-present
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1646 This function destructively removes any duplicate entries from a plist.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1647 In such cases, the first entry applies.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1648
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1649 The new plist is returned. If @var{nil-means-not-present} is given, the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1650 return value may not be @code{eq} to the passed-in value, so make sure
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1651 to @code{setq} the value back into where it came from.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1652 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1653
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1654 @node Working With Lax Plists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1655 @subsection Working With Lax Plists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1656
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1657 Recall that a @dfn{lax plist} is a property list whose keys are compared
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1658 using @code{equal} instead of @code{eq}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1659
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1660 @defun lax-plist-get lax-plist prop &optional default
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1661 This function extracts a value from a lax property list. The function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1662 returns the value corresponding to the given @var{prop}, or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1663 @var{default} if @var{prop} is not one of the properties on the list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1664 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1665
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1666 @defun lax-plist-put lax-plist prop val
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1667 This function changes the value in @var{lax-plist} of @var{prop} to @var{val}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1668 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1669
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1670 @defun lax-plist-remprop lax-plist prop
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1671 This function removes from @var{lax-plist} the property @var{prop} and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1672 its value. The new plist is returned; use @code{(setq x
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1673 (lax-plist-remprop x prop val))} to be sure to use the new value. The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1674 @var{lax-plist} is modified by side effects.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1675 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1676
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1677 @defun lax-plist-member lax-plist prop
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1678 This function returns @code{t} if @var{prop} has a value specified in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1679 @var{lax-plist}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1680 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1681
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1682 In the following functions, if optional arg @var{nil-means-not-present}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1683 is non-@code{nil}, then a property with a @code{nil} value is ignored or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1684 removed. This feature is a virus that has infected old Lisp
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 398
diff changeset
1685 implementations (and thus E-Lisp, due to @sc{RMS}'s enamorment with old
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1686 Lisps), but should not be used except for backward compatibility.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1687
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1688 @defun lax-plists-eq a b &optional nil-means-not-present
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1689 This function returns non-@code{nil} if lax property lists A and B are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1690 @code{eq} (i.e. their values are @code{eq}; their keys are still
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1691 compared using @code{equal}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1692 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1693
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1694 @defun lax-plists-equal a b &optional nil-means-not-present
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1695 This function returns non-@code{nil} if lax property lists A and B are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1696 @code{equal} (i.e. their values are @code{equal}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1697 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1698
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1699 @defun canonicalize-lax-plist lax-plist &optional nil-means-not-present
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1700 This function destructively removes any duplicate entries from a lax
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1701 plist. In such cases, the first entry applies.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1702
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1703 The new plist is returned. If @var{nil-means-not-present} is given, the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1704 return value may not be @code{eq} to the passed-in value, so make sure
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1705 to @code{setq} the value back into where it came from.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1706 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1707
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1708 @node Converting Plists To/From Alists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1709 @subsection Converting Plists To/From Alists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1710
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1711 @defun alist-to-plist alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1712 This function converts association list @var{alist} into the equivalent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1713 property-list form. The plist is returned. This converts from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1714
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1715 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1716 ((a . 1) (b . 2) (c . 3))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1717 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1718
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1719 into
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1720
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1721 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1722 (a 1 b 2 c 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1723 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1724
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1725 The original alist is not modified.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1726 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1727
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1728 @defun plist-to-alist plist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1729 This function converts property list @var{plist} into the equivalent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1730 association-list form. The alist is returned. This converts from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1731
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1732 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1733 (a 1 b 2 c 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1734 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1735
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1736 into
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1737
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1738 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1739 ((a . 1) (b . 2) (c . 3))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1740 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1741
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1742 The original plist is not modified.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1743 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1744
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1745 The following two functions are equivalent to the preceding two except
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1746 that they destructively modify their arguments, using cons cells from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1747 the original list to form the new list rather than allocating new
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1748 cons cells.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1749
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1750 @defun destructive-alist-to-plist alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1751 This function destructively converts association list @var{alist} into
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1752 the equivalent property-list form. The plist is returned.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1753 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1754
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1755 @defun destructive-plist-to-alist plist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1756 This function destructively converts property list @var{plist} into the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1757 equivalent association-list form. The alist is returned.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1758 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1759
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1760 @node Weak Lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1761 @section Weak Lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1762 @cindex weak list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1763
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1764 A @dfn{weak list} is a special sort of list whose members are not counted
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1765 as references for the purpose of garbage collection. This means that,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1766 for any object in the list, if there are no references to the object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1767 anywhere outside of the list (or other weak list or weak hash table),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1768 that object will disappear the next time a garbage collection happens.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1769 Weak lists can be useful for keeping track of things such as unobtrusive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1770 lists of another function's buffers or markers. When that function is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1771 done with the elements, they will automatically disappear from the list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1772
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1773 Weak lists are used internally, for example, to manage the list holding
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 398
diff changeset
1774 the children of an extent -- an extent that is unused but has a parent
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1775 will still be reclaimed, and will automatically be removed from its
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1776 parent's list of children.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1777
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1778 Weak lists are similar to weak hash tables (@pxref{Weak Hash Tables}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1779
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1780 @defun weak-list-p object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1781 This function returns non-@code{nil} if @var{object} is a weak list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1782 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1783
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1784 Weak lists come in one of four types:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1785
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1786 @table @code
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1787 @item simple
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1788 Objects in the list disappear if not referenced outside of the list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1789
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1790 @item assoc
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1791 Objects in the list disappear if they are conses and either the car or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1792 the cdr of the cons is not referenced outside of the list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1793
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1794 @item key-assoc
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1795 Objects in the list disappear if they are conses and the car is not
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1796 referenced outside of the list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1797
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1798 @item value-assoc
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1799 Objects in the list disappear if they are conses and the cdr is not
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1800 referenced outside of the list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1801 @end table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1802
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1803 @defun make-weak-list &optional type
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1804 This function creates a new weak list of type @var{type}. @var{type} is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1805 a symbol (one of @code{simple}, @code{assoc}, @code{key-assoc}, or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1806 @code{value-assoc}, as described above) and defaults to @code{simple}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1807 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1808
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1809 @defun weak-list-type weak
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1810 This function returns the type of the given weak-list object.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1811 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1812
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1813 @defun weak-list-list weak
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1814 This function returns the list contained in a weak-list object.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1815 @end defun
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1816
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1817 @defun set-weak-list-list weak new-list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1818 This function changes the list contained in a weak-list object.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1819 @end defun