282 lines
16 KiB
HTML
282 lines
16 KiB
HTML
<!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>
|