Debian Codegraph @

doc/extremum.html

raw
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <HTML>
3 <HEAD>
4 <TITLE>Function EXTREMUM, EXTREMA, N-MOST-EXTREME</TITLE>
5 <LINK REL="stylesheet" HREF="style.css" type="text/css">
6 </HEAD>
7 <BODY>
8
9 <p><p><i>Function</i> <b>EXTREMUM, EXTREMA, N-MOST-EXTREME</b></a></a> <p>
10 <p><b>Syntax:</b><p>
11
12 <p>
13
14 <p><b>extremum</b> <i>sequence predicate <tt>&key</tt> key (start 0) end</i> => <i>morally-smallest-element</i><p>
15 <p><b>extrema</b> <i>sequence predicate <tt>&key</tt> key (start 0) end</i> => <i>morally-smallest-elements</i><p>
16 <p><b>n-most-extreme</b> <i>n sequence predicate <tt>&key</tt> key (start 0) end</i> => <i>n-smallest-elements</i><p>
17 <p>
18 <p><b>Arguments and Values:</b><p>
19 <p>
20 <i>sequence</i>---a <i>proper sequence</i></a>. <p>
21
22 <i>predicate</i>---a <i>designator</i> for a <i>function</i> of two
23 arguments that returns a <i>generalized boolean</i>. <p>
24
25 <i>key</i>---a <i>designator</i> for a <i>function</i> of one
26 argument, or <b>nil</b>. <p>
27
28 <i>start, end</i>---bounding index designators of <i>sequence</i>. The
29 defaults for start and end are 0 and <b>nil</b>, respectively.<p>
30
31 <i>morally-smallest-element</i>---the element of <i>sequence</i> that
32 would appear first if the sequence were ordered according to <a
33 class="hyperspec" href ="
34 http://www.lispworks.com/documentation/HyperSpec/Body/fun_sortcm_stable-sort.html"><b>sort</b></a>
35 using <i>predicate</i> and <i>key</i>
36
37 <p><i>morally-smallest-elements</i>---the identical elements of
38 <i>sequence</i> that would appear first if the sequence were ordered
39 according to <a class="hyperspec" href ="
40 http://www.lispworks.com/documentation/HyperSpec/Body/fun_sortcm_stable-sort.html"><b>sort</b></a>
41 using <i>predicate</i> and <i>key</i>. If <i>predicate</i> states that
42 neither of two objects is before the other, they are considered
43 identical.
44
45 <i>n</i>---a positive integer<p>
46
47 <i>n-smallest-elements</i>---the <i>n</i> elements of <i>sequence</i> that
48 would appear first if the sequence were ordered according to <a
49 class="hyperspec" href ="
50 http://www.lispworks.com/documentation/HyperSpec/Body/fun_sortcm_stable-sort.html"><b>sort</b></a>
51 using <i>predicate</i> and <i>key</i>
52
53 <p>
54 <p><b>Description:</b><p>
55 <p>
56 <b>extremum</b> returns the element of <i>sequence</i> that would
57 appear first if the subsequence of <i>sequence</i> specified by
58 <i>start</i> and <i>end</i> were ordered according to <a
59 class="hyperspec" href ="
60 http://www.lispworks.com/documentation/HyperSpec/Body/fun_sortcm_stable-sort.html"><b>sort</b></a>
61 using <i>predicate</i> and <i>key</i>.
62
63
64 <p><p><b>extremum</b> determines the relationship between two elements
65 by giving keys extracted from the elements to the
66 <i>predicate</i>. The first argument to the <i>predicate</i> function
67 is the part of one element of <i>sequence</i> extracted by the
68 <i>key</i> function (if supplied); the second argument is the part of
69 another element of <i>sequence</i> extracted by the <i>key</i>
70 function (if supplied). <i>Predicate</i> should return <i>true</i> if
71 and only if the first argument is strictly less than the second (in
72 some appropriate sense). If the first argument is greater than or
73 equal to the second (in the appropriate sense), then the
74 <i>predicate</i> should return <i>false</i>. <p>
75
76 <p>The argument to the <i>key</i> function is the <i>sequence</i>
77 element. The return value of the <i>key</i> function becomes an
78 argument to <i>predicate</i>. If <i>key</i> is not supplied or
79 <b>nil</b>, the <i>sequence</i> element itself is used. There is no
80 guarantee on the number of times the <i>key</i> will be called. <p>
81
82 <p>If the <i>key</i> and <i>predicate</i> always return, then the
83 operation will always terminate. This is guaranteed even if the
84 <i>predicate</i> does not really consistently represent a total order
85 (in which case the answer may be wrong). If the <i>key</i>
86 consistently returns meaningful keys, and the <i>predicate</i> does
87 reflect some total ordering criterion on those keys, then the answer
88 will be right <p>
89
90 <p>The <i>predicate</i> is assumed to consider two elements <tt>x</tt>
91 and <tt>y</tt> to be equal if <tt>(funcall </tt><i>predicate</i><tt>
92 </tt><i>x</i><tt> </tt><i>y</i><tt>)</tt> and <tt>(funcall
93 </tt><i>predicate</i><tt> </tt><i>y</i><tt> </tt><i>x</i><tt>)</tt>
94 are both <i>false</i>.
95
96
97 <p>The return value of <tt>(extremum predicate sequence :key key)</tt>
98 can be defined as <tt>(elt (<a class="hyperspec" href ="
99 http://www.lispworks.com/documentation/HyperSpec/Body/fun_sortcm_stable-sort.html"><b>sort</b></a>
100 predicate (subseq sequence start end) :key key) 0)</tt> except when
101 <i>sequence</i> is empty (see Exceptional Situations), but may use
102 faster (less asymptotically complex) algorithms to find this answer.
103
104 <p><b>extrema</b> is similar to <b>extremum</b>, but it returns a list
105 of values. There can be more than one extremum, as determined by
106 <i>predicate</i>, and with <b>extremum</b> the choice of which
107 extremum to return is arbitrary. <b>extrema</b> returns all the
108 possible values which <i>predicate</i> determines to be equal.
109
110 <p><b>n-most-extreme</b> returns a list of <i>n</i> values without
111 testing for equality. It orders <i>sequence</i> in the same way as
112 <b>extremum</b> and <b>extrema</b>, then returns the first <i>n</i>
113 elements of the sorted sequence.
114
115 <p>
116 <p><b>Exceptional situations:</b><p>
117 <p>
118
119 <p>If <i>sequence</i> is empty, then the error <i>no-extremum</i> is
120 signalled. Invoking the <b>continue</b> restart will cause
121 <b>extremum</b> to return <b>nil</b>.
122
123
124 <p>Should be prepared to signal an error of type <b>type-error</b> if
125 <i>sequence</i> is not a proper sequence.
126
127 <p>If there are fewer than <i>n</i> values in the part of
128 <i>sequence</i> that <b>n-most-extreme</b> may operate on, it returns
129 all the values it can in sorted order and signals the warning
130 <b>n-most-extreme-not-enough-elements</b>. This warning stores the
131 given values for <i>n</i> and the relevant subsequence, and they may
132 be accessed with <b>n-most-extreme-not-enough-elements-n</b> and
133 <b>n-most-extreme-not-enough-elements-subsequence</b>, respectively.
134
135 <p><p><b>Implementation notes:</b></p>
136
137 <p>There are two implementations of this function included in
138 cl-utilities, which should only concern you if you want to squeeze out
139 more efficiency, since the versions perform differently on different
140 inputs.
141
142 <p>The function <b>extremum-fastkey</b> is used exactly like
143 <b>extremum</b>, but it calls <i>key</i> fewer times. If <i>key</i> is
144 fast, <b>extremum-fastkey</b> is slower than regular <b>extremum</b>,
145 but if <i>key</i> is hard to compute you can get significant gains in
146 speed. The <b>extremum-fastkey</b> function is more complicated than
147 <b>extremum</b>, and therefore may be more likely to contain
148 bugs. That said, it doesn't seem buggy.</p>
149
150 <p>Don't worry about the performance of passing <tt>#'identity</tt> as
151 <i>key</i>. This is optimized by a compiler macro.</p>
152
153 <p class="footer"><hr><a href="index.html">Manual Index</a></p>
154
155 </body></html>