comparison lispref/lists.texi @ 22253:ed6b191416cf

*** empty log message ***
author Richard M. Stallman <rms@gnu.org>
date Tue, 26 May 1998 20:36:22 +0000
parents d4ac295a98b3
children 71f954d59214
comparison
equal deleted inserted replaced
22252:40089afa2b1d 22253:ed6b191416cf
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 that represents an 34 @dfn{cons cells}. A cons cell is a data object that represents an
35 ordered pair. It records two Lisp objects, one labeled as the @sc{car}, 35 ordered pair. It holds, or ``points to,'' two Lisp objects, one labeled
36 and the other labeled as the @sc{cdr}. These names are traditional; see 36 as the @sc{car}, and the other labeled as the @sc{cdr}. These names are
37 @ref{Cons Cell Type}. @sc{cdr} is pronounced ``could-er.'' 37 traditional; see @ref{Cons Cell Type}. @sc{cdr} is pronounced
38 ``could-er.''
38 39
39 A list is a series of cons cells chained together, one cons cell per 40 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 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 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 the @sc{cdr} of each cons cell is the following cons cell. The @sc{cdr}
80 @end group 81 @end group
81 @end example 82 @end example
82 83
83 Each pair of boxes represents a cons cell. Each box ``refers to'', 84 Each pair of boxes represents a cons cell. Each box ``refers to'',
84 ``points to'' or ``contains'' a Lisp object. (These terms are 85 ``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 synonymous.) The first box, which describes the @sc{car} of the first
86 cell, contains the symbol @code{tulip}. The arrow from the @sc{cdr} of 87 cons cell, contains the symbol @code{tulip}. The arrow from the
87 the first cons cell to the second cons cell indicates that the @sc{cdr} 88 @sc{cdr} box of the first cons cell to the second cons cell indicates
88 of the first cons cell points to the second cons cell. 89 that the @sc{cdr} of the first cons cell is the second cons cell.
89 90
90 The same list can be illustrated in a different sort of box notation 91 The same list can be illustrated in a different sort of box notation
91 like this: 92 like this:
92 93
93 @example 94 @example
666 used on a list, @code{setcar} replaces one element of a list with a 667 used on a list, @code{setcar} replaces one element of a list with a
667 different element. 668 different element.
668 669
669 @defun setcar cons object 670 @defun setcar cons object
670 This function stores @var{object} as the new @sc{car} of @var{cons}, 671 This function stores @var{object} as the new @sc{car} of @var{cons},
671 replacing its previous @sc{car}. It returns the value @var{object}. 672 replacing its previous @sc{car}. In other words, it changes the
672 For example: 673 @sc{car} slot of @var{cons} to point to @var{object}. It returns the
674 value @var{object}. For example:
673 675
674 @example 676 @example
675 @group 677 @group
676 (setq x '(1 2)) 678 (setq x '(1 2))
677 @result{} (1 2) 679 @result{} (1 2)
768 770
769 The lowest-level primitive for modifying a @sc{cdr} is @code{setcdr}: 771 The lowest-level primitive for modifying a @sc{cdr} is @code{setcdr}:
770 772
771 @defun setcdr cons object 773 @defun setcdr cons object
772 This function stores @var{object} as the new @sc{cdr} of @var{cons}, 774 This function stores @var{object} as the new @sc{cdr} of @var{cons},
773 replacing its previous @sc{cdr}. It returns the value @var{object}. 775 replacing its previous @sc{cdr}. In other words, it changes the
776 @sc{cdr} slot of @var{cons} to point to @var{object}. It returns the
777 value @var{object}.
774 @end defun 778 @end defun
775 779
776 Here is an example of replacing the @sc{cdr} of a list with a 780 Here is an example of replacing the @sc{cdr} of a list with a
777 different list. All but the first element of the list are removed in 781 different list. All but the first element of the list are removed in
778 favor of a different sequence of elements. The first element is 782 favor of a different sequence of elements. The first element is
795 @end example 799 @end example
796 800
797 You can delete elements from the middle of a list by altering the 801 You can delete elements from the middle of a list by altering the
798 @sc{cdr}s of the cons cells in the list. For example, here we delete 802 @sc{cdr}s of the cons cells in the list. For example, here we delete
799 the second element, @code{b}, from the list @code{(a b c)}, by changing 803 the second element, @code{b}, from the list @code{(a b c)}, by changing
800 the @sc{cdr} of the first cell: 804 the @sc{cdr} of the first cons cell:
801 805
802 @example 806 @example
803 @group 807 @group
804 (setq x1 '(a b c)) 808 (setq x1 '(a b c))
805 @result{} (a b c) 809 @result{} (a b c)
966 @defun nreverse list 970 @defun nreverse list
967 @cindex reversing a list 971 @cindex reversing a list
968 This function reverses the order of the elements of @var{list}. 972 This function reverses the order of the elements of @var{list}.
969 Unlike @code{reverse}, @code{nreverse} alters its argument by reversing 973 Unlike @code{reverse}, @code{nreverse} alters its argument by reversing
970 the @sc{cdr}s in the cons cells forming the list. The cons cell that 974 the @sc{cdr}s in the cons cells forming the list. The cons cell that
971 used to be the last one in @var{list} becomes the first cell of the 975 used to be the last one in @var{list} becomes the first cons cell of the
972 value. 976 value.
973 977
974 For example: 978 For example:
975 979
976 @example 980 @example
983 @result{} (1 2 3 4) 987 @result{} (1 2 3 4)
984 (nreverse x) 988 (nreverse x)
985 @result{} (4 3 2 1) 989 @result{} (4 3 2 1)
986 @end group 990 @end group
987 @group 991 @group
988 ;; @r{The cell that was first is now last.} 992 ;; @r{The cons cell that was first is now last.}
989 x 993 x
990 @result{} (1) 994 @result{} (1)
991 @end group 995 @end group
992 @end example 996 @end example
993 997
1247 @cindex association list 1251 @cindex association list
1248 @cindex alist 1252 @cindex alist
1249 1253
1250 An @dfn{association list}, or @dfn{alist} for short, records a mapping 1254 An @dfn{association list}, or @dfn{alist} for short, records a mapping
1251 from keys to values. It is a list of cons cells called 1255 from keys to values. It is a list of cons cells called
1252 @dfn{associations}: the @sc{car} of each cell is the @dfn{key}, and the 1256 @dfn{associations}: the @sc{car} of each cons cell is the @dfn{key}, and the
1253 @sc{cdr} is the @dfn{associated value}.@footnote{This usage of ``key'' 1257 @sc{cdr} is the @dfn{associated value}.@footnote{This usage of ``key''
1254 is not related to the term ``key sequence''; it means a value used to 1258 is not related to the term ``key sequence''; it means a value used to
1255 look up an item in a table. In this case, the table is the alist, and 1259 look up an item in a table. In this case, the table is the alist, and
1256 the alist associations are the items.} 1260 the alist associations are the items.}
1257 1261