diff options
| author | Richard M. Stallman | 2004-05-22 22:04:56 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 2004-05-22 22:04:56 +0000 |
| commit | d67029478a1418211459bda6e5cba9981665dfbf (patch) | |
| tree | de1d5b667f7d3781974c851e1b3f6354daa6ee5a | |
| parent | 0df043a7066c4dd6ef093b83ec04973ace516a91 (diff) | |
| download | emacs-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.texi | 42 |
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 | |||
| 51 | level of cons cells, the @sc{car} and @sc{cdr} slots have the same | 51 | level of cons cells, the @sc{car} and @sc{cdr} slots have the same |
| 52 | characteristics. | 52 | characteristics. |
| 53 | 53 | ||
| 54 | @cindex true list | ||
| 55 | Since @code{nil} is the conventional value to put in the @sc{cdr} of | ||
| 56 | the 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 | ||
| 59 | symbol; 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 | ||
| 61 | as its @sc{car}). Therefore, the @sc{cdr} of a true list is always a | ||
| 62 | true 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, | ||
| 67 | neither @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} | ||
| 70 | could point to one of the previous cons cells in the list. We call | ||
| 71 | that structure a @dfn{circular list}. | ||
| 72 | |||
| 73 | For some purposes, it does not matter whether a list is true, | ||
| 74 | circular or dotted. If the program doesn't look far enough down the | ||
| 75 | list to see the @sc{cdr} of the final cons cell, it won't care. | ||
| 76 | However, some functions that operate on lists demand true lists and | ||
| 77 | signal errors if given a dotted list. Most functions that try to find | ||
| 78 | the 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 |
| 57 | cells. | 83 | cells. |
| 58 | 84 | ||
| 59 | The symbol @code{nil} is considered a list as well as a symbol; it is | ||
| 60 | the list with no elements. For convenience, the symbol @code{nil} is | ||
| 61 | considered 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 |
| 65 | elements of @var{l} except the first. | 86 | elements 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 |
| 397 | This function returns the length of @var{list}, with no risk | 418 | This function returns the length of @var{list}, with no risk of either |
| 398 | of either an error or an infinite loop. | 419 | an error or an infinite loop. It generally returns the number of |
| 420 | distinct cons cells in the list. However, for circular lists, | ||
| 421 | the value is just an upper bound; it is often too large. | ||
| 399 | 422 | ||
| 400 | If @var{list} is not really a list, @code{safe-length} returns 0. If | 423 | If @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 | 424 | returns 0. |
| 402 | number 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 |