comparison lispref/lists.texi @ 7118:08d61ef58d13

*** empty log message ***
author Richard M. Stallman <rms@gnu.org>
date Tue, 26 Apr 1994 22:08:09 +0000
parents fa8ff07eaafc
children cd57cd335fff
comparison
equal deleted inserted replaced
7117:f1b6a927a442 7118:08d61ef58d13
29 @section Lists and Cons Cells 29 @section Lists and Cons Cells
30 @cindex lists and cons cells 30 @cindex lists and cons cells
31 @cindex @code{nil} and lists 31 @cindex @code{nil} and lists
32 32
33 Lists in Lisp are not a primitive data type; they are built up from 33 Lists in Lisp are not a primitive data type; they are built up from
34 @dfn{cons cells}. A cons cell is a data object which represents an ordered 34 @dfn{cons cells}. A cons cell is a data object that represents an
35 pair. It records two Lisp objects, one labeled as the @sc{car}, and the 35 ordered pair. It records two Lisp objects, one labeled as the @sc{car},
36 other labeled as the @sc{cdr}. These names are traditional; @sc{cdr} is 36 and the other labeled as the @sc{cdr}. These names are traditional; see
37 pronounced ``could-er.'' 37 @ref{Cons Cell Type}. @sc{cdr} is pronounced ``could-er.''
38 38
39 A list is a series of cons cells chained together, one cons cell per 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 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: 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} 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 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 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 45 level of cons cells, the @sc{car} and @sc{cdr} slots have the same
46 characteristics. 46 characteristics.
47
48 @cindex list structure
49 Because most cons cells are used as part of lists, the phrase
50 @dfn{list structure} has come to mean any structure made out of cons
51 cells.
47 52
48 The symbol @code{nil} is considered a list as well as a symbol; it is 53 The symbol @code{nil} is considered a list as well as a symbol; it is
49 the list with no elements. For convenience, the symbol @code{nil} is 54 the list with no elements. For convenience, the symbol @code{nil} is
50 considered to have @code{nil} as its @sc{cdr} (and also as its 55 considered to have @code{nil} as its @sc{cdr} (and also as its
51 @sc{car}). 56 @sc{car}).
132 | | | | | | 137 | | | | | |
133 -------------- ---------------- 138 -------------- ----------------
134 @end group 139 @end group
135 @end example 140 @end example
136 141
137 @xref{List Type}, for the read and print syntax of lists, and for more 142 @xref{Cons Cell Type}, for the read and print syntax of cons cells and
138 ``box and arrow'' illustrations of lists. 143 lists, and for more ``box and arrow'' illustrations of lists.
139 144
140 @node List-related Predicates 145 @node List-related Predicates
141 @section Predicates on Lists 146 @section Predicates on Lists
142 147
143 The following predicates test whether a Lisp object is an atom, is a 148 The following predicates test whether a Lisp object is an atom, is a
153 @defun atom object 158 @defun atom object
154 @cindex atoms 159 @cindex atoms
155 This function returns @code{t} if @var{object} is an atom, @code{nil} 160 This function returns @code{t} if @var{object} is an atom, @code{nil}
156 otherwise. All objects except cons cells are atoms. The symbol 161 otherwise. All objects except cons cells are atoms. The symbol
157 @code{nil} is an atom and is also a list; it is the only Lisp object 162 @code{nil} is an atom and is also a list; it is the only Lisp object
158 which is both. 163 that is both.
159 164
160 @example 165 @example
161 (atom @var{object}) @equiv{} (not (consp @var{object})) 166 (atom @var{object}) @equiv{} (not (consp @var{object}))
162 @end example 167 @end example
163 @end defun 168 @end defun
431 @end defun 436 @end defun
432 437
433 @defun append &rest sequences 438 @defun append &rest sequences
434 @cindex copying lists 439 @cindex copying lists
435 This function returns a list containing all the elements of 440 This function returns a list containing all the elements of
436 @var{sequences}. The @var{sequences} may be lists, vectors, or strings. 441 @var{sequences}. The @var{sequences} may be lists, vectors, or strings,
437 All arguments except the last one are copied, so none of them are 442 but the last one should be a list. All arguments except the last one
438 altered. 443 are copied, so none of them are altered.
439 444
440 The final argument to @code{append} may be any object but it is 445 More generally, the final argument to @code{append} may be any Lisp
441 typically a list. The final argument is not copied or converted; it 446 object. The final argument is not copied or converted; it becomes the
442 becomes part of the structure of the new list. 447 @sc{cdr} of the last cons cell in the new list. If the final argument
443 448 is itself a list, then its elements become in effect elements of the
444 Here is an example: 449 result list. If the final element is not a list, the result is a
450 ``dotted list'' since its final @sc{cdr} is not @code{nil} as required
451 in a true list.
452
453 See @code{nconc} in @ref{Rearrangement}, for a way to join lists with no
454 copying.
455
456 Here is an example of using @code{append}:
445 457
446 @example 458 @example
447 @group 459 @group
448 (setq trees '(pine oak)) 460 (setq trees '(pine oak))
449 @result{} (pine oak) 461 @result{} (pine oak)
461 (eq trees (cdr (cdr more-trees))) 473 (eq trees (cdr (cdr more-trees)))
462 @result{} t 474 @result{} t
463 @end group 475 @end group
464 @end example 476 @end example
465 477
466 You can see what happens by looking at a box diagram. The variable 478 You can see how @code{append} works by looking at a box diagram. The
467 @code{trees} is set to the list @code{(pine oak)} and then the variable 479 variable @code{trees} is set to the list @code{(pine oak)} and then the
468 @code{more-trees} is set to the list @code{(maple birch pine oak)}. 480 variable @code{more-trees} is set to the list @code{(maple birch pine
469 However, the variable @code{trees} continues to refer to the original 481 oak)}. However, the variable @code{trees} continues to refer to the
470 list: 482 original list:
471 483
472 @smallexample 484 @smallexample
473 @group 485 @group
474 more-trees trees 486 more-trees trees
475 | | 487 | |
525 (append) 537 (append)
526 @result{} nil 538 @result{} nil
527 @end group 539 @end group
528 @end example 540 @end example
529 541
530 See @code{nconc} in @ref{Rearrangement}, for a way to join lists with no 542 Here are some examples where the final argument is not a list:
531 copying. 543
544 @example
545 (append '(x y) 'z)
546 @result{} (x y z)
547 (append '(x y) [z])
548 @result{} (x y [z])
549 @end example
550
551 @noindent
552 The second example shows that when the final argument is a sequence but
553 not a list, the sequence's elements do not become elements of the
554 resulting list. Instead, the sequence becomes the final @sc{cdr}, like
555 any other non-list final argument.
532 556
533 Integers are also allowed as arguments to @code{append}. They are 557 Integers are also allowed as arguments to @code{append}. They are
534 converted to strings of digits making up the decimal print 558 converted to strings of digits making up the decimal print
535 representation of the integer, and these strings are then appended. 559 representation of the integer, and these strings are then appended.
536 Here's what happens: 560 Here's what happens:
604 @end menu 628 @end menu
605 629
606 @node Setcar 630 @node Setcar
607 @subsection Altering List Elements with @code{setcar} 631 @subsection Altering List Elements with @code{setcar}
608 632
609 Changing the @sc{car} of a cons cell is done with @code{setcar}, which 633 Changing the @sc{car} of a cons cell is done with @code{setcar}. When
610 replaces one element of a list with a different element. 634 used on a list, @code{setcar} replaces one element of a list with a
635 different element.
611 636
612 @defun setcar cons object 637 @defun setcar cons object
613 This function stores @var{object} as the new @sc{car} of @var{cons}, 638 This function stores @var{object} as the new @sc{car} of @var{cons},
614 replacing its previous @sc{car}. It returns the value @var{object}. 639 replacing its previous @sc{car}. It returns the value @var{object}.
615 For example: 640 For example:
708 @subsection Altering the CDR of a List 733 @subsection Altering the CDR of a List
709 734
710 The lowest-level primitive for modifying a @sc{cdr} is @code{setcdr}: 735 The lowest-level primitive for modifying a @sc{cdr} is @code{setcdr}:
711 736
712 @defun setcdr cons object 737 @defun setcdr cons object
713 This function stores @var{object} into the @sc{cdr} of @var{cons}. The 738 This function stores @var{object} as the new @sc{cdr} of @var{cons},
714 value returned is @var{object}, not @var{cons}. 739 replacing its previous @sc{cdr}. It returns the value @var{object}.
715 @end defun 740 @end defun
716 741
717 Here is an example of replacing the @sc{cdr} of a list with a 742 Here is an example of replacing the @sc{cdr} of a list with a
718 different list. All but the first element of the list are removed in 743 different list. All but the first element of the list are removed in
719 favor of a different sequence of elements. The first element is 744 favor of a different sequence of elements. The first element is
811 Here are some functions that rearrange lists ``destructively'' by 836 Here are some functions that rearrange lists ``destructively'' by
812 modifying the @sc{cdr}s of their component cons cells. We call these 837 modifying the @sc{cdr}s of their component cons cells. We call these
813 functions ``destructive'' because they chew up the original lists passed 838 functions ``destructive'' because they chew up the original lists passed
814 to them as arguments, to produce a new list that is the returned value. 839 to them as arguments, to produce a new list that is the returned value.
815 840
841 @ifinfo
842 See @code{delq}, in @ref{Sets And Lists}, for another function
843 that modifies cons cells.
844 @end ifinfo
845 @iftex
846 The function @code{delq} in the following section is another example
847 of destructive list manipulation.
848 @end iftex
849
816 @defun nconc &rest lists 850 @defun nconc &rest lists
817 @cindex concatenating lists 851 @cindex concatenating lists
818 @cindex joining lists 852 @cindex joining lists
819 This function returns a list containing all the elements of @var{lists}. 853 This function returns a list containing all the elements of @var{lists}.
820 Unlike @code{append} (@pxref{Building Lists}), the @var{lists} are 854 Unlike @code{append} (@pxref{Building Lists}), the @var{lists} are
893 @end defun 927 @end defun
894 928
895 @defun nreverse list 929 @defun nreverse list
896 @cindex reversing a list 930 @cindex reversing a list
897 This function reverses the order of the elements of @var{list}. 931 This function reverses the order of the elements of @var{list}.
898 Unlike @code{reverse}, @code{nreverse} alters its argument destructively 932 Unlike @code{reverse}, @code{nreverse} alters its argument by reversing
899 by reversing the @sc{cdr}s in the cons cells forming the list. The cons 933 the @sc{cdr}s in the cons cells forming the list. The cons cell that
900 cell which used to be the last one in @var{list} becomes the first cell 934 used to be the last one in @var{list} becomes the first cell of the
901 of the value. 935 value.
902 936
903 For example: 937 For example:
904 938
905 @example 939 @example
906 @group 940 @group
964 function would create new cons cells to store the elements in their 998 function would create new cons cells to store the elements in their
965 sorted order. If you wish to make a sorted copy without destroying the 999 sorted order. If you wish to make a sorted copy without destroying the
966 original, copy it first with @code{copy-sequence} and then sort. 1000 original, copy it first with @code{copy-sequence} and then sort.
967 1001
968 Sorting does not change the @sc{car}s of the cons cells in @var{list}; 1002 Sorting does not change the @sc{car}s of the cons cells in @var{list};
1003 each cons cell in the result contains the same element that it contained
1004 before. The result differs from the argument @var{list} because the
1005 cells themselves have been reordered.
1006
1007 Sorting does not change the @sc{car}s of the cons cells in @var{list};
969 the cons cell that originally contained the element @code{a} in 1008 the cons cell that originally contained the element @code{a} in
970 @var{list} still has @code{a} in its @sc{car} after sorting, but it now 1009 @var{list} still has @code{a} in its @sc{car} after sorting, but it now
971 appears in a different position in the list due to the change of 1010 appears in a different position in the list due to the change of
972 @sc{cdr}s. For example: 1011 @sc{cdr}s. For example:
973 1012
1001 @xref{Sorting}, for more functions that perform sorting. 1040 @xref{Sorting}, for more functions that perform sorting.
1002 See @code{documentation} in @ref{Accessing Documentation}, for a 1041 See @code{documentation} in @ref{Accessing Documentation}, for a
1003 useful example of @code{sort}. 1042 useful example of @code{sort}.
1004 @end defun 1043 @end defun
1005 1044
1006 @ifinfo
1007 See @code{delq}, in @ref{Sets And Lists}, for another function
1008 that modifies cons cells.
1009 @end ifinfo
1010 @iftex
1011 The function @code{delq} in the following section is another example
1012 of destructive list manipulation.
1013 @end iftex
1014
1015 @node Sets And Lists 1045 @node Sets And Lists
1016 @section Using Lists as Sets 1046 @section Using Lists as Sets
1017 @cindex lists as sets 1047 @cindex lists as sets
1018 @cindex sets 1048 @cindex sets
1019 1049
1040 The letter @samp{q} in @code{memq} says that it uses @code{eq} to 1070 The letter @samp{q} in @code{memq} says that it uses @code{eq} to
1041 compare @var{object} against the elements of the list. For example: 1071 compare @var{object} against the elements of the list. For example:
1042 1072
1043 @example 1073 @example
1044 @group 1074 @group
1045 (memq 2 '(1 2 3 2 1)) 1075 (memq 'b '(a b c b a))
1046 @result{} (2 3 2 1) 1076 @result{} (b c b a)
1047 @end group 1077 @end group
1048 @group 1078 @group
1049 (memq '(2) '((1) (2))) ; @r{@code{(2)} and @code{(2)} are not @code{eq}.} 1079 (memq '(2) '((1) (2))) ; @r{@code{(2)} and @code{(2)} are not @code{eq}.}
1050 @result{} nil 1080 @result{} nil
1051 @end group 1081 @end group
1073 When an element to be deleted appears in the middle of the list, 1103 When an element to be deleted appears in the middle of the list,
1074 removing it involves changing the @sc{cdr}s (@pxref{Setcdr}). 1104 removing it involves changing the @sc{cdr}s (@pxref{Setcdr}).
1075 1105
1076 @example 1106 @example
1077 @group 1107 @group
1078 (setq sample-list '(1 2 3 (4))) 1108 (setq sample-list '(a b c (4)))
1079 @result{} (1 2 3 (4)) 1109 @result{} (a b c (4))
1080 @end group 1110 @end group
1081 @group 1111 @group
1082 (delq 1 sample-list) 1112 (delq 'a sample-list)
1083 @result{} (2 3 (4)) 1113 @result{} (b c (4))
1084 @end group 1114 @end group
1085 @group 1115 @group
1086 sample-list 1116 sample-list
1087 @result{} (1 2 3 (4)) 1117 @result{} (a b c (4))
1088 @end group 1118 @end group
1089 @group 1119 @group
1090 (delq 2 sample-list) 1120 (delq 'c sample-list)
1091 @result{} (1 3 (4)) 1121 @result{} (a c (4))
1092 @end group 1122 @end group
1093 @group 1123 @group
1094 sample-list 1124 sample-list
1095 @result{} (1 3 (4)) 1125 @result{} (a c (4))
1096 @end group 1126 @end group
1097 @end example 1127 @end example
1098 1128
1099 Note that @code{(delq 2 sample-list)} modifies @code{sample-list} to 1129 Note that @code{(delq 'b sample-list)} modifies @code{sample-list} to
1100 splice out the second element, but @code{(delq 1 sample-list)} does not 1130 splice out the second element, but @code{(delq 'a sample-list)} does not
1101 splice anything---it just returns a shorter list. Don't assume that a 1131 splice anything---it just returns a shorter list. Don't assume that a
1102 variable which formerly held the argument @var{list} now has fewer 1132 variable which formerly held the argument @var{list} now has fewer
1103 elements, or that it still holds the original list! Instead, save the 1133 elements, or that it still holds the original list! Instead, save the
1104 result of @code{delq} and use that. Most often we store the result back 1134 result of @code{delq} and use that. Most often we store the result back
1105 into the variable that held the original list: 1135 into the variable that held the original list:
1112 and the @code{(4)} in the @code{sample-list} are not @code{eq}: 1142 and the @code{(4)} in the @code{sample-list} are not @code{eq}:
1113 1143
1114 @example 1144 @example
1115 @group 1145 @group
1116 (delq '(4) sample-list) 1146 (delq '(4) sample-list)
1117 @result{} (1 3 (4)) 1147 @result{} (a c (4))
1118 @end group 1148 @end group
1119 @end example 1149 @end example
1120 1150
1121 The following two functions are like @code{memq} and @code{delq} but use 1151 The following two functions are like @code{memq} and @code{delq} but use
1122 @code{equal} rather than @code{eq} to compare elements. They are new in 1152 @code{equal} rather than @code{eq} to compare elements. They are new in
1256 @result{} acorns 1286 @result{} acorns
1257 (assoc 'birch trees) 1287 (assoc 'birch trees)
1258 @result{} nil 1288 @result{} nil
1259 @end smallexample 1289 @end smallexample
1260 1290
1261 Here is another example in which the keys and values are not symbols: 1291 Here is another example, in which the keys and values are not symbols:
1262 1292
1263 @smallexample 1293 @smallexample
1264 (setq needles-per-cluster 1294 (setq needles-per-cluster
1265 '((2 "Austrian Pine" "Red Pine") 1295 '((2 "Austrian Pine" "Red Pine")
1266 (3 "Pitch Pine") 1296 (3 "Pitch Pine")
1351 1381
1352 @smallexample 1382 @smallexample
1353 @group 1383 @group
1354 (setq needles-per-cluster 1384 (setq needles-per-cluster
1355 '((2 . ("Austrian Pine" "Red Pine")) 1385 '((2 . ("Austrian Pine" "Red Pine"))
1356 (3 . "Pitch Pine") 1386 (3 . ("Pitch Pine"))
1357 (5 . "White Pine"))) 1387 (5 . ("White Pine"))))
1358 @result{} 1388 @result{}
1359 ((2 "Austrian Pine" "Red Pine") 1389 ((2 "Austrian Pine" "Red Pine")
1360 (3 . "Pitch Pine") 1390 (3 "Pitch Pine")
1361 (5 . "White Pine")) 1391 (5 "White Pine"))
1362 1392
1363 (setq copy (copy-alist needles-per-cluster)) 1393 (setq copy (copy-alist needles-per-cluster))
1364 @result{} 1394 @result{}
1365 ((2 "Austrian Pine" "Red Pine") 1395 ((2 "Austrian Pine" "Red Pine")
1366 (3 . "Pitch Pine") 1396 (3 "Pitch Pine")
1367 (5 . "White Pine")) 1397 (5 "White Pine"))
1368 1398
1369 (eq needles-per-cluster copy) 1399 (eq needles-per-cluster copy)
1370 @result{} nil 1400 @result{} nil
1371 (equal needles-per-cluster copy) 1401 (equal needles-per-cluster copy)
1372 @result{} t 1402 @result{} t
1373 (eq (car needles-per-cluster) (car copy)) 1403 (eq (car needles-per-cluster) (car copy))
1374 @result{} nil 1404 @result{} nil
1375 (cdr (car (cdr needles-per-cluster))) 1405 (cdr (car (cdr needles-per-cluster)))
1376 @result{} "Pitch Pine" 1406 @result{} ("Pitch Pine")
1377 (eq (cdr (car (cdr needles-per-cluster))) 1407 (eq (cdr (car (cdr needles-per-cluster)))
1378 (cdr (car (cdr copy)))) 1408 (cdr (car (cdr copy))))
1379 @result{} t 1409 @result{} t
1380 @end group 1410 @end group
1411 @end example
1412
1413 This example shows how @code{copy-alist} makes it possible to change
1414 the associations of one copy without affecting the other:
1415
1416 @example
1417 @group
1418 (setcdr (assq 3 needles-per-cluster)
1419 '("Martian Vacuum Pine"))
1420 (cdr (assq 3 needles-per-cluster))
1421 @result{} ("Pitch Pine")
1422 @end group
1381 @end smallexample 1423 @end smallexample
1382 @end defun 1424 @end defun
1383 1425
1384 1426