1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!--
Generated from r6rs-lib.tex by tex2page, v 20070803
(running on MzScheme 371, unix),
(c) Dorai Sitaram,
http://www.ccs.neu.edu/~dorai/tex2page/tex2page-doc.html
-->
<head>
<title>
r6rs-lib
</title>
<link rel="stylesheet" type="text/css" href="r6rs-lib-Z-S.css" title=default>
<meta name=robots content="index,follow">
</head>
<body>
<div id=slidecontent>
<div align=right class=navigation>[Go to <span><a href="r6rs-lib.html">first</a>, <a href="r6rs-lib-Z-H-19.html">previous</a></span><span>, <a href="r6rs-lib-Z-H-21.html">next</a></span> page<span>; </span><span><a href="r6rs-lib-Z-H-1.html#node_toc_start">contents</a></span><span><span>; </span><a href="r6rs-lib-Z-H-21.html#node_index_start">index</a></span>]</div>
<p></p>
<a name="node_chap_19"></a>
<h1 class=chapter>
<div class=chapterheading><a href="r6rs-lib-Z-H-1.html#node_toc_node_chap_19">Chapter 19</a></div><br>
<a href="r6rs-lib-Z-H-1.html#node_toc_node_chap_19">R<sup>5</sup>RS compatibility</a></h1>
<p></p>
<p>
The features described in this chapter are exported from the
<tt>(rnrs r5rs (6))</tt><a name="node_idx_1284"></a>library and provide some functionality of the
preceding revision of this report [<a href="r6rs-lib-Z-H-21.html#node_bib_8">8</a>] that was omitted from
the main part of the current report.</p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1286"></a>exact->inexact<i> z</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1288"></a>inexact->exact<i> z</i>)</tt> procedure </div>
<p>
These are the same as the <tt>inexact</tt> and <tt>exact</tt>
procedures; see report section on “Generic conversions”.
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1290"></a>quotient<i> <i>n<sub>1</sub></i> <i>n<sub>2</sub></i></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1292"></a>remainder<i> <i>n<sub>1</sub></i> <i>n<sub>2</sub></i></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1294"></a>modulo<i> <i>n<sub>1</sub></i> <i>n<sub>2</sub></i></i>)</tt> procedure </div>
<p>
These procedures implement number-theoretic (integer)
division. <i>N<sub>2</sub></i> must be non-zero. All three procedures
return integer objects. If <i>n<sub>1</sub></i>/<i>n<sub>2</sub></i> is an integer object:
</p>
<tt> (quotient <i>n<sub>1</sub></i> <i>n<sub>2</sub></i>) ⇒ <i>n<sub>1</sub></i>/<i>n<sub>2</sub></i><br>
(remainder <i>n<sub>1</sub></i> <i>n<sub>2</sub></i>) ⇒ 0<br>
(modulo <i>n<sub>1</sub></i> <i>n<sub>2</sub></i>) ⇒ 0<br>
<p></tt>
If <i>n<sub>1</sub></i>/<i>n<sub>2</sub></i> is not an integer object:
</p>
<tt> (quotient <i>n<sub>1</sub></i> <i>n<sub>2</sub></i>) ⇒ <i>n<sub><em>q</em></sub></i><br>
(remainder <i>n<sub>1</sub></i> <i>n<sub>2</sub></i>) ⇒ <i>n<sub><em>r</em></sub></i><br>
(modulo <i>n<sub>1</sub></i> <i>n<sub>2</sub></i>) ⇒ <i>n<sub><em>m</em></sub></i><br>
<p></tt>
where <i>n<sub><em>q</em></sub></i> is <i>n<sub>1</sub></i>/<i>n<sub>2</sub></i> rounded towards zero,
0 < |<i>n<sub><em>r</em></sub></i>| < |<i>n<sub>2</sub></i>|, 0 < |<i>n<sub><em>m</em></sub></i>| < |<i>n<sub>2</sub></i>|,
<i>n<sub><em>r</em></sub></i> and <i>n<sub><em>m</em></sub></i> differ from <i>n<sub>1</sub></i> by a multiple of <i>n<sub>2</sub></i>,
<i>n<sub><em>r</em></sub></i> has the same sign as <i>n<sub>1</sub></i>, and
<i>n<sub><em>m</em></sub></i> has the same sign as <i>n<sub>2</sub></i>.</p>
<p>
Consequently, for integer objects <i>n<sub>1</sub></i> and <i>n<sub>2</sub></i> with
<i>n<sub>2</sub></i> not equal to 0,
</p>
<tt> (= <i>n<sub>1</sub></i> (+ (* <i>n<sub>2</sub></i> (quotient <i>n<sub>1</sub></i> <i>n<sub>2</sub></i>))<br>
(remainder <i>n<sub>1</sub></i> <i>n<sub>2</sub></i>)))<br>
⇒ <tt>#t</tt><p></tt>
provided all number object involved in that computation are exact.</p>
<p>
</p>
<tt>(modulo 13 4) ⇒ 1<br>
(remainder 13 4) ⇒ 1<br>
<br>
(modulo -13 4) ⇒ 3<br>
(remainder -13 4) ⇒ -1<br>
<br>
(modulo 13 -4) ⇒ -3<br>
(remainder 13 -4) ⇒ 1<br>
<br>
(modulo -13 -4) ⇒ -1<br>
(remainder -13 -4) ⇒ -1<br>
<br>
(remainder -13 -4.0) ⇒ -1.0<p></tt></p>
<p>
</p>
<blockquote><em>Note: </em>
These procedures could be defined in terms of <tt>div</tt> and <tt>mod</tt> (see report section on “Arithmetic operations”) as follows (without checking of the
argument types):
<tt>(define (sign n)<br>
(cond<br>
((negative? n) -1)<br>
((positive? n) 1)<br>
(else 0)))<br>
<br>
(define (quotient n1 n2)<br>
(* (sign n1) (sign n2) (div (abs n1) (abs n2))))<br>
<br>
(define (remainder n1 n2)<br>
(* (sign n1) (mod (abs n1) (abs n2))))<br>
<br>
(define (modulo n1 n2)<br>
(* (sign n2) (mod (* (sign n2) n1) (abs n2))))<br>
<p></tt>
</p>
</blockquote>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1296"></a>delay<i> <expression></i>)</tt> syntax </div>
<p>
The <tt>delay</tt> construct is used together with the procedure <tt>force</tt> to
implement <a name="node_idx_1298"></a><em>lazy evaluation</em> or <a name="node_idx_1300"></a><em>call by need</em>.
<tt>(delay <expression>)</tt> returns an object called a
<a name="node_idx_1302"></a><em>promise</em> which at some point in the future may be asked (by
the <tt>force</tt> procedure) to evaluate
<expression>, and deliver the resulting value.
The effect of <expression> returning multiple values
is unspecified.</p>
<p>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1304"></a>force<i> promise</i>)</tt> procedure </div>
<p>
<i>Promise</i> must be a promise.
The <tt>force</tt> procedure forces the value of <i>promise</i>. If no value has been computed for
the promise, then a value is computed and returned. The value of the
promise is cached (or “memoized”) so that if it is forced a second
time, the previously computed value is returned.</p>
<p>
</p>
<tt>(force (delay (+ 1 2))) ⇒ 3<br>
(let ((p (delay (+ 1 2))))<br>
(list (force p) (force p))) <br>
⇒ (3 3)<br>
<br>
(define a-stream<br>
(letrec ((next<br>
(lambda (n)<br>
(cons n (delay (next (+ n 1)))))))<br>
(next 0)))<br>
(define head car)<br>
(define tail<br>
(lambda (stream) (force (cdr stream))))<br>
<br>
(head (tail (tail a-stream))) <br>
⇒ 2<p></tt></p>
<p>
Promises are mainly intended for programs written in
functional style. The following examples should not be considered to
illustrate good programming style, but they illustrate the property that
only one value is computed for a promise, no matter how many times it is
forced.</p>
<p>
</p>
<tt>(define count 0)<br>
(define p<br>
(delay (begin (set! count (+ count 1))<br>
(if (> count x)<br>
count<br>
(force p)))))<br>
(define x 5)<br>
p ⇒ <i>a promise</i><br>
(force p) ⇒ 6<br>
p ⇒ <i>a promise, still</i><br>
(begin (set! x 10)<br>
(force p)) ⇒ 6<p></tt></p>
<p>
Here is a possible implementation of <tt>delay</tt> and <tt>force</tt>.
Promises are implemented here as procedures of no arguments,
and <tt>force</tt> simply calls its argument:</p>
<p>
</p>
<tt>(define force<br>
(lambda (object)<br>
(object)))<p></tt></p>
<p>
The expression</p>
<p>
</p>
<tt>(delay <expression>)<p></tt></p>
<p>
has the same meaning as the procedure call</p>
<p>
</p>
<tt>(make-promise (lambda () <expression>))<p></tt></p>
<p>
as follows</p>
<p>
</p>
<tt>(define-syntax delay<br>
(syntax-rules ()<br>
((delay expression)<br>
(make-promise (lambda () expression))))),<p></tt></p>
<p>
where <tt>make-promise</tt> is defined as follows:</p>
<p>
</p>
<tt>(define make-promise<br>
(lambda (proc)<br>
(let ((result-ready? <tt>#f</tt>)<br>
(result <tt>#f</tt>))<br>
(lambda ()<br>
(if result-ready?<br>
result<br>
(let ((x (proc)))<br>
(if result-ready?<br>
result<br>
(begin (set! result-ready? <tt>#t</tt>)<br>
(set! result x)<br>
result))))))))<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1306"></a>null-environment<i> n</i>)</tt> procedure </div>
<p>
<i>N</i> must be the exact integer object 5. The <tt>null-environment</tt> procedure returns an
environment specifier suitable for use with <tt>eval</tt> (see
chapter <a href="r6rs-lib-Z-H-17.html#node_chap_16">16</a>) representing an environment that is empty except
for the (syntactic) bindings for all keywords described in
the previous revision of this report [<a href="r6rs-lib-Z-H-21.html#node_bib_8">8</a>], including bindings
for <tt>=></tt>, <tt>...</tt>, <tt>else</tt>, and <tt>_</tt> that are the same as those in
the <tt>(rnrs base (6))</tt> library.
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1308"></a>scheme-report-environment<i> n</i>)</tt> procedure </div>
<p>
<i>N</i> must be the exact integer object 5. The <tt>scheme-report-environment</tt> procedure returns
an environment specifier for an environment that is empty except for
the bindings for the identifiers described in the previous
revision of this report [<a href="r6rs-lib-Z-H-21.html#node_bib_8">8</a>], omitting <tt>load</tt>, <tt>interaction-environment</tt>, <tt>transcript-on</tt>, <tt>transcript-off</tt>, and <tt>char-ready?</tt>. The
variable bindings have as values the procedures of the same names described in
this report, and the keyword bindings, including
<tt>=></tt>, <tt>...</tt>, <tt>else</tt>, and <tt>_</tt> are the same as those described
in this report.
</p>
<p></p>
<p>
</p>
<p></p>
<p>
</p>
<div class=smallskip></div>
<p style="margin-top: 0pt; margin-bottom: 0pt">
<div align=right class=navigation>[Go to <span><a href="r6rs-lib.html">first</a>, <a href="r6rs-lib-Z-H-19.html">previous</a></span><span>, <a href="r6rs-lib-Z-H-21.html">next</a></span> page<span>; </span><span><a href="r6rs-lib-Z-H-1.html#node_toc_start">contents</a></span><span><span>; </span><a href="r6rs-lib-Z-H-21.html#node_index_start">index</a></span>]</div>
</p>
<p></p>
</div>
</body>
</html>
|