aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman2004-05-22 22:04:56 +0000
committerRichard M. Stallman2004-05-22 22:04:56 +0000
commitd67029478a1418211459bda6e5cba9981665dfbf (patch)
treede1d5b667f7d3781974c851e1b3f6354daa6ee5a
parent0df043a7066c4dd6ef093b83ec04973ace516a91 (diff)
downloademacs-d67029478a1418211459bda6e5cba9981665dfbf.tar.gz
emacs-d67029478a1418211459bda6e5cba9981665dfbf.zip
(Cons Cells): Explain dotted lists, true lists, circular lists.
(List Elements): Explain handling of circular and dotted lists.
-rw-r--r--lispref/lists.texi42
1 files changed, 32 insertions, 10 deletions
diff --git a/lispref/lists.texi b/lispref/lists.texi
index 7c369633c2e..2aa3c40b0e5 100644
--- a/lispref/lists.texi
+++ b/lispref/lists.texi
@@ -51,16 +51,37 @@ the @sc{car} and the @sc{cdr} is entirely a matter of convention; at the
51level of cons cells, the @sc{car} and @sc{cdr} slots have the same 51level of cons cells, the @sc{car} and @sc{cdr} slots have the same
52characteristics. 52characteristics.
53 53
54@cindex true list
55 Since @code{nil} is the conventional value to put in the @sc{cdr} of
56the last cons cell in the list, we call that case a @dfn{true list}.
57
58 In Lisp, we consider the symbol @code{nil} a list as well as a
59symbol; it is the list with no elements. For convenience, the symbol
60@code{nil} is considered to have @code{nil} as its @sc{cdr} (and also
61as its @sc{car}). Therefore, the @sc{cdr} of a true list is always a
62true list.
63
64@cindex dotted list
65@cindex circular list
66 If the @sc{cdr} of a list's last cons cell is some other value,
67neither @code{nil} nor another cons cell, we call the structure a
68@dfn{dotted list}, since its printed representation would use
69@samp{.}. There is one other possibility: some cons cell's @sc{cdr}
70could point to one of the previous cons cells in the list. We call
71that structure a @dfn{circular list}.
72
73 For some purposes, it does not matter whether a list is true,
74circular or dotted. If the program doesn't look far enough down the
75list to see the @sc{cdr} of the final cons cell, it won't care.
76However, some functions that operate on lists demand true lists and
77signal errors if given a dotted list. Most functions that try to find
78the end of a list enter infinite loops if given a circular list.
79
54@cindex list structure 80@cindex list structure
55 Because most cons cells are used as part of lists, the phrase 81 Because most cons cells are used as part of lists, the phrase
56@dfn{list structure} has come to mean any structure made out of cons 82@dfn{list structure} has come to mean any structure made out of cons
57cells. 83cells.
58 84
59 The symbol @code{nil} is considered a list as well as a symbol; it is
60the list with no elements. For convenience, the symbol @code{nil} is
61considered to have @code{nil} as its @sc{cdr} (and also as its
62@sc{car}).
63
64 The @sc{cdr} of any nonempty list @var{l} is a list containing all the 85 The @sc{cdr} of any nonempty list @var{l} is a list containing all the
65elements of @var{l} except the first. 86elements of @var{l} except the first.
66 87
@@ -394,12 +415,13 @@ if @var{n} is bigger than @var{list}'s length.
394 415
395@anchor{Definition of safe-length} 416@anchor{Definition of safe-length}
396@defun safe-length list 417@defun safe-length list
397This function returns the length of @var{list}, with no risk 418This function returns the length of @var{list}, with no risk of either
398of either an error or an infinite loop. 419an error or an infinite loop. It generally returns the number of
420distinct cons cells in the list. However, for circular lists,
421the value is just an upper bound; it is often too large.
399 422
400If @var{list} is not really a list, @code{safe-length} returns 0. If 423If @var{list} is not @code{nil} or a cons cell, @code{safe-length}
401@var{list} is circular, it returns a finite value which is at least the 424returns 0.
402number of distinct elements.
403@end defun 425@end defun
404 426
405 The most common way to compute the length of a list, when you are not 427 The most common way to compute the length of a list, when you are not