diff options
| author | Joakim Verona | 2012-01-23 15:10:06 +0100 |
|---|---|---|
| committer | Joakim Verona | 2012-01-23 15:10:06 +0100 |
| commit | 0322b140eead7c94de7f0f6d19a90bd15690b4eb (patch) | |
| tree | 950c011783cc896d0450084cb5155e54548bfe5b /doc/lispref/lists.texi | |
| parent | d5114bfea3ea4c37c57e2af0f3b095be9fcd8bac (diff) | |
| parent | cb5850f27c1b4d26957d58e2da2314dd12498671 (diff) | |
| download | emacs-0322b140eead7c94de7f0f6d19a90bd15690b4eb.tar.gz emacs-0322b140eead7c94de7f0f6d19a90bd15690b4eb.zip | |
upstream
Diffstat (limited to 'doc/lispref/lists.texi')
| -rw-r--r-- | doc/lispref/lists.texi | 163 |
1 files changed, 39 insertions, 124 deletions
diff --git a/doc/lispref/lists.texi b/doc/lispref/lists.texi index eb9ddf58603..c8433c79b54 100644 --- a/doc/lispref/lists.texi +++ b/doc/lispref/lists.texi | |||
| @@ -23,7 +23,6 @@ the whole list. | |||
| 23 | * Modifying Lists:: Storing new pieces into an existing list. | 23 | * Modifying Lists:: Storing new pieces into an existing list. |
| 24 | * Sets And Lists:: A list can represent a finite mathematical set. | 24 | * Sets And Lists:: A list can represent a finite mathematical set. |
| 25 | * Association Lists:: A list can represent a finite relation or mapping. | 25 | * Association Lists:: A list can represent a finite relation or mapping. |
| 26 | * Rings:: Managing a fixed-size ring of objects. | ||
| 27 | @end menu | 26 | @end menu |
| 28 | 27 | ||
| 29 | @node Cons Cells | 28 | @node Cons Cells |
| @@ -31,61 +30,56 @@ the whole list. | |||
| 31 | @cindex lists and cons cells | 30 | @cindex lists and cons cells |
| 32 | 31 | ||
| 33 | Lists in Lisp are not a primitive data type; they are built up from | 32 | 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 | 33 | @dfn{cons cells} (@pxref{Cons Cell Type}). A cons cell is a data |
| 35 | ordered pair. That is, it has two slots, and each slot @dfn{holds}, or | 34 | object that represents an ordered pair. That is, it has two slots, |
| 36 | @dfn{refers to}, some Lisp object. One slot is known as the @sc{car}, | 35 | and each slot @dfn{holds}, or @dfn{refers to}, some Lisp object. One |
| 37 | and the other is known as the @sc{cdr}. (These names are traditional; | 36 | slot is known as the @sc{car}, and the other is known as the @sc{cdr}. |
| 38 | see @ref{Cons Cell Type}.) @sc{cdr} is pronounced ``could-er.'' | 37 | (These names are traditional; see @ref{Cons Cell Type}.) @sc{cdr} is |
| 38 | pronounced ``could-er.'' | ||
| 39 | 39 | ||
| 40 | We say that ``the @sc{car} of this cons cell is'' whatever object | 40 | We say that ``the @sc{car} of this cons cell is'' whatever object |
| 41 | its @sc{car} slot currently holds, and likewise for the @sc{cdr}. | 41 | its @sc{car} slot currently holds, and likewise for the @sc{cdr}. |
| 42 | 42 | ||
| 43 | A list is a series of cons cells ``chained together,'' so that each | 43 | A list is a series of cons cells ``chained together,'' so that each |
| 44 | cell refers to the next one. There is one cons cell for each element of | 44 | cell refers to the next one. There is one cons cell for each element |
| 45 | the list. By convention, the @sc{car}s of the cons cells hold the | 45 | of the list. By convention, the @sc{car}s of the cons cells hold the |
| 46 | elements of the list, and the @sc{cdr}s are used to chain the list: the | 46 | elements of the list, and the @sc{cdr}s are used to chain the list |
| 47 | @sc{cdr} slot of each cons cell refers to the following cons cell. The | 47 | (this asymmetry between @sc{car} and @sc{cdr} is entirely a matter of |
| 48 | @sc{cdr} of the last cons cell is @code{nil}. This asymmetry between | 48 | convention; at the level of cons cells, the @sc{car} and @sc{cdr} |
| 49 | the @sc{car} and the @sc{cdr} is entirely a matter of convention; at the | 49 | slots have similar properties). Hence, the @sc{cdr} slot of each cons |
| 50 | level of cons cells, the @sc{car} and @sc{cdr} slots have the same | 50 | cell in a list refers to the following cons cell. |
| 51 | characteristics. | ||
| 52 | 51 | ||
| 53 | @cindex true list | 52 | @cindex true list |
| 54 | Since @code{nil} is the conventional value to put in the @sc{cdr} of | 53 | Also by convention, the @sc{cdr} of the last cons cell in a list is |
| 55 | the last cons cell in the list, we call that case a @dfn{true list}. | 54 | @code{nil}. We call such a @code{nil}-terminated structure a |
| 56 | 55 | @dfn{true list}. In Emacs Lisp, the symbol @code{nil} is both a | |
| 57 | In Lisp, we consider the symbol @code{nil} a list as well as a | 56 | symbol and a list with no elements. For convenience, the symbol |
| 58 | symbol; it is the list with no elements. For convenience, the symbol | ||
| 59 | @code{nil} is considered to have @code{nil} as its @sc{cdr} (and also | 57 | @code{nil} is considered to have @code{nil} as its @sc{cdr} (and also |
| 60 | as its @sc{car}). Therefore, the @sc{cdr} of a true list is always a | 58 | as its @sc{car}). |
| 61 | true list. | 59 | |
| 60 | Hence, the @sc{cdr} of a true list is always a true list. The | ||
| 61 | @sc{cdr} of a nonempty true list is a true list containing all the | ||
| 62 | elements except the first. | ||
| 62 | 63 | ||
| 63 | @cindex dotted list | 64 | @cindex dotted list |
| 64 | @cindex circular list | 65 | @cindex circular list |
| 65 | If the @sc{cdr} of a list's last cons cell is some other value, | 66 | If the @sc{cdr} of a list's last cons cell is some value other than |
| 66 | neither @code{nil} nor another cons cell, we call the structure a | 67 | @code{nil}, we call the structure a @dfn{dotted list}, since its |
| 67 | @dfn{dotted list}, since its printed representation would use | 68 | printed representation would use dotted pair notation (@pxref{Dotted |
| 68 | @samp{.}. There is one other possibility: some cons cell's @sc{cdr} | 69 | Pair Notation}). There is one other possibility: some cons cell's |
| 69 | could point to one of the previous cons cells in the list. We call | 70 | @sc{cdr} could point to one of the previous cons cells in the list. |
| 70 | that structure a @dfn{circular list}. | 71 | We call that structure a @dfn{circular list}. |
| 71 | 72 | ||
| 72 | For some purposes, it does not matter whether a list is true, | 73 | For some purposes, it does not matter whether a list is true, |
| 73 | circular or dotted. If the program doesn't look far enough down the | 74 | circular or dotted. If a program doesn't look far enough down the |
| 74 | list to see the @sc{cdr} of the final cons cell, it won't care. | 75 | list to see the @sc{cdr} of the final cons cell, it won't care. |
| 75 | However, some functions that operate on lists demand true lists and | 76 | However, some functions that operate on lists demand true lists and |
| 76 | signal errors if given a dotted list. Most functions that try to find | 77 | signal errors if given a dotted list. Most functions that try to find |
| 77 | the end of a list enter infinite loops if given a circular list. | 78 | the end of a list enter infinite loops if given a circular list. |
| 78 | 79 | ||
| 79 | @cindex list structure | 80 | @cindex list structure |
| 80 | Because most cons cells are used as part of lists, the phrase | 81 | Because most cons cells are used as part of lists, we refer to any |
| 81 | @dfn{list structure} has come to mean any structure made out of cons | 82 | structure made out of cons cells as a @dfn{list structure}. |
| 82 | cells. | ||
| 83 | |||
| 84 | The @sc{cdr} of any nonempty true list @var{l} is a list containing all the | ||
| 85 | elements of @var{l} except the first. | ||
| 86 | |||
| 87 | @xref{Cons Cell Type}, for the read and print syntax of cons cells and | ||
| 88 | lists, and for ``box and arrow'' illustrations of lists. | ||
| 89 | 83 | ||
| 90 | @node List-related Predicates | 84 | @node List-related Predicates |
| 91 | @section Predicates on Lists | 85 | @section Predicates on Lists |
| @@ -257,6 +251,10 @@ x | |||
| 257 | x | 251 | x |
| 258 | @result{} (b c) | 252 | @result{} (b c) |
| 259 | @end example | 253 | @end example |
| 254 | |||
| 255 | @noindent | ||
| 256 | For the @code{pop} macro, which removes an element from a list, | ||
| 257 | @xref{List Variables}. | ||
| 260 | @end defmac | 258 | @end defmac |
| 261 | 259 | ||
| 262 | @defun nth n list | 260 | @defun nth n list |
| @@ -695,6 +693,10 @@ This macro provides an alternative way to write | |||
| 695 | l | 693 | l |
| 696 | @result{} (c a b) | 694 | @result{} (c a b) |
| 697 | @end example | 695 | @end example |
| 696 | |||
| 697 | @noindent | ||
| 698 | For the @code{pop} macro, which removes the first element from a list, | ||
| 699 | @xref{List Elements}. | ||
| 698 | @end defmac | 700 | @end defmac |
| 699 | 701 | ||
| 700 | Two functions modify lists that are the values of variables. | 702 | Two functions modify lists that are the values of variables. |
| @@ -1800,90 +1802,3 @@ often modifies the original list structure of @var{alist}. | |||
| 1800 | compares the @sc{cdr} of each @var{alist} association instead of the | 1802 | compares the @sc{cdr} of each @var{alist} association instead of the |
| 1801 | @sc{car}. | 1803 | @sc{car}. |
| 1802 | @end defun | 1804 | @end defun |
| 1803 | |||
| 1804 | @node Rings | ||
| 1805 | @section Managing a Fixed-Size Ring of Objects | ||
| 1806 | |||
| 1807 | @cindex ring data structure | ||
| 1808 | This section describes functions for operating on rings. A | ||
| 1809 | @dfn{ring} is a fixed-size data structure that supports insertion, | ||
| 1810 | deletion, rotation, and modulo-indexed reference and traversal. | ||
| 1811 | |||
| 1812 | @defun make-ring size | ||
| 1813 | This returns a new ring capable of holding @var{size} objects. | ||
| 1814 | @var{size} should be an integer. | ||
| 1815 | @end defun | ||
| 1816 | |||
| 1817 | @defun ring-p object | ||
| 1818 | This returns @code{t} if @var{object} is a ring, @code{nil} otherwise. | ||
| 1819 | @end defun | ||
| 1820 | |||
| 1821 | @defun ring-size ring | ||
| 1822 | This returns the maximum capacity of the @var{ring}. | ||
| 1823 | @end defun | ||
| 1824 | |||
| 1825 | @defun ring-length ring | ||
| 1826 | This returns the number of objects that @var{ring} currently contains. | ||
| 1827 | The value will never exceed that returned by @code{ring-size}. | ||
| 1828 | @end defun | ||
| 1829 | |||
| 1830 | @defun ring-elements ring | ||
| 1831 | This returns a list of the objects in @var{ring}, in order, newest first. | ||
| 1832 | @end defun | ||
| 1833 | |||
| 1834 | @defun ring-copy ring | ||
| 1835 | This returns a new ring which is a copy of @var{ring}. | ||
| 1836 | The new ring contains the same (@code{eq}) objects as @var{ring}. | ||
| 1837 | @end defun | ||
| 1838 | |||
| 1839 | @defun ring-empty-p ring | ||
| 1840 | This returns @code{t} if @var{ring} is empty, @code{nil} otherwise. | ||
| 1841 | @end defun | ||
| 1842 | |||
| 1843 | The newest element in the ring always has index 0. Higher indices | ||
| 1844 | correspond to older elements. Indices are computed modulo the ring | ||
| 1845 | length. Index @minus{}1 corresponds to the oldest element, @minus{}2 | ||
| 1846 | to the next-oldest, and so forth. | ||
| 1847 | |||
| 1848 | @defun ring-ref ring index | ||
| 1849 | This returns the object in @var{ring} found at index @var{index}. | ||
| 1850 | @var{index} may be negative or greater than the ring length. If | ||
| 1851 | @var{ring} is empty, @code{ring-ref} signals an error. | ||
| 1852 | @end defun | ||
| 1853 | |||
| 1854 | @defun ring-insert ring object | ||
| 1855 | This inserts @var{object} into @var{ring}, making it the newest | ||
| 1856 | element, and returns @var{object}. | ||
| 1857 | |||
| 1858 | If the ring is full, insertion removes the oldest element to | ||
| 1859 | make room for the new element. | ||
| 1860 | @end defun | ||
| 1861 | |||
| 1862 | @defun ring-remove ring &optional index | ||
| 1863 | Remove an object from @var{ring}, and return that object. The | ||
| 1864 | argument @var{index} specifies which item to remove; if it is | ||
| 1865 | @code{nil}, that means to remove the oldest item. If @var{ring} is | ||
| 1866 | empty, @code{ring-remove} signals an error. | ||
| 1867 | @end defun | ||
| 1868 | |||
| 1869 | @defun ring-insert-at-beginning ring object | ||
| 1870 | This inserts @var{object} into @var{ring}, treating it as the oldest | ||
| 1871 | element. The return value is not significant. | ||
| 1872 | |||
| 1873 | If the ring is full, this function removes the newest element to make | ||
| 1874 | room for the inserted element. | ||
| 1875 | @end defun | ||
| 1876 | |||
| 1877 | @cindex fifo data structure | ||
| 1878 | If you are careful not to exceed the ring size, you can | ||
| 1879 | use the ring as a first-in-first-out queue. For example: | ||
| 1880 | |||
| 1881 | @lisp | ||
| 1882 | (let ((fifo (make-ring 5))) | ||
| 1883 | (mapc (lambda (obj) (ring-insert fifo obj)) | ||
| 1884 | '(0 one "two")) | ||
| 1885 | (list (ring-remove fifo) t | ||
| 1886 | (ring-remove fifo) t | ||
| 1887 | (ring-remove fifo))) | ||
| 1888 | @result{} (0 t one t "two") | ||
| 1889 | @end lisp | ||