Mercurial > emacs
annotate lispref/lists.texi @ 21099:440d5927ef18
(news-exit): Use quit-window.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 08 Mar 1998 00:23:16 +0000 |
parents | 66d807bdc5b4 |
children | 90da2489c498 |
rev | line source |
---|---|
6558 | 1 @c -*-texinfo-*- |
2 @c This is part of the GNU Emacs Lisp Reference Manual. | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc. |
6558 | 4 @c See the file elisp.texi for copying conditions. |
5 @setfilename ../info/lists | |
6 @node Lists, Sequences Arrays Vectors, Strings and Characters, Top | |
7 @chapter Lists | |
8 @cindex list | |
9 @cindex element (of list) | |
10 | |
11 A @dfn{list} represents a sequence of zero or more elements (which may | |
12 be any Lisp objects). The important difference between lists and | |
13 vectors is that two or more lists can share part of their structure; in | |
14 addition, you can insert or delete elements in a list without copying | |
15 the whole list. | |
16 | |
17 @menu | |
18 * Cons Cells:: How lists are made out of cons cells. | |
19 * Lists as Boxes:: Graphical notation to explain lists. | |
20 * List-related Predicates:: Is this object a list? Comparing two lists. | |
21 * List Elements:: Extracting the pieces of a list. | |
22 * Building Lists:: Creating list structure. | |
23 * Modifying Lists:: Storing new pieces into an existing list. | |
24 * Sets And Lists:: A list can represent a finite mathematical set. | |
25 * Association Lists:: A list can represent a finite relation or mapping. | |
26 @end menu | |
27 | |
28 @node Cons Cells | |
29 @section Lists and Cons Cells | |
30 @cindex lists and cons cells | |
31 @cindex @code{nil} and lists | |
32 | |
33 Lists in Lisp are not a primitive data type; they are built up from | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
34 @dfn{cons cells}. A cons cell is a data object that represents an |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
35 ordered pair. It records two Lisp objects, one labeled as the @sc{car}, |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
36 and the other labeled as the @sc{cdr}. These names are traditional; see |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
37 @ref{Cons Cell Type}. @sc{cdr} is pronounced ``could-er.'' |
6558 | 38 |
39 A list is a series of cons cells chained together, one cons cell per | |
40 element of the list. By convention, the @sc{car}s of the cons cells are | |
41 the elements of the list, and the @sc{cdr}s are used to chain the list: | |
42 the @sc{cdr} of each cons cell is the following cons cell. The @sc{cdr} | |
43 of the last cons cell is @code{nil}. This asymmetry between the | |
44 @sc{car} and the @sc{cdr} is entirely a matter of convention; at the | |
45 level of cons cells, the @sc{car} and @sc{cdr} slots have the same | |
46 characteristics. | |
47 | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
48 @cindex list structure |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
49 Because most cons cells are used as part of lists, the phrase |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
50 @dfn{list structure} has come to mean any structure made out of cons |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
51 cells. |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
52 |
6558 | 53 The symbol @code{nil} is considered a list as well as a symbol; it is |
54 the list with no elements. For convenience, the symbol @code{nil} is | |
55 considered to have @code{nil} as its @sc{cdr} (and also as its | |
56 @sc{car}). | |
57 | |
58 The @sc{cdr} of any nonempty list @var{l} is a list containing all the | |
59 elements of @var{l} except the first. | |
60 | |
61 @node Lists as Boxes | |
62 @comment node-name, next, previous, up | |
63 @section Lists as Linked Pairs of Boxes | |
64 @cindex box representation for lists | |
65 @cindex lists represented as boxes | |
66 @cindex cons cell as box | |
67 | |
68 A cons cell can be illustrated as a pair of boxes. The first box | |
69 represents the @sc{car} and the second box represents the @sc{cdr}. | |
70 Here is an illustration of the two-element list, @code{(tulip lily)}, | |
71 made from two cons cells: | |
72 | |
73 @example | |
74 @group | |
75 --------------- --------------- | |
76 | car | cdr | | car | cdr | | |
77 | tulip | o---------->| lily | nil | | |
78 | | | | | | | |
79 --------------- --------------- | |
80 @end group | |
81 @end example | |
82 | |
83 Each pair of boxes represents a cons cell. Each box ``refers to'', | |
84 ``points to'' or ``contains'' a Lisp object. (These terms are | |
85 synonymous.) The first box, which is the @sc{car} of the first cons | |
86 cell, contains the symbol @code{tulip}. The arrow from the @sc{cdr} of | |
87 the first cons cell to the second cons cell indicates that the @sc{cdr} | |
88 of the first cons cell points to the second cons cell. | |
89 | |
90 The same list can be illustrated in a different sort of box notation | |
91 like this: | |
92 | |
93 @example | |
94 @group | |
95 ___ ___ ___ ___ | |
96 |___|___|--> |___|___|--> nil | |
97 | | | |
98 | | | |
99 --> tulip --> lily | |
100 @end group | |
101 @end example | |
102 | |
103 Here is a more complex illustration, showing the three-element list, | |
104 @code{((pine needles) oak maple)}, the first element of which is a | |
105 two-element list: | |
106 | |
107 @example | |
108 @group | |
109 ___ ___ ___ ___ ___ ___ | |
110 |___|___|--> |___|___|--> |___|___|--> nil | |
111 | | | | |
112 | | | | |
113 | --> oak --> maple | |
114 | | |
115 | ___ ___ ___ ___ | |
116 --> |___|___|--> |___|___|--> nil | |
117 | | | |
118 | | | |
119 --> pine --> needles | |
120 @end group | |
121 @end example | |
122 | |
123 The same list represented in the first box notation looks like this: | |
124 | |
125 @example | |
126 @group | |
127 -------------- -------------- -------------- | |
128 | car | cdr | | car | cdr | | car | cdr | | |
129 | o | o------->| oak | o------->| maple | nil | | |
130 | | | | | | | | | | | |
131 -- | --------- -------------- -------------- | |
132 | | |
133 | | |
134 | -------------- ---------------- | |
135 | | car | cdr | | car | cdr | | |
136 ------>| pine | o------->| needles | nil | | |
137 | | | | | | | |
138 -------------- ---------------- | |
139 @end group | |
140 @end example | |
141 | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
142 @xref{Cons Cell Type}, for the read and print syntax of cons cells and |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
143 lists, and for more ``box and arrow'' illustrations of lists. |
6558 | 144 |
145 @node List-related Predicates | |
146 @section Predicates on Lists | |
147 | |
148 The following predicates test whether a Lisp object is an atom, is a | |
149 cons cell or is a list, or whether it is the distinguished object | |
150 @code{nil}. (Many of these predicates can be defined in terms of the | |
151 others, but they are used so often that it is worth having all of them.) | |
152 | |
153 @defun consp object | |
154 This function returns @code{t} if @var{object} is a cons cell, @code{nil} | |
155 otherwise. @code{nil} is not a cons cell, although it @emph{is} a list. | |
156 @end defun | |
157 | |
158 @defun atom object | |
159 @cindex atoms | |
160 This function returns @code{t} if @var{object} is an atom, @code{nil} | |
161 otherwise. All objects except cons cells are atoms. The symbol | |
162 @code{nil} is an atom and is also a list; it is the only Lisp object | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
163 that is both. |
6558 | 164 |
165 @example | |
166 (atom @var{object}) @equiv{} (not (consp @var{object})) | |
167 @end example | |
168 @end defun | |
169 | |
170 @defun listp object | |
171 This function returns @code{t} if @var{object} is a cons cell or | |
172 @code{nil}. Otherwise, it returns @code{nil}. | |
173 | |
174 @example | |
175 @group | |
176 (listp '(1)) | |
177 @result{} t | |
178 @end group | |
179 @group | |
180 (listp '()) | |
181 @result{} t | |
182 @end group | |
183 @end example | |
184 @end defun | |
185 | |
186 @defun nlistp object | |
187 This function is the opposite of @code{listp}: it returns @code{t} if | |
188 @var{object} is not a list. Otherwise, it returns @code{nil}. | |
189 | |
190 @example | |
191 (listp @var{object}) @equiv{} (not (nlistp @var{object})) | |
192 @end example | |
193 @end defun | |
194 | |
195 @defun null object | |
196 This function returns @code{t} if @var{object} is @code{nil}, and | |
197 returns @code{nil} otherwise. This function is identical to @code{not}, | |
198 but as a matter of clarity we use @code{null} when @var{object} is | |
199 considered a list and @code{not} when it is considered a truth value | |
200 (see @code{not} in @ref{Combining Conditions}). | |
201 | |
202 @example | |
203 @group | |
204 (null '(1)) | |
205 @result{} nil | |
206 @end group | |
207 @group | |
208 (null '()) | |
209 @result{} t | |
210 @end group | |
211 @end example | |
212 @end defun | |
213 | |
7734 | 214 @need 2000 |
6558 | 215 |
216 @node List Elements | |
217 @section Accessing Elements of Lists | |
218 @cindex list elements | |
219 | |
220 @defun car cons-cell | |
221 This function returns the value pointed to by the first pointer of the | |
222 cons cell @var{cons-cell}. Expressed another way, this function | |
223 returns the @sc{car} of @var{cons-cell}. | |
224 | |
225 As a special case, if @var{cons-cell} is @code{nil}, then @code{car} | |
226 is defined to return @code{nil}; therefore, any list is a valid argument | |
227 for @code{car}. An error is signaled if the argument is not a cons cell | |
228 or @code{nil}. | |
229 | |
230 @example | |
231 @group | |
232 (car '(a b c)) | |
233 @result{} a | |
234 @end group | |
235 @group | |
236 (car '()) | |
237 @result{} nil | |
238 @end group | |
239 @end example | |
240 @end defun | |
241 | |
242 @defun cdr cons-cell | |
243 This function returns the value pointed to by the second pointer of | |
244 the cons cell @var{cons-cell}. Expressed another way, this function | |
245 returns the @sc{cdr} of @var{cons-cell}. | |
246 | |
247 As a special case, if @var{cons-cell} is @code{nil}, then @code{cdr} | |
248 is defined to return @code{nil}; therefore, any list is a valid argument | |
249 for @code{cdr}. An error is signaled if the argument is not a cons cell | |
250 or @code{nil}. | |
251 | |
252 @example | |
253 @group | |
254 (cdr '(a b c)) | |
255 @result{} (b c) | |
256 @end group | |
257 @group | |
258 (cdr '()) | |
259 @result{} nil | |
260 @end group | |
261 @end example | |
262 @end defun | |
263 | |
264 @defun car-safe object | |
265 This function lets you take the @sc{car} of a cons cell while avoiding | |
266 errors for other data types. It returns the @sc{car} of @var{object} if | |
267 @var{object} is a cons cell, @code{nil} otherwise. This is in contrast | |
268 to @code{car}, which signals an error if @var{object} is not a list. | |
269 | |
270 @example | |
271 @group | |
272 (car-safe @var{object}) | |
273 @equiv{} | |
274 (let ((x @var{object})) | |
275 (if (consp x) | |
276 (car x) | |
277 nil)) | |
278 @end group | |
279 @end example | |
280 @end defun | |
281 | |
282 @defun cdr-safe object | |
283 This function lets you take the @sc{cdr} of a cons cell while | |
284 avoiding errors for other data types. It returns the @sc{cdr} of | |
285 @var{object} if @var{object} is a cons cell, @code{nil} otherwise. | |
286 This is in contrast to @code{cdr}, which signals an error if | |
287 @var{object} is not a list. | |
288 | |
289 @example | |
290 @group | |
291 (cdr-safe @var{object}) | |
292 @equiv{} | |
293 (let ((x @var{object})) | |
294 (if (consp x) | |
295 (cdr x) | |
296 nil)) | |
297 @end group | |
298 @end example | |
299 @end defun | |
300 | |
301 @defun nth n list | |
302 This function returns the @var{n}th element of @var{list}. Elements | |
303 are numbered starting with zero, so the @sc{car} of @var{list} is | |
304 element number zero. If the length of @var{list} is @var{n} or less, | |
305 the value is @code{nil}. | |
306 | |
307 If @var{n} is negative, @code{nth} returns the first element of | |
308 @var{list}. | |
309 | |
310 @example | |
311 @group | |
312 (nth 2 '(1 2 3 4)) | |
313 @result{} 3 | |
314 @end group | |
315 @group | |
316 (nth 10 '(1 2 3 4)) | |
317 @result{} nil | |
318 @end group | |
319 @group | |
320 (nth -3 '(1 2 3 4)) | |
321 @result{} 1 | |
322 | |
323 (nth n x) @equiv{} (car (nthcdr n x)) | |
324 @end group | |
325 @end example | |
326 @end defun | |
327 | |
328 @defun nthcdr n list | |
329 This function returns the @var{n}th @sc{cdr} of @var{list}. In other | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
330 words, it skips past the first @var{n} links of @var{list} and returns |
6558 | 331 what follows. |
332 | |
333 If @var{n} is zero or negative, @code{nthcdr} returns all of | |
334 @var{list}. If the length of @var{list} is @var{n} or less, | |
335 @code{nthcdr} returns @code{nil}. | |
336 | |
337 @example | |
338 @group | |
339 (nthcdr 1 '(1 2 3 4)) | |
340 @result{} (2 3 4) | |
341 @end group | |
342 @group | |
343 (nthcdr 10 '(1 2 3 4)) | |
344 @result{} nil | |
345 @end group | |
346 @group | |
347 (nthcdr -3 '(1 2 3 4)) | |
348 @result{} (1 2 3 4) | |
349 @end group | |
350 @end example | |
351 @end defun | |
352 | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
353 @tindex safe-length |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
354 @defun safe-length sequence |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
355 This function returns the length of @var{list}, with no risk |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
356 of either an error or an infinite loop. |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
357 |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
358 If @var{list} is not really a list, @code{safe-length} returns 0. If |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
359 @var{list} is circular, it returns a finite value which is at least the |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
360 number of distinct elements. |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
361 @end defun |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
362 |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
363 @tindex caar |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
364 @defun caar list |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
365 This is the same as @code{(car (car @var{list}))}. |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
366 @end defun |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
367 |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
368 @tindex cadr |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
369 @defun cadr list |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
370 This is the same as @code{(car (cdr @var{list}))} |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
371 or @code{(nth 1 @var{list})}. |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
372 @end defun |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
373 |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
374 @tindex cdar |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
375 @defun cdar list |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
376 This is the same as @code{(cdr (car @var{list}))}. |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
377 @end defun |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
378 |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
379 @tindex cddr |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
380 @defun cddr list |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
381 This is the same as @code{(cdr (cdr @var{list}))} |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
382 or @code{(nthcdr 2 @var{list})}. |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
383 @end defun |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
384 |
6558 | 385 @node Building Lists |
386 @comment node-name, next, previous, up | |
387 @section Building Cons Cells and Lists | |
388 @cindex cons cells | |
389 @cindex building lists | |
390 | |
391 Many functions build lists, as lists reside at the very heart of Lisp. | |
392 @code{cons} is the fundamental list-building function; however, it is | |
393 interesting to note that @code{list} is used more times in the source | |
394 code for Emacs than @code{cons}. | |
395 | |
396 @defun cons object1 object2 | |
397 This function is the fundamental function used to build new list | |
398 structure. It creates a new cons cell, making @var{object1} the | |
399 @sc{car}, and @var{object2} the @sc{cdr}. It then returns the new cons | |
400 cell. The arguments @var{object1} and @var{object2} may be any Lisp | |
401 objects, but most often @var{object2} is a list. | |
402 | |
403 @example | |
404 @group | |
405 (cons 1 '(2)) | |
406 @result{} (1 2) | |
407 @end group | |
408 @group | |
409 (cons 1 '()) | |
410 @result{} (1) | |
411 @end group | |
412 @group | |
413 (cons 1 2) | |
414 @result{} (1 . 2) | |
415 @end group | |
416 @end example | |
417 | |
418 @cindex consing | |
419 @code{cons} is often used to add a single element to the front of a | |
420 list. This is called @dfn{consing the element onto the list}. For | |
421 example: | |
422 | |
423 @example | |
424 (setq list (cons newelt list)) | |
425 @end example | |
426 | |
427 Note that there is no conflict between the variable named @code{list} | |
428 used in this example and the function named @code{list} described below; | |
429 any symbol can serve both purposes. | |
430 @end defun | |
431 | |
432 @defun list &rest objects | |
433 This function creates a list with @var{objects} as its elements. The | |
434 resulting list is always @code{nil}-terminated. If no @var{objects} | |
435 are given, the empty list is returned. | |
436 | |
437 @example | |
438 @group | |
439 (list 1 2 3 4 5) | |
440 @result{} (1 2 3 4 5) | |
441 @end group | |
442 @group | |
443 (list 1 2 '(3 4 5) 'foo) | |
444 @result{} (1 2 (3 4 5) foo) | |
445 @end group | |
446 @group | |
447 (list) | |
448 @result{} nil | |
449 @end group | |
450 @end example | |
451 @end defun | |
452 | |
453 @defun make-list length object | |
454 This function creates a list of length @var{length}, in which all the | |
455 elements have the identical value @var{object}. Compare | |
456 @code{make-list} with @code{make-string} (@pxref{Creating Strings}). | |
457 | |
458 @example | |
459 @group | |
460 (make-list 3 'pigs) | |
461 @result{} (pigs pigs pigs) | |
462 @end group | |
463 @group | |
464 (make-list 0 'pigs) | |
465 @result{} nil | |
466 @end group | |
467 @end example | |
468 @end defun | |
469 | |
470 @defun append &rest sequences | |
471 @cindex copying lists | |
472 This function returns a list containing all the elements of | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
473 @var{sequences}. The @var{sequences} may be lists, vectors, or strings, |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
474 but the last one should usually be a list. All arguments except the |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
475 last one are copied, so none of the arguments is altered. |
6558 | 476 |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
477 More generally, the final argument to @code{append} may be any Lisp |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
478 object. The final argument is not copied or converted; it becomes the |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
479 @sc{cdr} of the last cons cell in the new list. If the final argument |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
480 is itself a list, then its elements become in effect elements of the |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
481 result list. If the final element is not a list, the result is a |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
482 ``dotted list'' since its final @sc{cdr} is not @code{nil} as required |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
483 in a true list. |
6558 | 484 |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
485 See @code{nconc} in @ref{Rearrangement}, for a way to join lists with no |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
486 copying. |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
487 |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
488 Here is an example of using @code{append}: |
6558 | 489 |
490 @example | |
491 @group | |
492 (setq trees '(pine oak)) | |
493 @result{} (pine oak) | |
494 (setq more-trees (append '(maple birch) trees)) | |
495 @result{} (maple birch pine oak) | |
496 @end group | |
497 | |
498 @group | |
499 trees | |
500 @result{} (pine oak) | |
501 more-trees | |
502 @result{} (maple birch pine oak) | |
503 @end group | |
504 @group | |
505 (eq trees (cdr (cdr more-trees))) | |
506 @result{} t | |
507 @end group | |
508 @end example | |
509 | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
510 You can see how @code{append} works by looking at a box diagram. The |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
511 variable @code{trees} is set to the list @code{(pine oak)} and then the |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
512 variable @code{more-trees} is set to the list @code{(maple birch pine |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
513 oak)}. However, the variable @code{trees} continues to refer to the |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
514 original list: |
6558 | 515 |
516 @smallexample | |
517 @group | |
518 more-trees trees | |
519 | | | |
520 | ___ ___ ___ ___ -> ___ ___ ___ ___ | |
521 --> |___|___|--> |___|___|--> |___|___|--> |___|___|--> nil | |
522 | | | | | |
523 | | | | | |
524 --> maple -->birch --> pine --> oak | |
525 @end group | |
526 @end smallexample | |
527 | |
528 An empty sequence contributes nothing to the value returned by | |
529 @code{append}. As a consequence of this, a final @code{nil} argument | |
530 forces a copy of the previous argument. | |
531 | |
532 @example | |
533 @group | |
534 trees | |
535 @result{} (pine oak) | |
536 @end group | |
537 @group | |
538 (setq wood (append trees ())) | |
539 @result{} (pine oak) | |
540 @end group | |
541 @group | |
542 wood | |
543 @result{} (pine oak) | |
544 @end group | |
545 @group | |
546 (eq wood trees) | |
547 @result{} nil | |
548 @end group | |
549 @end example | |
550 | |
551 @noindent | |
552 This once was the usual way to copy a list, before the function | |
553 @code{copy-sequence} was invented. @xref{Sequences Arrays Vectors}. | |
554 | |
555 With the help of @code{apply}, we can append all the lists in a list of | |
556 lists: | |
557 | |
558 @example | |
559 @group | |
560 (apply 'append '((a b c) nil (x y z) nil)) | |
561 @result{} (a b c x y z) | |
562 @end group | |
563 @end example | |
564 | |
565 If no @var{sequences} are given, @code{nil} is returned: | |
566 | |
567 @example | |
568 @group | |
569 (append) | |
570 @result{} nil | |
571 @end group | |
572 @end example | |
573 | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
574 Here are some examples where the final argument is not a list: |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
575 |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
576 @example |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
577 (append '(x y) 'z) |
12098 | 578 @result{} (x y . z) |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
579 (append '(x y) [z]) |
12098 | 580 @result{} (x y . [z]) |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
581 @end example |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
582 |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
583 @noindent |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
584 The second example shows that when the final argument is a sequence but |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
585 not a list, the sequence's elements do not become elements of the |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
586 resulting list. Instead, the sequence becomes the final @sc{cdr}, like |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
587 any other non-list final argument. |
6558 | 588 |
12067 | 589 The @code{append} function also allows integers as arguments. It |
590 converts them to strings of digits, making up the decimal print | |
591 representation of the integer, and then uses the strings instead of the | |
592 original integers. @strong{Don't use this feature; we plan to eliminate | |
593 it. If you already use this feature, change your programs now!} The | |
594 proper way to convert an integer to a decimal number in this way is with | |
595 @code{format} (@pxref{Formatting Strings}) or @code{number-to-string} | |
596 (@pxref{String Conversion}). | |
6558 | 597 @end defun |
598 | |
599 @defun reverse list | |
600 This function creates a new list whose elements are the elements of | |
601 @var{list}, but in reverse order. The original argument @var{list} is | |
602 @emph{not} altered. | |
603 | |
604 @example | |
605 @group | |
606 (setq x '(1 2 3 4)) | |
607 @result{} (1 2 3 4) | |
608 @end group | |
609 @group | |
610 (reverse x) | |
611 @result{} (4 3 2 1) | |
612 x | |
613 @result{} (1 2 3 4) | |
614 @end group | |
615 @end example | |
616 @end defun | |
617 | |
618 @node Modifying Lists | |
619 @section Modifying Existing List Structure | |
620 | |
621 You can modify the @sc{car} and @sc{cdr} contents of a cons cell with the | |
622 primitives @code{setcar} and @code{setcdr}. | |
623 | |
624 @cindex CL note---@code{rplaca} vrs @code{setcar} | |
625 @quotation | |
626 @findex rplaca | |
627 @findex rplacd | |
628 @b{Common Lisp note:} Common Lisp uses functions @code{rplaca} and | |
629 @code{rplacd} to alter list structure; they change structure the same | |
630 way as @code{setcar} and @code{setcdr}, but the Common Lisp functions | |
631 return the cons cell while @code{setcar} and @code{setcdr} return the | |
632 new @sc{car} or @sc{cdr}. | |
633 @end quotation | |
634 | |
635 @menu | |
636 * Setcar:: Replacing an element in a list. | |
637 * Setcdr:: Replacing part of the list backbone. | |
638 This can be used to remove or add elements. | |
639 * Rearrangement:: Reordering the elements in a list; combining lists. | |
640 @end menu | |
641 | |
642 @node Setcar | |
643 @subsection Altering List Elements with @code{setcar} | |
644 | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
645 Changing the @sc{car} of a cons cell is done with @code{setcar}. When |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
646 used on a list, @code{setcar} replaces one element of a list with a |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
647 different element. |
6558 | 648 |
649 @defun setcar cons object | |
650 This function stores @var{object} as the new @sc{car} of @var{cons}, | |
651 replacing its previous @sc{car}. It returns the value @var{object}. | |
652 For example: | |
653 | |
654 @example | |
655 @group | |
656 (setq x '(1 2)) | |
657 @result{} (1 2) | |
658 @end group | |
659 @group | |
660 (setcar x 4) | |
661 @result{} 4 | |
662 @end group | |
663 @group | |
664 x | |
665 @result{} (4 2) | |
666 @end group | |
667 @end example | |
668 @end defun | |
669 | |
670 When a cons cell is part of the shared structure of several lists, | |
671 storing a new @sc{car} into the cons changes one element of each of | |
672 these lists. Here is an example: | |
673 | |
674 @example | |
675 @group | |
676 ;; @r{Create two lists that are partly shared.} | |
677 (setq x1 '(a b c)) | |
678 @result{} (a b c) | |
679 (setq x2 (cons 'z (cdr x1))) | |
680 @result{} (z b c) | |
681 @end group | |
682 | |
683 @group | |
684 ;; @r{Replace the @sc{car} of a shared link.} | |
685 (setcar (cdr x1) 'foo) | |
686 @result{} foo | |
687 x1 ; @r{Both lists are changed.} | |
688 @result{} (a foo c) | |
689 x2 | |
690 @result{} (z foo c) | |
691 @end group | |
692 | |
693 @group | |
694 ;; @r{Replace the @sc{car} of a link that is not shared.} | |
695 (setcar x1 'baz) | |
696 @result{} baz | |
697 x1 ; @r{Only one list is changed.} | |
698 @result{} (baz foo c) | |
699 x2 | |
700 @result{} (z foo c) | |
701 @end group | |
702 @end example | |
703 | |
704 Here is a graphical depiction of the shared structure of the two lists | |
705 in the variables @code{x1} and @code{x2}, showing why replacing @code{b} | |
706 changes them both: | |
707 | |
708 @example | |
709 @group | |
710 ___ ___ ___ ___ ___ ___ | |
711 x1---> |___|___|----> |___|___|--> |___|___|--> nil | |
712 | --> | | | |
713 | | | | | |
714 --> a | --> b --> c | |
715 | | |
716 ___ ___ | | |
717 x2--> |___|___|-- | |
718 | | |
719 | | |
720 --> z | |
721 @end group | |
722 @end example | |
723 | |
724 Here is an alternative form of box diagram, showing the same relationship: | |
725 | |
726 @example | |
727 @group | |
728 x1: | |
729 -------------- -------------- -------------- | |
730 | car | cdr | | car | cdr | | car | cdr | | |
731 | a | o------->| b | o------->| c | nil | | |
732 | | | -->| | | | | | | |
733 -------------- | -------------- -------------- | |
734 | | |
735 x2: | | |
736 -------------- | | |
737 | car | cdr | | | |
738 | z | o---- | |
739 | | | | |
740 -------------- | |
741 @end group | |
742 @end example | |
743 | |
744 @node Setcdr | |
745 @subsection Altering the CDR of a List | |
746 | |
747 The lowest-level primitive for modifying a @sc{cdr} is @code{setcdr}: | |
748 | |
749 @defun setcdr cons object | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
750 This function stores @var{object} as the new @sc{cdr} of @var{cons}, |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
751 replacing its previous @sc{cdr}. It returns the value @var{object}. |
6558 | 752 @end defun |
753 | |
754 Here is an example of replacing the @sc{cdr} of a list with a | |
755 different list. All but the first element of the list are removed in | |
756 favor of a different sequence of elements. The first element is | |
757 unchanged, because it resides in the @sc{car} of the list, and is not | |
758 reached via the @sc{cdr}. | |
759 | |
760 @example | |
761 @group | |
762 (setq x '(1 2 3)) | |
763 @result{} (1 2 3) | |
764 @end group | |
765 @group | |
766 (setcdr x '(4)) | |
767 @result{} (4) | |
768 @end group | |
769 @group | |
770 x | |
771 @result{} (1 4) | |
772 @end group | |
773 @end example | |
774 | |
775 You can delete elements from the middle of a list by altering the | |
776 @sc{cdr}s of the cons cells in the list. For example, here we delete | |
777 the second element, @code{b}, from the list @code{(a b c)}, by changing | |
778 the @sc{cdr} of the first cell: | |
779 | |
780 @example | |
781 @group | |
782 (setq x1 '(a b c)) | |
783 @result{} (a b c) | |
784 (setcdr x1 (cdr (cdr x1))) | |
785 @result{} (c) | |
786 x1 | |
787 @result{} (a c) | |
788 @end group | |
789 @end example | |
790 | |
12282
586e3ea81792
updates for version 19.29 made by melissa; also needed to check out files
Melissa Weisshaus <melissa@gnu.org>
parents:
12098
diff
changeset
|
791 @need 4000 |
6558 | 792 Here is the result in box notation: |
793 | |
794 @example | |
795 @group | |
796 -------------------- | |
797 | | | |
798 -------------- | -------------- | -------------- | |
799 | car | cdr | | | car | cdr | -->| car | cdr | | |
800 | a | o----- | b | o-------->| c | nil | | |
801 | | | | | | | | | | |
802 -------------- -------------- -------------- | |
803 @end group | |
804 @end example | |
805 | |
806 @noindent | |
807 The second cons cell, which previously held the element @code{b}, still | |
808 exists and its @sc{car} is still @code{b}, but it no longer forms part | |
809 of this list. | |
810 | |
811 It is equally easy to insert a new element by changing @sc{cdr}s: | |
812 | |
813 @example | |
814 @group | |
815 (setq x1 '(a b c)) | |
816 @result{} (a b c) | |
817 (setcdr x1 (cons 'd (cdr x1))) | |
818 @result{} (d b c) | |
819 x1 | |
820 @result{} (a d b c) | |
821 @end group | |
822 @end example | |
823 | |
824 Here is this result in box notation: | |
825 | |
826 @smallexample | |
827 @group | |
828 -------------- ------------- ------------- | |
829 | car | cdr | | car | cdr | | car | cdr | | |
830 | a | o | -->| b | o------->| c | nil | | |
831 | | | | | | | | | | | | |
832 --------- | -- | ------------- ------------- | |
833 | | | |
834 ----- -------- | |
835 | | | |
836 | --------------- | | |
837 | | car | cdr | | | |
838 -->| d | o------ | |
839 | | | | |
840 --------------- | |
841 @end group | |
842 @end smallexample | |
843 | |
844 @node Rearrangement | |
845 @subsection Functions that Rearrange Lists | |
846 @cindex rearrangement of lists | |
847 @cindex modification of lists | |
848 | |
849 Here are some functions that rearrange lists ``destructively'' by | |
850 modifying the @sc{cdr}s of their component cons cells. We call these | |
851 functions ``destructive'' because they chew up the original lists passed | |
852 to them as arguments, to produce a new list that is the returned value. | |
853 | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
854 @ifinfo |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
855 See @code{delq}, in @ref{Sets And Lists}, for another function |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
856 that modifies cons cells. |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
857 @end ifinfo |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
858 @iftex |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
859 The function @code{delq} in the following section is another example |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
860 of destructive list manipulation. |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
861 @end iftex |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
862 |
6558 | 863 @defun nconc &rest lists |
864 @cindex concatenating lists | |
865 @cindex joining lists | |
866 This function returns a list containing all the elements of @var{lists}. | |
867 Unlike @code{append} (@pxref{Building Lists}), the @var{lists} are | |
868 @emph{not} copied. Instead, the last @sc{cdr} of each of the | |
869 @var{lists} is changed to refer to the following list. The last of the | |
870 @var{lists} is not altered. For example: | |
871 | |
872 @example | |
873 @group | |
874 (setq x '(1 2 3)) | |
875 @result{} (1 2 3) | |
876 @end group | |
877 @group | |
878 (nconc x '(4 5)) | |
879 @result{} (1 2 3 4 5) | |
880 @end group | |
881 @group | |
882 x | |
883 @result{} (1 2 3 4 5) | |
884 @end group | |
885 @end example | |
886 | |
887 Since the last argument of @code{nconc} is not itself modified, it is | |
888 reasonable to use a constant list, such as @code{'(4 5)}, as in the | |
889 above example. For the same reason, the last argument need not be a | |
890 list: | |
891 | |
892 @example | |
893 @group | |
894 (setq x '(1 2 3)) | |
895 @result{} (1 2 3) | |
896 @end group | |
897 @group | |
898 (nconc x 'z) | |
899 @result{} (1 2 3 . z) | |
900 @end group | |
901 @group | |
902 x | |
903 @result{} (1 2 3 . z) | |
904 @end group | |
905 @end example | |
906 | |
907 A common pitfall is to use a quoted constant list as a non-last | |
908 argument to @code{nconc}. If you do this, your program will change | |
909 each time you run it! Here is what happens: | |
910 | |
911 @smallexample | |
912 @group | |
913 (defun add-foo (x) ; @r{We want this function to add} | |
914 (nconc '(foo) x)) ; @r{@code{foo} to the front of its arg.} | |
915 @end group | |
916 | |
917 @group | |
918 (symbol-function 'add-foo) | |
919 @result{} (lambda (x) (nconc (quote (foo)) x)) | |
920 @end group | |
921 | |
922 @group | |
923 (setq xx (add-foo '(1 2))) ; @r{It seems to work.} | |
924 @result{} (foo 1 2) | |
925 @end group | |
926 @group | |
927 (setq xy (add-foo '(3 4))) ; @r{What happened?} | |
928 @result{} (foo 1 2 3 4) | |
929 @end group | |
930 @group | |
931 (eq xx xy) | |
932 @result{} t | |
933 @end group | |
934 | |
935 @group | |
936 (symbol-function 'add-foo) | |
937 @result{} (lambda (x) (nconc (quote (foo 1 2 3 4) x))) | |
938 @end group | |
939 @end smallexample | |
940 @end defun | |
941 | |
942 @defun nreverse list | |
943 @cindex reversing a list | |
944 This function reverses the order of the elements of @var{list}. | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
945 Unlike @code{reverse}, @code{nreverse} alters its argument by reversing |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
946 the @sc{cdr}s in the cons cells forming the list. The cons cell that |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
947 used to be the last one in @var{list} becomes the first cell of the |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
948 value. |
6558 | 949 |
950 For example: | |
951 | |
952 @example | |
953 @group | |
954 (setq x '(1 2 3 4)) | |
955 @result{} (1 2 3 4) | |
956 @end group | |
957 @group | |
958 x | |
959 @result{} (1 2 3 4) | |
960 (nreverse x) | |
961 @result{} (4 3 2 1) | |
962 @end group | |
963 @group | |
964 ;; @r{The cell that was first is now last.} | |
965 x | |
966 @result{} (1) | |
967 @end group | |
968 @end example | |
969 | |
970 To avoid confusion, we usually store the result of @code{nreverse} | |
971 back in the same variable which held the original list: | |
972 | |
973 @example | |
974 (setq x (nreverse x)) | |
975 @end example | |
976 | |
977 Here is the @code{nreverse} of our favorite example, @code{(a b c)}, | |
978 presented graphically: | |
979 | |
980 @smallexample | |
981 @group | |
982 @r{Original list head:} @r{Reversed list:} | |
983 ------------- ------------- ------------ | |
984 | car | cdr | | car | cdr | | car | cdr | | |
985 | a | nil |<-- | b | o |<-- | c | o | | |
986 | | | | | | | | | | | | | | |
987 ------------- | --------- | - | -------- | - | |
988 | | | | | |
989 ------------- ------------ | |
990 @end group | |
991 @end smallexample | |
992 @end defun | |
993 | |
994 @defun sort list predicate | |
995 @cindex stable sort | |
996 @cindex sorting lists | |
997 This function sorts @var{list} stably, though destructively, and | |
998 returns the sorted list. It compares elements using @var{predicate}. A | |
999 stable sort is one in which elements with equal sort keys maintain their | |
1000 relative order before and after the sort. Stability is important when | |
1001 successive sorts are used to order elements according to different | |
1002 criteria. | |
1003 | |
1004 The argument @var{predicate} must be a function that accepts two | |
1005 arguments. It is called with two elements of @var{list}. To get an | |
1006 increasing order sort, the @var{predicate} should return @code{t} if the | |
1007 first element is ``less than'' the second, or @code{nil} if not. | |
1008 | |
1009 The destructive aspect of @code{sort} is that it rearranges the cons | |
1010 cells forming @var{list} by changing @sc{cdr}s. A nondestructive sort | |
1011 function would create new cons cells to store the elements in their | |
1012 sorted order. If you wish to make a sorted copy without destroying the | |
1013 original, copy it first with @code{copy-sequence} and then sort. | |
1014 | |
1015 Sorting does not change the @sc{car}s of the cons cells in @var{list}; | |
1016 the cons cell that originally contained the element @code{a} in | |
1017 @var{list} still has @code{a} in its @sc{car} after sorting, but it now | |
1018 appears in a different position in the list due to the change of | |
1019 @sc{cdr}s. For example: | |
1020 | |
1021 @example | |
1022 @group | |
1023 (setq nums '(1 3 2 6 5 4 0)) | |
1024 @result{} (1 3 2 6 5 4 0) | |
1025 @end group | |
1026 @group | |
1027 (sort nums '<) | |
1028 @result{} (0 1 2 3 4 5 6) | |
1029 @end group | |
1030 @group | |
1031 nums | |
1032 @result{} (1 2 3 4 5 6) | |
1033 @end group | |
1034 @end example | |
1035 | |
1036 @noindent | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
1037 @strong{Warning}: Note that the list in @code{nums} no longer contains |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
1038 0; this is the same cons cell that it was before, but it is no longer |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
1039 the first one in the list. Don't assume a variable that formerly held |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
1040 the argument now holds the entire sorted list! Instead, save the result |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
1041 of @code{sort} and use that. Most often we store the result back into |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
1042 the variable that held the original list: |
6558 | 1043 |
1044 @example | |
1045 (setq nums (sort nums '<)) | |
1046 @end example | |
1047 | |
1048 @xref{Sorting}, for more functions that perform sorting. | |
1049 See @code{documentation} in @ref{Accessing Documentation}, for a | |
1050 useful example of @code{sort}. | |
1051 @end defun | |
1052 | |
1053 @node Sets And Lists | |
1054 @section Using Lists as Sets | |
1055 @cindex lists as sets | |
1056 @cindex sets | |
1057 | |
1058 A list can represent an unordered mathematical set---simply consider a | |
1059 value an element of a set if it appears in the list, and ignore the | |
1060 order of the list. To form the union of two sets, use @code{append} (as | |
1061 long as you don't mind having duplicate elements). Other useful | |
1062 functions for sets include @code{memq} and @code{delq}, and their | |
1063 @code{equal} versions, @code{member} and @code{delete}. | |
1064 | |
13229 | 1065 @cindex CL note---lack @code{union}, @code{intersection} |
6558 | 1066 @quotation |
1067 @b{Common Lisp note:} Common Lisp has functions @code{union} (which | |
1068 avoids duplicate elements) and @code{intersection} for set operations, | |
1069 but GNU Emacs Lisp does not have them. You can write them in Lisp if | |
1070 you wish. | |
1071 @end quotation | |
1072 | |
1073 @defun memq object list | |
1074 @cindex membership in a list | |
1075 This function tests to see whether @var{object} is a member of | |
1076 @var{list}. If it is, @code{memq} returns a list starting with the | |
1077 first occurrence of @var{object}. Otherwise, it returns @code{nil}. | |
1078 The letter @samp{q} in @code{memq} says that it uses @code{eq} to | |
1079 compare @var{object} against the elements of the list. For example: | |
1080 | |
1081 @example | |
1082 @group | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1083 (memq 'b '(a b c b a)) |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1084 @result{} (b c b a) |
6558 | 1085 @end group |
1086 @group | |
1087 (memq '(2) '((1) (2))) ; @r{@code{(2)} and @code{(2)} are not @code{eq}.} | |
1088 @result{} nil | |
1089 @end group | |
1090 @end example | |
1091 @end defun | |
1092 | |
1093 @defun delq object list | |
1094 @cindex deletion of elements | |
1095 This function destructively removes all elements @code{eq} to | |
1096 @var{object} from @var{list}. The letter @samp{q} in @code{delq} says | |
1097 that it uses @code{eq} to compare @var{object} against the elements of | |
1098 the list, like @code{memq}. | |
1099 @end defun | |
1100 | |
1101 When @code{delq} deletes elements from the front of the list, it does so | |
1102 simply by advancing down the list and returning a sublist that starts | |
1103 after those elements: | |
1104 | |
1105 @example | |
1106 @group | |
1107 (delq 'a '(a b c)) @equiv{} (cdr '(a b c)) | |
1108 @end group | |
1109 @end example | |
1110 | |
1111 When an element to be deleted appears in the middle of the list, | |
1112 removing it involves changing the @sc{cdr}s (@pxref{Setcdr}). | |
1113 | |
1114 @example | |
1115 @group | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1116 (setq sample-list '(a b c (4))) |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1117 @result{} (a b c (4)) |
6558 | 1118 @end group |
1119 @group | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1120 (delq 'a sample-list) |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1121 @result{} (b c (4)) |
6558 | 1122 @end group |
1123 @group | |
1124 sample-list | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1125 @result{} (a b c (4)) |
6558 | 1126 @end group |
1127 @group | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1128 (delq 'c sample-list) |
11628 | 1129 @result{} (a b (4)) |
6558 | 1130 @end group |
1131 @group | |
1132 sample-list | |
11628 | 1133 @result{} (a b (4)) |
6558 | 1134 @end group |
1135 @end example | |
1136 | |
12098 | 1137 Note that @code{(delq 'c sample-list)} modifies @code{sample-list} to |
1138 splice out the third element, but @code{(delq 'a sample-list)} does not | |
6558 | 1139 splice anything---it just returns a shorter list. Don't assume that a |
1140 variable which formerly held the argument @var{list} now has fewer | |
1141 elements, or that it still holds the original list! Instead, save the | |
1142 result of @code{delq} and use that. Most often we store the result back | |
1143 into the variable that held the original list: | |
1144 | |
1145 @example | |
1146 (setq flowers (delq 'rose flowers)) | |
1147 @end example | |
1148 | |
1149 In the following example, the @code{(4)} that @code{delq} attempts to match | |
1150 and the @code{(4)} in the @code{sample-list} are not @code{eq}: | |
1151 | |
1152 @example | |
1153 @group | |
1154 (delq '(4) sample-list) | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1155 @result{} (a c (4)) |
6558 | 1156 @end group |
1157 @end example | |
1158 | |
1159 The following two functions are like @code{memq} and @code{delq} but use | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
1160 @code{equal} rather than @code{eq} to compare elements. |
6558 | 1161 |
1162 @defun member object list | |
1163 The function @code{member} tests to see whether @var{object} is a member | |
1164 of @var{list}, comparing members with @var{object} using @code{equal}. | |
1165 If @var{object} is a member, @code{member} returns a list starting with | |
1166 its first occurrence in @var{list}. Otherwise, it returns @code{nil}. | |
1167 | |
1168 Compare this with @code{memq}: | |
1169 | |
1170 @example | |
1171 @group | |
1172 (member '(2) '((1) (2))) ; @r{@code{(2)} and @code{(2)} are @code{equal}.} | |
1173 @result{} ((2)) | |
1174 @end group | |
1175 @group | |
1176 (memq '(2) '((1) (2))) ; @r{@code{(2)} and @code{(2)} are not @code{eq}.} | |
1177 @result{} nil | |
1178 @end group | |
1179 @group | |
1180 ;; @r{Two strings with the same contents are @code{equal}.} | |
1181 (member "foo" '("foo" "bar")) | |
1182 @result{} ("foo" "bar") | |
1183 @end group | |
1184 @end example | |
1185 @end defun | |
1186 | |
1187 @defun delete object list | |
1188 This function destructively removes all elements @code{equal} to | |
1189 @var{object} from @var{list}. It is to @code{delq} as @code{member} is | |
1190 to @code{memq}: it uses @code{equal} to compare elements with | |
1191 @var{object}, like @code{member}; when it finds an element that matches, | |
1192 it removes the element just as @code{delq} would. For example: | |
1193 | |
1194 @example | |
1195 @group | |
1196 (delete '(2) '((2) (1) (2))) | |
13229 | 1197 @result{} ((1)) |
6558 | 1198 @end group |
1199 @end example | |
1200 @end defun | |
1201 | |
1202 @quotation | |
1203 @b{Common Lisp note:} The functions @code{member} and @code{delete} in | |
1204 GNU Emacs Lisp are derived from Maclisp, not Common Lisp. The Common | |
1205 Lisp versions do not use @code{equal} to compare elements. | |
1206 @end quotation | |
1207 | |
12098 | 1208 See also the function @code{add-to-list}, in @ref{Setting Variables}, |
1209 for another way to add an element to a list stored in a variable. | |
1210 | |
6558 | 1211 @node Association Lists |
1212 @section Association Lists | |
1213 @cindex association list | |
1214 @cindex alist | |
1215 | |
1216 An @dfn{association list}, or @dfn{alist} for short, records a mapping | |
1217 from keys to values. It is a list of cons cells called | |
1218 @dfn{associations}: the @sc{car} of each cell is the @dfn{key}, and the | |
1219 @sc{cdr} is the @dfn{associated value}.@footnote{This usage of ``key'' | |
1220 is not related to the term ``key sequence''; it means a value used to | |
1221 look up an item in a table. In this case, the table is the alist, and | |
1222 the alist associations are the items.} | |
1223 | |
1224 Here is an example of an alist. The key @code{pine} is associated with | |
1225 the value @code{cones}; the key @code{oak} is associated with | |
1226 @code{acorns}; and the key @code{maple} is associated with @code{seeds}. | |
1227 | |
1228 @example | |
1229 @group | |
1230 '((pine . cones) | |
1231 (oak . acorns) | |
1232 (maple . seeds)) | |
1233 @end group | |
1234 @end example | |
1235 | |
1236 The associated values in an alist may be any Lisp objects; so may the | |
1237 keys. For example, in the following alist, the symbol @code{a} is | |
1238 associated with the number @code{1}, and the string @code{"b"} is | |
1239 associated with the @emph{list} @code{(2 3)}, which is the @sc{cdr} of | |
1240 the alist element: | |
1241 | |
1242 @example | |
1243 ((a . 1) ("b" 2 3)) | |
1244 @end example | |
1245 | |
1246 Sometimes it is better to design an alist to store the associated | |
1247 value in the @sc{car} of the @sc{cdr} of the element. Here is an | |
1248 example: | |
1249 | |
1250 @example | |
1251 '((rose red) (lily white) (buttercup yellow)) | |
1252 @end example | |
1253 | |
1254 @noindent | |
1255 Here we regard @code{red} as the value associated with @code{rose}. One | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
13229
diff
changeset
|
1256 advantage of this kind of alist is that you can store other related |
6558 | 1257 information---even a list of other items---in the @sc{cdr} of the |
1258 @sc{cdr}. One disadvantage is that you cannot use @code{rassq} (see | |
1259 below) to find the element containing a given value. When neither of | |
1260 these considerations is important, the choice is a matter of taste, as | |
1261 long as you are consistent about it for any given alist. | |
1262 | |
1263 Note that the same alist shown above could be regarded as having the | |
1264 associated value in the @sc{cdr} of the element; the value associated | |
1265 with @code{rose} would be the list @code{(red)}. | |
1266 | |
1267 Association lists are often used to record information that you might | |
1268 otherwise keep on a stack, since new associations may be added easily to | |
1269 the front of the list. When searching an association list for an | |
1270 association with a given key, the first one found is returned, if there | |
1271 is more than one. | |
1272 | |
1273 In Emacs Lisp, it is @emph{not} an error if an element of an | |
1274 association list is not a cons cell. The alist search functions simply | |
1275 ignore such elements. Many other versions of Lisp signal errors in such | |
1276 cases. | |
1277 | |
1278 Note that property lists are similar to association lists in several | |
1279 respects. A property list behaves like an association list in which | |
1280 each key can occur only once. @xref{Property Lists}, for a comparison | |
1281 of property lists and association lists. | |
1282 | |
1283 @defun assoc key alist | |
1284 This function returns the first association for @var{key} in | |
1285 @var{alist}. It compares @var{key} against the alist elements using | |
1286 @code{equal} (@pxref{Equality Predicates}). It returns @code{nil} if no | |
1287 association in @var{alist} has a @sc{car} @code{equal} to @var{key}. | |
1288 For example: | |
1289 | |
1290 @smallexample | |
1291 (setq trees '((pine . cones) (oak . acorns) (maple . seeds))) | |
1292 @result{} ((pine . cones) (oak . acorns) (maple . seeds)) | |
1293 (assoc 'oak trees) | |
1294 @result{} (oak . acorns) | |
1295 (cdr (assoc 'oak trees)) | |
1296 @result{} acorns | |
1297 (assoc 'birch trees) | |
1298 @result{} nil | |
1299 @end smallexample | |
1300 | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1301 Here is another example, in which the keys and values are not symbols: |
6558 | 1302 |
1303 @smallexample | |
1304 (setq needles-per-cluster | |
1305 '((2 "Austrian Pine" "Red Pine") | |
1306 (3 "Pitch Pine") | |
1307 (5 "White Pine"))) | |
1308 | |
1309 (cdr (assoc 3 needles-per-cluster)) | |
1310 @result{} ("Pitch Pine") | |
1311 (cdr (assoc 2 needles-per-cluster)) | |
1312 @result{} ("Austrian Pine" "Red Pine") | |
1313 @end smallexample | |
1314 @end defun | |
1315 | |
12067 | 1316 @defun rassoc value alist |
1317 This function returns the first association with value @var{value} in | |
1318 @var{alist}. It returns @code{nil} if no association in @var{alist} has | |
1319 a @sc{cdr} @code{equal} to @var{value}. | |
1320 | |
1321 @code{rassoc} is like @code{assoc} except that it compares the @sc{cdr} of | |
1322 each @var{alist} association instead of the @sc{car}. You can think of | |
1323 this as ``reverse @code{assoc}'', finding the key for a given value. | |
1324 @end defun | |
1325 | |
6558 | 1326 @defun assq key alist |
1327 This function is like @code{assoc} in that it returns the first | |
1328 association for @var{key} in @var{alist}, but it makes the comparison | |
1329 using @code{eq} instead of @code{equal}. @code{assq} returns @code{nil} | |
1330 if no association in @var{alist} has a @sc{car} @code{eq} to @var{key}. | |
1331 This function is used more often than @code{assoc}, since @code{eq} is | |
1332 faster than @code{equal} and most alists use symbols as keys. | |
1333 @xref{Equality Predicates}. | |
1334 | |
1335 @smallexample | |
1336 (setq trees '((pine . cones) (oak . acorns) (maple . seeds))) | |
1337 @result{} ((pine . cones) (oak . acorns) (maple . seeds)) | |
1338 (assq 'pine trees) | |
1339 @result{} (pine . cones) | |
1340 @end smallexample | |
1341 | |
1342 On the other hand, @code{assq} is not usually useful in alists where the | |
1343 keys may not be symbols: | |
1344 | |
1345 @smallexample | |
1346 (setq leaves | |
1347 '(("simple leaves" . oak) | |
1348 ("compound leaves" . horsechestnut))) | |
1349 | |
1350 (assq "simple leaves" leaves) | |
1351 @result{} nil | |
1352 (assoc "simple leaves" leaves) | |
1353 @result{} ("simple leaves" . oak) | |
1354 @end smallexample | |
1355 @end defun | |
1356 | |
1357 @defun rassq value alist | |
1358 This function returns the first association with value @var{value} in | |
1359 @var{alist}. It returns @code{nil} if no association in @var{alist} has | |
1360 a @sc{cdr} @code{eq} to @var{value}. | |
1361 | |
1362 @code{rassq} is like @code{assq} except that it compares the @sc{cdr} of | |
1363 each @var{alist} association instead of the @sc{car}. You can think of | |
1364 this as ``reverse @code{assq}'', finding the key for a given value. | |
1365 | |
1366 For example: | |
1367 | |
1368 @smallexample | |
1369 (setq trees '((pine . cones) (oak . acorns) (maple . seeds))) | |
1370 | |
1371 (rassq 'acorns trees) | |
1372 @result{} (oak . acorns) | |
1373 (rassq 'spores trees) | |
1374 @result{} nil | |
1375 @end smallexample | |
1376 | |
1377 Note that @code{rassq} cannot search for a value stored in the @sc{car} | |
1378 of the @sc{cdr} of an element: | |
1379 | |
1380 @smallexample | |
1381 (setq colors '((rose red) (lily white) (buttercup yellow))) | |
1382 | |
1383 (rassq 'white colors) | |
1384 @result{} nil | |
1385 @end smallexample | |
1386 | |
1387 In this case, the @sc{cdr} of the association @code{(lily white)} is not | |
1388 the symbol @code{white}, but rather the list @code{(white)}. This | |
1389 becomes clearer if the association is written in dotted pair notation: | |
1390 | |
1391 @smallexample | |
1392 (lily white) @equiv{} (lily . (white)) | |
1393 @end smallexample | |
1394 @end defun | |
1395 | |
1396 @defun copy-alist alist | |
1397 @cindex copying alists | |
1398 This function returns a two-level deep copy of @var{alist}: it creates a | |
1399 new copy of each association, so that you can alter the associations of | |
1400 the new alist without changing the old one. | |
1401 | |
1402 @smallexample | |
1403 @group | |
1404 (setq needles-per-cluster | |
1405 '((2 . ("Austrian Pine" "Red Pine")) | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1406 (3 . ("Pitch Pine")) |
7734 | 1407 @end group |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1408 (5 . ("White Pine")))) |
6558 | 1409 @result{} |
1410 ((2 "Austrian Pine" "Red Pine") | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1411 (3 "Pitch Pine") |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1412 (5 "White Pine")) |
6558 | 1413 |
1414 (setq copy (copy-alist needles-per-cluster)) | |
1415 @result{} | |
1416 ((2 "Austrian Pine" "Red Pine") | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1417 (3 "Pitch Pine") |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1418 (5 "White Pine")) |
6558 | 1419 |
1420 (eq needles-per-cluster copy) | |
1421 @result{} nil | |
1422 (equal needles-per-cluster copy) | |
1423 @result{} t | |
1424 (eq (car needles-per-cluster) (car copy)) | |
1425 @result{} nil | |
1426 (cdr (car (cdr needles-per-cluster))) | |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1427 @result{} ("Pitch Pine") |
7734 | 1428 @group |
6558 | 1429 (eq (cdr (car (cdr needles-per-cluster))) |
1430 (cdr (car (cdr copy)))) | |
1431 @result{} t | |
1432 @end group | |
7337
cd57cd335fff
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7118
diff
changeset
|
1433 @end smallexample |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1434 |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1435 This example shows how @code{copy-alist} makes it possible to change |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1436 the associations of one copy without affecting the other: |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1437 |
7337
cd57cd335fff
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7118
diff
changeset
|
1438 @smallexample |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1439 @group |
11137 | 1440 (setcdr (assq 3 copy) '("Martian Vacuum Pine")) |
7118
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1441 (cdr (assq 3 needles-per-cluster)) |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1442 @result{} ("Pitch Pine") |
08d61ef58d13
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6558
diff
changeset
|
1443 @end group |
6558 | 1444 @end smallexample |
1445 @end defun | |
1446 | |
1447 |