aboutsummaryrefslogtreecommitdiffstats
path: root/doc/lispref/lists.texi
diff options
context:
space:
mode:
Diffstat (limited to 'doc/lispref/lists.texi')
-rw-r--r--doc/lispref/lists.texi163
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
35ordered pair. That is, it has two slots, and each slot @dfn{holds}, or 34object 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}, 35and each slot @dfn{holds}, or @dfn{refers to}, some Lisp object. One
37and the other is known as the @sc{cdr}. (These names are traditional; 36slot is known as the @sc{car}, and the other is known as the @sc{cdr}.
38see @ref{Cons Cell Type}.) @sc{cdr} is pronounced ``could-er.'' 37(These names are traditional; see @ref{Cons Cell Type}.) @sc{cdr} is
38pronounced ``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
41its @sc{car} slot currently holds, and likewise for the @sc{cdr}. 41its @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
44cell refers to the next one. There is one cons cell for each element of 44cell refers to the next one. There is one cons cell for each element
45the list. By convention, the @sc{car}s of the cons cells hold the 45of the list. By convention, the @sc{car}s of the cons cells hold the
46elements of the list, and the @sc{cdr}s are used to chain the list: the 46elements 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 48convention; at the level of cons cells, the @sc{car} and @sc{cdr}
49the @sc{car} and the @sc{cdr} is entirely a matter of convention; at the 49slots have similar properties). Hence, the @sc{cdr} slot of each cons
50level of cons cells, the @sc{car} and @sc{cdr} slots have the same 50cell in a list refers to the following cons cell.
51characteristics.
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
55the 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 56symbol and a list with no elements. For convenience, the symbol
58symbol; 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
60as its @sc{car}). Therefore, the @sc{cdr} of a true list is always a 58as its @sc{car}).
61true 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
62elements 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
66neither @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 68printed representation would use dotted pair notation (@pxref{Dotted
68@samp{.}. There is one other possibility: some cons cell's @sc{cdr} 69Pair Notation}). There is one other possibility: some cons cell's
69could 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.
70that structure a @dfn{circular list}. 71We 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,
73circular or dotted. If the program doesn't look far enough down the 74circular or dotted. If a program doesn't look far enough down the
74list to see the @sc{cdr} of the final cons cell, it won't care. 75list to see the @sc{cdr} of the final cons cell, it won't care.
75However, some functions that operate on lists demand true lists and 76However, some functions that operate on lists demand true lists and
76signal errors if given a dotted list. Most functions that try to find 77signal errors if given a dotted list. Most functions that try to find
77the end of a list enter infinite loops if given a circular list. 78the 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 82structure made out of cons cells as a @dfn{list structure}.
82cells.
83
84 The @sc{cdr} of any nonempty true list @var{l} is a list containing all the
85elements of @var{l} except the first.
86
87 @xref{Cons Cell Type}, for the read and print syntax of cons cells and
88lists, 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
257x 251x
258 @result{} (b c) 252 @result{} (b c)
259@end example 253@end example
254
255@noindent
256For 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
695l 693l
696 @result{} (c a b) 694 @result{} (c a b)
697@end example 695@end example
696
697@noindent
698For 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}.
1800compares the @sc{cdr} of each @var{alist} association instead of the 1802compares 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,
1810deletion, rotation, and modulo-indexed reference and traversal.
1811
1812@defun make-ring size
1813This 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
1818This returns @code{t} if @var{object} is a ring, @code{nil} otherwise.
1819@end defun
1820
1821@defun ring-size ring
1822This returns the maximum capacity of the @var{ring}.
1823@end defun
1824
1825@defun ring-length ring
1826This returns the number of objects that @var{ring} currently contains.
1827The value will never exceed that returned by @code{ring-size}.
1828@end defun
1829
1830@defun ring-elements ring
1831This returns a list of the objects in @var{ring}, in order, newest first.
1832@end defun
1833
1834@defun ring-copy ring
1835This returns a new ring which is a copy of @var{ring}.
1836The new ring contains the same (@code{eq}) objects as @var{ring}.
1837@end defun
1838
1839@defun ring-empty-p ring
1840This 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
1844correspond to older elements. Indices are computed modulo the ring
1845length. Index @minus{}1 corresponds to the oldest element, @minus{}2
1846to the next-oldest, and so forth.
1847
1848@defun ring-ref ring index
1849This 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
1855This inserts @var{object} into @var{ring}, making it the newest
1856element, and returns @var{object}.
1857
1858If the ring is full, insertion removes the oldest element to
1859make room for the new element.
1860@end defun
1861
1862@defun ring-remove ring &optional index
1863Remove an object from @var{ring}, and return that object. The
1864argument @var{index} specifies which item to remove; if it is
1865@code{nil}, that means to remove the oldest item. If @var{ring} is
1866empty, @code{ring-remove} signals an error.
1867@end defun
1868
1869@defun ring-insert-at-beginning ring object
1870This inserts @var{object} into @var{ring}, treating it as the oldest
1871element. The return value is not significant.
1872
1873If the ring is full, this function removes the newest element to make
1874room 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
1879use 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