5279 lines
294 KiB
HTML
5279 lines
294 KiB
HTML
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
<html>
|
|
<!--
|
|
|
|
Generated from r6rs.tex by tex2page, v 20100828
|
|
(running on MzScheme 4.2.4, :unix),
|
|
(c) Dorai Sitaram,
|
|
http://evalwhen.com/tex2page/index.html
|
|
|
|
-->
|
|
<head>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<title>
|
|
Revised^6 Report on the Algorithmic Language Scheme
|
|
</title>
|
|
<link rel="stylesheet" type="text/css" href="r6rs-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.html">first</a>, <a href="r6rs-Z-H-13.html">previous</a></span><span>, <a href="r6rs-Z-H-15.html">next</a></span> page<span>; </span><span><a href="r6rs-Z-H-2.html#node_toc_start">contents</a></span><span><span>; </span><a href="r6rs-Z-H-21.html#node_index_start">index</a></span>]</div>
|
|
<p></p>
|
|
<a name="node_chap_11"></a>
|
|
<h1 class=chapter>
|
|
<div class=chapterheading><a href="r6rs-Z-H-2.html#node_toc_node_chap_11">Chapter 11</a></div><br>
|
|
<a href="r6rs-Z-H-2.html#node_toc_node_chap_11">Base library</a></h1>
|
|
<p></p>
|
|
<p>
|
|
This chapter describes Scheme's <tt>(rnrs base (6))</tt><a name="node_idx_324"></a>library, which exports many of
|
|
the procedure and syntax bindings that are traditionally associated
|
|
with Scheme.</p>
|
|
<p>
|
|
Section <a href="r6rs-Z-H-14.html#node_sec_11.20">11.20</a> defines the rules that identify
|
|
tail calls and tail contexts in constructs from the <tt>(rnrs base (6))</tt>
|
|
library.</p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.1"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.1">11.1 Base types</a></h2>
|
|
<p></p>
|
|
<p>
|
|
No object satisfies more than one of the following predicates:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>boolean? pair?
|
|
<p class=nopadding>symbol? number?</p>
|
|
|
|
<p class=nopadding>char? string?</p>
|
|
|
|
<p class=nopadding>vector? procedure?</p>
|
|
|
|
<p class=nopadding>null?</p>
|
|
<p></tt></p>
|
|
<p>
|
|
These predicates define the base types <em>boolean</em>, <em>pair</em>, <em>symbol</em>, <em>number</em>, <em>char</em> (or <em>character</em>), <em>string</em>, <em>vector</em>, and <em>procedure</em>. Moreover, the empty list is a special
|
|
object of its own type.
|
|
<a name="node_idx_326"></a><a name="node_idx_328"></a><a name="node_idx_330"></a><a name="node_idx_332"></a><a name="node_idx_334"></a><a name="node_idx_336"></a><a name="node_idx_338"></a><a name="node_idx_340"></a><a name="node_idx_342"></a><a name="node_idx_344"></a><a name="node_idx_346"></a></p>
|
|
<p>
|
|
Note that, although there is a separate boolean type, any Scheme value
|
|
can be used as a boolean value for the purpose of a conditional test;
|
|
see section <a href="r6rs-Z-H-8.html#node_sec_5.7">5.7</a>.</p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.2"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.2">11.2 Definitions</a></h2>
|
|
<p></p>
|
|
<p>
|
|
Definitions<a name="node_idx_348"></a>may appear within a
|
|
<top-level body> (section <a href="r6rs-Z-H-11.html#node_sec_8.1">8.1</a>),
|
|
at the top of a <library body> (section <a href="r6rs-Z-H-10.html#node_sec_7.1">7.1</a>),
|
|
or at the top of a <body> (section <a href="r6rs-Z-H-14.html#node_sec_11.3">11.3</a>).</p>
|
|
<p>
|
|
A <definition> may be a variable definition
|
|
(section <a href="r6rs-Z-H-14.html#node_sec_11.2.1">11.2.1</a>) or
|
|
keyword definition
|
|
(section <a href="r6rs-Z-H-14.html#node_sec_11.2.1">11.2.1</a>).
|
|
Macro uses that expand into definitions or groups of
|
|
definitions (packaged in a <tt>begin</tt>, <tt>let-syntax</tt>, or
|
|
<tt>letrec-syntax</tt> form; see section <a href="r6rs-Z-H-14.html#node_sec_11.4.7">11.4.7</a>) may also appear
|
|
wherever other definitions may appear.</p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.2.1"></a>
|
|
<h3 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.2.1">11.2.1 Variable definitions</a></h3>
|
|
<p></p>
|
|
<p>
|
|
The <tt>define</tt> form described in this section is a
|
|
<definition><a name="node_idx_350"></a>used to create variable bindings
|
|
and may appear anywhere other definitions may appear.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_352"></a>define<i> <variable> <expression></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_354"></a>define<i> <variable></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
|
|
<div align=left><tt>(define (<variable> <formals>) <body>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
|
|
<div align=left><tt>(define (<variable> . <formal>) <body>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
The first from of <tt>define</tt> binds <variable> to a new
|
|
location before assigning the value of <expression> to it.
|
|
</p>
|
|
|
|
<tt>(define add3
|
|
<p class=nopadding> (lambda (x) (+ x 3)))</p>
|
|
|
|
<p class=nopadding>(add3 3) <span style="margin-left: 2em">‌</span>⇒ 6</p>
|
|
|
|
<p class=nopadding>(define first car)</p>
|
|
|
|
<p class=nopadding>(first '(1 2)) <span style="margin-left: 2em">‌</span>⇒ 1</p>
|
|
<p></tt>
|
|
The continuation of <expression> should not be invoked more than
|
|
once.</p>
|
|
<p>
|
|
<em>Implementation responsibilities: </em>Implementations should detect that the
|
|
continuation of <expression> is invoked more than once.
|
|
If the implementation detects this, it must raise an
|
|
exception with condition type <tt>&assertion</tt>.</p>
|
|
<p>
|
|
The second form of <tt>define</tt> is equivalent to
|
|
</p>
|
|
|
|
<tt>(define <variable> <unspecified>)<p></tt>
|
|
where <unspecified> is a side-effect-free expression returning
|
|
an unspecified value.</p>
|
|
<p>
|
|
In the third form of <tt>define</tt>, <formals> must be either a
|
|
sequence of zero or more variables, or a sequence of one or more
|
|
variables followed by a dot <tt>.</tt> and another variable (as
|
|
in a lambda expression, see section <a href="r6rs-Z-H-14.html#node_sec_11.4.2">11.4.2</a>). This form is equivalent to
|
|
</p>
|
|
|
|
<tt>(define <variable>
|
|
<p class=nopadding> (lambda (<formals>) <body>)).</p>
|
|
<p></tt></p>
|
|
<p>
|
|
In the fourth form of <tt>define</tt>,
|
|
<formal> must be a single
|
|
variable. This form is equivalent to
|
|
</p>
|
|
|
|
<tt>(define <variable>
|
|
<p class=nopadding> (lambda <formal> <body>)).</p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.2.2"></a>
|
|
<h3 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.2.2">11.2.2 Syntax definitions</a></h3>
|
|
<p></p>
|
|
<p>
|
|
The <tt>define-syntax</tt> form described in this section is a
|
|
<definition><a name="node_idx_356"></a>used to create keyword bindings
|
|
and may appear anywhere other definitions may appear.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_358"></a>define-syntax<i> <keyword> <expression></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
Binds <keyword> to the value of
|
|
<expression>, which must evaluate, at macro-expansion
|
|
time, to a transformer. Macro transformers can be created using the
|
|
<tt>syntax-rules</tt> and <tt>identifier-syntax</tt> forms described in
|
|
section <a href="r6rs-Z-H-14.html#node_sec_11.19">11.19</a>. See library
|
|
section on “Transformers” for a more
|
|
complete description of transformers.</p>
|
|
<p>
|
|
Keyword bindings established by <tt>define-syntax</tt> are visible
|
|
throughout the body in which they appear, except where shadowed by
|
|
other bindings, and nowhere else, just like variable bindings established
|
|
by <tt>define</tt>.
|
|
All bindings established by a set of definitions, whether
|
|
keyword or variable definitions, are visible within the definitions
|
|
themselves.</p>
|
|
<p>
|
|
<em>Implementation responsibilities: </em>The implementation should detect if the value of
|
|
<expression> cannot possibly be a transformer.</p>
|
|
<p>
|
|
Example:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let ()
|
|
<p class=nopadding> (define even?</p>
|
|
|
|
<p class=nopadding> (lambda (x)</p>
|
|
|
|
<p class=nopadding> (or (= x 0) (odd? (- x 1)))))</p>
|
|
|
|
<p class=nopadding> (define-syntax odd?</p>
|
|
|
|
<p class=nopadding> (syntax-rules ()</p>
|
|
|
|
<p class=nopadding> ((odd? x) (not (even? x)))))</p>
|
|
|
|
<p class=nopadding> (even? 10)) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
<p></tt></p>
|
|
<p>
|
|
An implication of the left-to-right processing order
|
|
(section <a href="r6rs-Z-H-13.html#node_chap_10">10</a>) is that one definition can
|
|
affect whether a subsequent form is also a definition. </p>
|
|
<p>
|
|
Example:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let ()
|
|
<p class=nopadding> (define-syntax bind-to-zero</p>
|
|
|
|
<p class=nopadding> (syntax-rules ()</p>
|
|
|
|
<p class=nopadding> ((bind-to-zero id) (define id 0))))</p>
|
|
|
|
<p class=nopadding> (bind-to-zero x)</p>
|
|
|
|
<p class=nopadding> x) <span style="margin-left: 2em">‌</span>⇒ 0</p>
|
|
<p></tt></p>
|
|
<p>
|
|
The behavior is unaffected by any binding for
|
|
<tt>bind-to-zero</tt> that might appear outside of the <tt>let</tt>
|
|
expression.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.3"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.3">11.3 Bodies</a></h2>
|
|
<p></p>
|
|
<p>
|
|
<a name="node_idx_360"></a>The <body> of a <tt>lambda</tt>, <tt>let</tt>, <tt>let*</tt>,
|
|
<tt>let-values</tt>, <tt>let*-values</tt>, <tt>letrec</tt>, or <tt>letrec*</tt>
|
|
expression, or that of a definition with a body
|
|
consists of zero or more definitions followed by one or more
|
|
expressions.</p>
|
|
<p>
|
|
<tt><definition> <tt>...</tt> <expression<sub>1</sub>> <expression<sub>2</sub>> <tt>...</tt></tt></p>
|
|
<p>
|
|
Each identifier defined by a
|
|
definition is local to the <body>. That is, the identifier is
|
|
bound, and the region of the binding is the
|
|
entire <body> (see section <a href="r6rs-Z-H-8.html#node_sec_5.2">5.2</a>).</p>
|
|
<p>
|
|
Example:
|
|
</p>
|
|
|
|
<tt>(let ((x 5))
|
|
<p class=nopadding> (define foo (lambda (y) (bar x y)))</p>
|
|
|
|
<p class=nopadding> (define bar (lambda (a b) (+ (* a b) a)))</p>
|
|
|
|
<p class=nopadding> (foo (+ x 3))) <span style="margin-left: 2em">‌</span>⇒ 45</p>
|
|
<p></tt>
|
|
When <tt>begin</tt>, <tt>let-syntax</tt>, or <tt>letrec-syntax</tt> forms
|
|
occur in a body prior to the first
|
|
expression, they are spliced into the body; see section <a href="r6rs-Z-H-14.html#node_sec_11.4.7">11.4.7</a>.
|
|
Some or all of the body, including portions wrapped in <tt>begin</tt>,
|
|
<tt>let-syntax</tt>, or <tt>letrec-syntax</tt>
|
|
forms, may be specified by a macro use
|
|
(see section <a href="r6rs-Z-H-12.html#node_sec_9.2">9.2</a>).</p>
|
|
<p>
|
|
An expanded <body> (see chapter <a href="r6rs-Z-H-13.html#node_chap_10">10</a>)
|
|
containing variable definitions can
|
|
always be converted into an equivalent <tt>letrec*</tt>
|
|
expression. For example, the <tt>let</tt> expression in the above
|
|
example is equivalent to</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let ((x 5))
|
|
<p class=nopadding> (letrec* ((foo (lambda (y) (bar x y)))</p>
|
|
|
|
<p class=nopadding> (bar (lambda (a b) (+ (* a b) a))))</p>
|
|
|
|
<p class=nopadding> (foo (+ x 3))))</p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.4"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.4">11.4 Expressions</a></h2>
|
|
<p></p>
|
|
<p>
|
|
The entries in this section describe the expressions of the <tt>(rnrs base (6))</tt>
|
|
library, which may occur in the position of the <expression>
|
|
syntactic variable in addition to the primitive
|
|
expression types as described in
|
|
section <a href="r6rs-Z-H-12.html#node_sec_9.1">9.1</a>.</p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.4.1"></a>
|
|
<h3 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.4.1">11.4.1 Quotation</a></h3>
|
|
<p><div style="height: -9.0pt"></div>
|
|
<p style="margin-top: 0pt; margin-bottom: 0pt"></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_362"></a>quote<i> <datum></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
<em>Syntax: </em><Datum> should be a syntactic datum.</p>
|
|
<p>
|
|
<em>Semantics: </em><tt>(quote <datum>)</tt> evaluates to the datum value
|
|
represented by <datum>
|
|
(see
|
|
section <a href="r6rs-Z-H-7.html#node_sec_4.3">4.3</a>). This notation is used to include
|
|
constants.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(quote a) <span style="margin-left: 2em">‌</span>⇒ a
|
|
<p class=nopadding>(quote <tt>#</tt>(a b c)) <span style="margin-left: 2em">‌</span>⇒ #(a b c)</p>
|
|
|
|
<p class=nopadding>(quote (+ 1 2)) <span style="margin-left: 2em">‌</span>⇒ (+ 1 2)</p>
|
|
<p></tt></p>
|
|
<p>
|
|
As noted in section <a href="r6rs-Z-H-7.html#node_sec_4.3.5">4.3.5</a>, <tt>(quote <datum>)</tt>
|
|
may be abbreviated as <tt>'</tt><datum>:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>'"abc" <span style="margin-left: 2em">‌</span>⇒ "abc"
|
|
<p class=nopadding>'145932 <span style="margin-left: 2em">‌</span>⇒ 145932</p>
|
|
|
|
<p class=nopadding>'a <span style="margin-left: 2em">‌</span>⇒ a</p>
|
|
|
|
<p class=nopadding>'#(a b c) <span style="margin-left: 2em">‌</span>⇒ #(a b c)</p>
|
|
|
|
<p class=nopadding>'() <span style="margin-left: 2em">‌</span>⇒ ()</p>
|
|
|
|
<p class=nopadding>'(+ 1 2) <span style="margin-left: 2em">‌</span>⇒ (+ 1 2)</p>
|
|
|
|
<p class=nopadding>'(quote a) <span style="margin-left: 2em">‌</span>⇒ (quote a)</p>
|
|
|
|
<p class=nopadding>''a <span style="margin-left: 2em">‌</span>⇒ (quote a)</p>
|
|
<p></tt></p>
|
|
<p>
|
|
As noted in section <a href="r6rs-Z-H-8.html#node_sec_5.10">5.10</a>, constants are immutable.</p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
Different constants that are the value of a <tt>quote</tt> expression may
|
|
share the same locations.
|
|
</blockquote>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.4.2"></a>
|
|
<h3 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.4.2">11.4.2 Procedures</a></h3>
|
|
<p><div style="height: -9.0pt"></div>
|
|
<p style="margin-top: 0pt; margin-bottom: 0pt"></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_364"></a>lambda<i> <formals> <body></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
<em>Syntax: </em><Formals> must be a formal parameter list as described below,
|
|
and <body> must be as described in section <a href="r6rs-Z-H-14.html#node_sec_11.3">11.3</a>.</p>
|
|
<p>
|
|
<em>Semantics: </em>A <tt>lambda</tt> expression evaluates to a procedure. The environment in
|
|
effect when the <tt>lambda</tt> expression is evaluated is remembered as part of the
|
|
procedure. When the procedure is later called with some
|
|
arguments, the environment in which the <tt>lambda</tt> expression was evaluated is
|
|
extended by binding the variables in the parameter list to
|
|
fresh locations, and the resulting argument values are stored
|
|
in those locations. Then, the expressions in the body of the <tt>lambda</tt> expression
|
|
(which may contain definitions and thus represent a <tt>letrec*</tt> form, see section <a href="r6rs-Z-H-14.html#node_sec_11.3">11.3</a>) are evaluated
|
|
sequentially in the extended environment.
|
|
The results of the last expression in the body are returned as
|
|
the results of the procedure call.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(lambda (x) (+ x x)) <span style="margin-left: 2em">‌</span>⇒ <em>a procedure</em>
|
|
<p class=nopadding>((lambda (x) (+ x x)) 4) <span style="margin-left: 2em">‌</span>⇒ 8</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>((lambda (x)</p>
|
|
|
|
<p class=nopadding> (define (p y)</p>
|
|
|
|
<p class=nopadding> (+ y 1))</p>
|
|
|
|
<p class=nopadding> (+ (p x) x))</p>
|
|
|
|
<p class=nopadding> 5) <span style="margin-left: 2em">‌</span>⇒ 11</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(define reverse-subtract</p>
|
|
|
|
<p class=nopadding> (lambda (x y) (- y x)))</p>
|
|
|
|
<p class=nopadding>(reverse-subtract 7 10) <span style="margin-left: 2em">‌</span>⇒ 3</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(define add4</p>
|
|
|
|
<p class=nopadding> (let ((x 4))</p>
|
|
|
|
<p class=nopadding> (lambda (y) (+ x y))))</p>
|
|
|
|
<p class=nopadding>(add4 6) <span style="margin-left: 2em">‌</span>⇒ 10</p>
|
|
<p></tt></p>
|
|
<p>
|
|
<Formals> must have one of the following forms:</p>
|
|
<p>
|
|
</p>
|
|
<ul>
|
|
<li><p><tt>(<variable<sub>1</sub>> <tt>...</tt>)</tt>:
|
|
The procedure takes a fixed number of arguments; when the procedure is
|
|
called, the arguments are stored in the bindings of the
|
|
corresponding variables.</p>
|
|
<p>
|
|
</p>
|
|
<li><p><variable>:
|
|
The procedure takes any number of arguments; when the procedure is
|
|
called, the sequence of arguments is converted into a newly
|
|
allocated list, and the list is stored in the binding of the
|
|
<variable>.</p>
|
|
<p>
|
|
</p>
|
|
<li><p><tt>(<variable<sub>1</sub>> <tt>...</tt> <variable<sub><em>n</em></sub>> <strong>.</strong>
|
|
<variable<sub><em>n</em>+1</sub>>)</tt>:
|
|
If a period <tt>.</tt> precedes the last variable, then
|
|
the procedure takes <em>n</em> or more arguments, where <em>n</em> is the
|
|
number of parameters before the period (there must
|
|
be at least one).
|
|
The value stored in the binding of the last variable is a
|
|
newly allocated
|
|
list of the arguments left over after all the other
|
|
arguments have been matched up against the other parameters.
|
|
</p>
|
|
</ul><p></p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>((lambda x x) 3 4 5 6) <span style="margin-left: 2em">‌</span>⇒ (3 4 5 6)
|
|
<p class=nopadding>((lambda (x y . z) z)</p>
|
|
|
|
<p class=nopadding> 3 4 5 6) <span style="margin-left: 2em">‌</span>⇒ (5 6)</p>
|
|
<p></tt></p>
|
|
<p>
|
|
Any <variable> must not appear more than once in
|
|
<formals>.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.4.3"></a>
|
|
<h3 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.4.3">11.4.3 Conditionals</a></h3>
|
|
<p><div style="height: -9.0pt"></div>
|
|
<p style="margin-top: 0pt; margin-bottom: 0pt"></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_366"></a>if<i> <test> <consequent> <alternate></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_368"></a>if<i> <test> <consequent></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
<em>Syntax: </em><Test>, <consequent>, and <alternate> must be
|
|
expressions.</p>
|
|
<p>
|
|
<em>Semantics: </em>An <tt>if</tt> expression is evaluated as follows: first,
|
|
<test> is evaluated. If it yields a true value<a name="node_idx_370"></a>(see
|
|
section <a href="r6rs-Z-H-8.html#node_sec_5.7">5.7</a>), then <consequent> is evaluated and
|
|
its values are returned. Otherwise <alternate> is evaluated and its
|
|
values are returned. If <test> yields <tt>#f</tt> and no
|
|
<alternate> is specified, then the result of the expression is unspecified.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(if (> 3 2) 'yes 'no) <span style="margin-left: 2em">‌</span>⇒ yes
|
|
<p class=nopadding>(if (> 2 3) 'yes 'no) <span style="margin-left: 2em">‌</span>⇒ no</p>
|
|
|
|
<p class=nopadding>(if (> 3 2)</p>
|
|
|
|
<p class=nopadding> (- 3 2)</p>
|
|
|
|
<p class=nopadding> (+ 3 2)) <span style="margin-left: 2em">‌</span>⇒ 1</p>
|
|
|
|
<p class=nopadding>(if #f #f) <span style="margin-left: 2em">‌</span>⇒ <em>unspecified</em></p>
|
|
<p></tt></p>
|
|
<p>
|
|
The <consequent> and <alternate> expressions are in
|
|
tail context if the <tt>if</tt> expression itself is; see
|
|
section <a href="r6rs-Z-H-14.html#node_sec_11.20">11.20</a>.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.4.4"></a>
|
|
<h3 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.4.4">11.4.4 Assignments</a></h3>
|
|
<p><div style="height: -9.0pt"></div>
|
|
<p style="margin-top: 0pt; margin-bottom: 0pt"></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_372"></a>set!<i> <variable> <expression></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
<Expression> is evaluated, and the resulting value is stored in
|
|
the location to which <variable> is bound. <Variable> must
|
|
be bound either in some region<a name="node_idx_374"></a>enclosing the <tt>set!</tt> expression
|
|
or at the top level. The result of the <tt>set!</tt>
|
|
expression is unspecified.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let ((x 2))
|
|
<p class=nopadding> (+ x 1)</p>
|
|
|
|
<p class=nopadding> (set! x 4)</p>
|
|
|
|
<p class=nopadding> (+ x 1)) <span style="margin-left: 2em">‌</span>⇒ 5</p>
|
|
<p></tt></p>
|
|
<p>
|
|
It is a syntax violation if <variable> refers to an
|
|
immutable binding.</p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
The identifier <tt>set!</tt> is exported with level 1 as well. See
|
|
section <a href="r6rs-Z-H-14.html#node_sec_11.19">11.19</a>.
|
|
</blockquote>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.4.5"></a>
|
|
<h3 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.4.5">11.4.5 Derived conditionals</a></h3>
|
|
<p><div style="height: -9.0pt"></div>
|
|
<p style="margin-top: 0pt; margin-bottom: 0pt"></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_376"></a>cond<i> <cond clause<sub>1</sub>> <cond clause<sub>2</sub>> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
|
|
<a name="node_idx_378"></a><div align=left><tt>=></tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>auxiliary syntax </div>
|
|
|
|
<a name="node_idx_380"></a><div align=left><tt>else</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>auxiliary syntax </div>
|
|
<p>
|
|
<em>Syntax: </em>Each <cond clause> must be of the form
|
|
</p>
|
|
|
|
<tt>(<test> <expression<sub>1</sub>> <tt>...</tt>)<p></tt>
|
|
where <test> is an expression. Alternatively, a <cond clause> may be
|
|
of the form
|
|
</p>
|
|
|
|
<tt>(<test> => <expression>)<p></tt>
|
|
The last <cond clause> may be
|
|
an “<tt>else</tt> clause”, which has the form
|
|
</p>
|
|
|
|
<tt>(else <expression<sub>1</sub>> <expression<sub>2</sub>> <tt>...</tt>).<p></tt></p>
|
|
<p>
|
|
<em>Semantics: </em>A <tt>cond</tt> expression is evaluated by evaluating the <test>
|
|
expressions of successive <cond clause>s in order until one of them
|
|
evaluates to a true value<a name="node_idx_382"></a>(see
|
|
section <a href="r6rs-Z-H-8.html#node_sec_5.7">5.7</a>). When a <test> evaluates to a true
|
|
value, then the remaining <expression>s in its <cond clause> are
|
|
evaluated in order, and the results of the last <expression> in the
|
|
<cond clause> are returned as the results of the entire <tt>cond</tt>
|
|
expression. If the selected <cond clause> contains only the
|
|
<test> and no <expression>s, then the value of the
|
|
<test> is returned as the result. If the selected <cond clause> uses the
|
|
<tt>=></tt> alternate form, then the <expression> is evaluated.
|
|
Its value must be a procedure. This procedure should accept one argument; it is
|
|
called on the value of the <test> and the values returned by this
|
|
procedure are returned by the <tt>cond</tt> expression.
|
|
If all <test>s evaluate
|
|
to <tt>#f</tt>, and there is no <tt>else</tt> clause, then
|
|
the conditional expression returns unspecified values; if there is an <tt>else</tt>
|
|
clause, then its <expression>s are evaluated, and the values of
|
|
the last one are returned.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(cond ((> 3 2) 'greater)
|
|
<p class=nopadding> ((< 3 2) 'less)) <span style="margin-left: 2em">‌</span>⇒ greater</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(cond ((> 3 3) 'greater)</p>
|
|
|
|
<p class=nopadding> ((< 3 3) 'less)</p>
|
|
|
|
<p class=nopadding> (else 'equal)) <span style="margin-left: 2em">‌</span>⇒ equal</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(cond ('(1 2 3) => cadr)</p>
|
|
|
|
<p class=nopadding> (else <tt>#f</tt>)) <span style="margin-left: 2em">‌</span>⇒ 2</p>
|
|
<p></tt></p>
|
|
<p>
|
|
For a <cond clause> of one of the following forms
|
|
</p>
|
|
|
|
<tt>(<test> <expression<sub>1</sub>> <tt>...</tt>)
|
|
<p class=nopadding>(else <expression<sub>1</sub>> <expression<sub>2</sub>> <tt>...</tt>)</p>
|
|
<p></tt>
|
|
the last <expression> is in tail context if the <tt>cond</tt> form
|
|
itself is. For a <cond clause> of the form
|
|
</p>
|
|
|
|
<tt>(<test> => <expression>)<p></tt>
|
|
the (implied) call to the procedure that results from the evaluation
|
|
of <expression> is in a tail context if the <tt>cond</tt> form
|
|
itself is. See section <a href="r6rs-Z-H-14.html#node_sec_11.20">11.20</a>.</p>
|
|
<p>
|
|
A sample definition of <tt>cond</tt> in terms of simpler forms is in
|
|
appendix <a href="r6rs-Z-H-16.html#node_chap_B">B</a>.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_384"></a>case<i> <key> <case clause<sub>1</sub>> <case clause<sub>2</sub>> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
<em>Syntax: </em><Key> must be an expression. Each <case clause> must have one of
|
|
the following forms:
|
|
</p>
|
|
|
|
<tt>((<datum<sub>1</sub>> <tt>...</tt>) <expression<sub>1</sub>> <expression<sub>2</sub>> <tt>...</tt>)
|
|
<p class=nopadding>(else <expression<sub>1</sub>> <expression<sub>2</sub>> <tt>...</tt>)</p>
|
|
<p></tt>
|
|
<a name="node_idx_386"></a>The second form, which specifies an “<tt>else</tt> clause”,
|
|
may only appear as the last <case clause>.
|
|
Each <datum> is an external representation of some object.
|
|
The data represented by the <datum>s need not be distinct.</p>
|
|
<p>
|
|
<em>Semantics: </em>A <tt>case</tt> expression is evaluated as follows. <Key> is
|
|
evaluated and its result is compared using <tt>eqv?</tt> (see
|
|
section <a href="r6rs-Z-H-14.html#node_sec_11.5">11.5</a>) against the data
|
|
represented by the <datum>s of each <case clause> in turn, proceeding
|
|
in order from left to right through the set of clauses. If the
|
|
result of evaluating <key> is equivalent to a datum of a <case clause>, the
|
|
corresponding <expression>s are evaluated from left
|
|
to right and the results of the last expression in the <case clause> are
|
|
returned as the results of the <tt>case</tt> expression. Otherwise, the
|
|
comparison process continues. If the result of
|
|
evaluating <key> is different from every datum in each set, then if
|
|
there is an <tt>else</tt> clause its expressions are evaluated and the
|
|
results of the last are the results of the <tt>case</tt> expression;
|
|
otherwise the <tt>case</tt> expression returns unspecified values.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(case (* 2 3)
|
|
<p class=nopadding> ((2 3 5 7) 'prime)</p>
|
|
|
|
<p class=nopadding> ((1 4 6 8 9) 'composite)) <span style="margin-left: 2em">‌</span>⇒ composite</p>
|
|
|
|
<p class=nopadding>(case (car '(c d))</p>
|
|
|
|
<p class=nopadding> ((a) 'a)</p>
|
|
|
|
<p class=nopadding> ((b) 'b)) <span style="margin-left: 2em">‌</span>⇒ <em>unspecified</em></p>
|
|
|
|
<p class=nopadding>(case (car '(c d))</p>
|
|
|
|
<p class=nopadding> ((a e i o u) 'vowel)</p>
|
|
|
|
<p class=nopadding> ((w y) 'semivowel)</p>
|
|
|
|
<p class=nopadding> (else 'consonant)) <span style="margin-left: 2em">‌</span>⇒ consonant</p>
|
|
<p></tt></p>
|
|
<p>
|
|
The last <expression> of a <case clause> is in tail
|
|
context if the <tt>case</tt> expression itself is; see
|
|
section <a href="r6rs-Z-H-14.html#node_sec_11.20">11.20</a>.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_388"></a>and<i> <test<sub>1</sub>> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
<em>Syntax: </em>The <test>s must be expressions.</p>
|
|
<p>
|
|
<em>Semantics: </em>If there are no <test>s, <tt>#t</tt> is returned.
|
|
Otherwise, the <test> expressions are evaluated from left to
|
|
right until a <test> returns <tt>#f</tt> or the last
|
|
<test> is reached. In the former case, the <tt>and</tt> expression
|
|
returns <tt>#f</tt> without evaluating the remaining expressions.
|
|
In the latter case, the last expression is evaluated and its values
|
|
are returned.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(and (= 2 2) (> 2 1)) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt>
|
|
<p class=nopadding>(and (= 2 2) (< 2 1)) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(and 1 2 'c '(f g)) <span style="margin-left: 2em">‌</span>⇒ (f g)</p>
|
|
|
|
<p class=nopadding>(and) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
<p></tt></p>
|
|
<p>
|
|
The <tt>and</tt> keyword could be defined in terms of <tt>if</tt> using <tt>syntax-rules</tt> (see section <a href="r6rs-Z-H-14.html#node_sec_11.19">11.19</a>) as follows:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(define-syntax <tt>and</tt>
|
|
<p class=nopadding> (syntax-rules ()</p>
|
|
|
|
<p class=nopadding> ((and) <tt>#t</tt>)</p>
|
|
|
|
<p class=nopadding> ((and test) test)</p>
|
|
|
|
<p class=nopadding> ((and test1 test2 ...)</p>
|
|
|
|
<p class=nopadding> (if test1 (and test2 ...) <tt>#f</tt>))))</p>
|
|
<p></tt></p>
|
|
<p>
|
|
The last <test> expression is in tail context if the <tt>and</tt>
|
|
expression itself is; see section <a href="r6rs-Z-H-14.html#node_sec_11.20">11.20</a>.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_390"></a>or<i> <test<sub>1</sub>> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
<em>Syntax: </em>The <test>s must be expressions.</p>
|
|
<p>
|
|
<em>Semantics: </em>If there are no <test>s, <tt>#f</tt> is returned.
|
|
Otherwise, the <test> expressions are evaluated from left to
|
|
right until a <test> returns a true value <i>val</i>
|
|
(see section <a href="r6rs-Z-H-8.html#node_sec_5.7">5.7</a>) or the last
|
|
<test> is reached. In the former case, the <tt>or</tt> expression
|
|
returns <i>val</i> without evaluating the remaining expressions.
|
|
In the latter case, the last expression is evaluated and its values
|
|
are returned.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(or (= 2 2) (> 2 1)) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt>
|
|
<p class=nopadding>(or (= 2 2) (< 2 1)) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(or <tt>#f</tt> <tt>#f</tt> <tt>#f</tt>) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(or '(b c) (/ 3 0)) <span style="margin-left: 2em">‌</span>⇒ (b c)</p>
|
|
<p></tt></p>
|
|
<p>
|
|
The <tt>or</tt> keyword could be defined in terms of <tt>if</tt> using <tt>syntax-rules</tt> (see section <a href="r6rs-Z-H-14.html#node_sec_11.19">11.19</a>) as follows:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(define-syntax <tt>or</tt>
|
|
<p class=nopadding> (syntax-rules ()</p>
|
|
|
|
<p class=nopadding> ((or) <tt>#f</tt>)</p>
|
|
|
|
<p class=nopadding> ((or test) test)</p>
|
|
|
|
<p class=nopadding> ((or test1 test2 ...)</p>
|
|
|
|
<p class=nopadding> (let ((x test1))</p>
|
|
|
|
<p class=nopadding> (if x x (or test2 ...))))))</p>
|
|
<p></tt></p>
|
|
<p>
|
|
The last <test> expression is in tail context if the <tt>or</tt>
|
|
expression itself is; see section <a href="r6rs-Z-H-14.html#node_sec_11.20">11.20</a>.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.4.6"></a>
|
|
<h3 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.4.6">11.4.6 Binding constructs</a></h3>
|
|
<p>The binding constructs described in this section
|
|
create local bindings for variables that are visible only in a
|
|
delimited region. The syntax of the
|
|
constructs
|
|
<tt>let</tt>, <tt>let*</tt>, <tt>letrec</tt>, and <tt>letrec*</tt>
|
|
is identical, but they differ in the regions<a name="node_idx_392"></a>(see section <a href="r6rs-Z-H-8.html#node_sec_5.2">5.2</a>) they establish
|
|
for their variable bindings and in the order in which the values for
|
|
the bindings are computed. In a <tt>let</tt> expression, the initial
|
|
values are computed before any of the variables become bound; in a
|
|
<tt>let*</tt> expression, the bindings and evaluations are performed
|
|
sequentially. In a <tt>letrec</tt> or <tt>letrec*</tt>
|
|
expression, all the bindings are in
|
|
effect while their initial values are being computed, thus allowing
|
|
mutually recursive definitions. In a <tt>letrec</tt> expression, the
|
|
initial values are computed before being assigned to the variables;
|
|
in a <tt>letrec*</tt>, the evaluations and assignments are performed
|
|
sequentially.</p>
|
|
<p>
|
|
In addition, the binding constructs <tt>let-values</tt> and <tt>let*-values</tt> generalize <tt>let</tt> and <tt>let*</tt> to allow multiple
|
|
variables to be bound to the results of expressions that evaluate to
|
|
multiple values.
|
|
They are analogous to <tt>let</tt> and <tt>let*</tt> in the
|
|
way they establish regions: in a <tt>let-values</tt> expression, the
|
|
initial values are computed before any of the variables become bound;
|
|
in a <tt>let*-values</tt> expression, the bindings are performed
|
|
sequentially. </p>
|
|
<p>
|
|
Sample definitions of all the binding forms of this section in terms
|
|
of simpler forms are in appendix <a href="r6rs-Z-H-16.html#node_chap_B">B</a>.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_394"></a>let<i> <bindings> <body></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
<em>Syntax: </em><Bindings> must have the form
|
|
</p>
|
|
|
|
<tt>((<variable<sub>1</sub>> <init<sub>1</sub>>) <tt>...</tt>),<p></tt>
|
|
where each <init> is an expression, and <body>
|
|
is as described in section <a href="r6rs-Z-H-14.html#node_sec_11.3">11.3</a>.
|
|
Any variable must not appear more than once in the <variable>s.</p>
|
|
<p>
|
|
<em>Semantics: </em>The <init>s are evaluated in the current environment (in some
|
|
unspecified order), the <variable>s are bound to fresh locations
|
|
holding the results, the <body> is evaluated in the extended
|
|
environment, and the values of the last expression of <body>
|
|
are returned. Each binding of a <variable> has <body> as its
|
|
region.<a name="node_idx_396"></a></p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let ((x 2) (y 3))
|
|
<p class=nopadding> (* x y)) <span style="margin-left: 2em">‌</span>⇒ 6</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(let ((x 2) (y 3))</p>
|
|
|
|
<p class=nopadding> (let ((x 7)</p>
|
|
|
|
<p class=nopadding> (z (+ x y)))</p>
|
|
|
|
<p class=nopadding> (* z x))) <span style="margin-left: 2em">‌</span>⇒ 35</p>
|
|
<p></tt></p>
|
|
<p>
|
|
See also named <tt>let</tt>, section <a href="r6rs-Z-H-14.html#node_sec_11.16">11.16</a>.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_398"></a>let*<i> <bindings> <body></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
<em>Syntax: </em><Bindings> must have the form
|
|
</p>
|
|
|
|
<tt>((<variable<sub>1</sub>> <init<sub>1</sub>>) <tt>...</tt>),<p></tt>
|
|
where each <init> is an expression, and <body>
|
|
is as described in section <a href="r6rs-Z-H-14.html#node_sec_11.3">11.3</a>.</p>
|
|
<p>
|
|
<em>Semantics: </em>The <tt>let*</tt> form is similar to <tt>let</tt>, but the <init>s are
|
|
evaluated and bindings created sequentially from left to right, with
|
|
the region<a name="node_idx_400"></a>of each binding including the bindings to
|
|
its right as well as <body>. Thus the second <init> is evaluated
|
|
in an environment in which the first binding is visible and initialized,
|
|
and so on.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let ((x 2) (y 3))
|
|
<p class=nopadding> (let* ((x 7)</p>
|
|
|
|
<p class=nopadding> (z (+ x y)))</p>
|
|
|
|
<p class=nopadding> (* z x))) <span style="margin-left: 2em">‌</span>⇒ 70</p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
While the variables bound by a <tt>let</tt> expression must be distinct,
|
|
the variables bound by a <tt>let*</tt> expression need not be distinct.
|
|
</blockquote>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_402"></a>letrec<i> <bindings> <body></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
<em>Syntax: </em><Bindings> must have the form
|
|
</p>
|
|
|
|
<tt>((<variable<sub>1</sub>> <init<sub>1</sub>>) <tt>...</tt>),<p></tt>
|
|
where each <init> is an expression, and <body>
|
|
is as described in section <a href="r6rs-Z-H-14.html#node_sec_11.3">11.3</a>. Any
|
|
variable must not appear more than once in the
|
|
<variable>s.</p>
|
|
<p>
|
|
<em>Semantics: </em>The <variable>s are bound to fresh locations, the <init>s
|
|
are evaluated in the resulting environment (in
|
|
some unspecified order), each <variable> is assigned to the result
|
|
of the corresponding <init>, the <body> is evaluated in the
|
|
resulting environment, and the values of the last expression in
|
|
<body> are returned. Each binding of a <variable> has the
|
|
entire <tt>letrec</tt> expression as its region<a name="node_idx_404"></a>, making it possible to
|
|
define mutually recursive procedures.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(letrec ((even?
|
|
<p class=nopadding> (lambda (n)</p>
|
|
|
|
<p class=nopadding> (if (zero? n)</p>
|
|
|
|
<p class=nopadding> <tt>#t</tt></p>
|
|
|
|
<p class=nopadding> (odd? (- n 1)))))</p>
|
|
|
|
<p class=nopadding> (odd?</p>
|
|
|
|
<p class=nopadding> (lambda (n)</p>
|
|
|
|
<p class=nopadding> (if (zero? n)</p>
|
|
|
|
<p class=nopadding> <tt>#f</tt></p>
|
|
|
|
<p class=nopadding> (even? (- n 1))))))</p>
|
|
|
|
<p class=nopadding> (even? 88)) </p>
|
|
|
|
<p class=nopadding> <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
<p></tt></p>
|
|
<p>
|
|
It should be possible
|
|
to evaluate each <init> without assigning or referring to the
|
|
value of any <variable>. In the most
|
|
common uses of <tt>letrec</tt>, all the <init>s are <tt>lambda</tt> expressions
|
|
and the restriction is satisfied automatically.
|
|
Another restriction is that the continuation of each <init> should not be invoked
|
|
more than once.</p>
|
|
<p>
|
|
<em>Implementation responsibilities: </em>Implementations must detect references to a <variable> during the
|
|
evaluation of the <init> expressions (using one particular
|
|
evaluation order and order of evaluating the <init> expressions).
|
|
If an implementation detects such a violation of the
|
|
restriction, it must raise an exception with condition type
|
|
<tt>&assertion</tt>.
|
|
Implementations may or may not detect that the continuation of each
|
|
<init> is invoked more than once. However, if the
|
|
implementation detects this, it must raise an exception with condition
|
|
type <tt>&assertion</tt>.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_406"></a>letrec*<i> <bindings> <body></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
<em>Syntax: </em><Bindings> must have the form
|
|
</p>
|
|
|
|
<tt>((<variable<sub>1</sub>> <init<sub>1</sub>>) <tt>...</tt>),<p></tt>
|
|
where each <init> is an expression, and <body>
|
|
is as described in section <a href="r6rs-Z-H-14.html#node_sec_11.3">11.3</a>.
|
|
Any variable must not appear more than once in the
|
|
<variable>s.</p>
|
|
<p>
|
|
<em>Semantics: </em>The <variable>s are bound to fresh locations,
|
|
each <variable> is assigned in left-to-right order to the
|
|
result of evaluating the corresponding <init>, the <body> is
|
|
evaluated in the resulting environment, and the values of the last
|
|
expression in <body> are returned.
|
|
Despite the left-to-right evaluation and assignment order, each binding of
|
|
a <variable> has the entire <tt>letrec*</tt> expression as its
|
|
region<a name="node_idx_408"></a>, making it possible to define mutually recursive
|
|
procedures.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(letrec* ((p
|
|
<p class=nopadding> (lambda (x)</p>
|
|
|
|
<p class=nopadding> (+ 1 (q (- x 1)))))</p>
|
|
|
|
<p class=nopadding> (q</p>
|
|
|
|
<p class=nopadding> (lambda (y)</p>
|
|
|
|
<p class=nopadding> (if (zero? y)</p>
|
|
|
|
<p class=nopadding> 0</p>
|
|
|
|
<p class=nopadding> (+ 1 (p (- y 1))))))</p>
|
|
|
|
<p class=nopadding> (x (p 5))</p>
|
|
|
|
<p class=nopadding> (y x))</p>
|
|
|
|
<p class=nopadding> y)</p>
|
|
|
|
<p class=nopadding> <span style="margin-left: 2em">‌</span>⇒ 5</p>
|
|
<p></tt></p>
|
|
<p>
|
|
It must be possible
|
|
to evaluate each <init> without assigning or referring to the value
|
|
of the corresponding <variable> or the <variable> of any of
|
|
the bindings that follow it in <bindings>.
|
|
Another restriction is that the continuation of each <init> should not be invoked
|
|
more than once.</p>
|
|
<p>
|
|
<em>Implementation responsibilities: </em>Implementations must, during the evaluation of an
|
|
<init> expression, detect references to the
|
|
value of the corresponding <variable> or the <variable> of
|
|
any of the bindings that follow it in <bindings>.
|
|
If an implementation detects such a
|
|
violation of the restriction, it must raise an exception with
|
|
condition type <tt>&assertion</tt>. Implementations may or may not
|
|
detect that the continuation of each <init> is invoked more than
|
|
once. However, if the implementation detects this, it must raise an
|
|
exception with condition type <tt>&assertion</tt>.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_410"></a>let-values<i> <mv-bindings> <body></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
<em>Syntax: </em><Mv-bindings> must have the form
|
|
</p>
|
|
|
|
<tt>((<formals<sub>1</sub>> <init<sub>1</sub>>) <tt>...</tt>),<p></tt>
|
|
where each <init> is an expression, and <body>
|
|
is as described in section <a href="r6rs-Z-H-14.html#node_sec_11.3">11.3</a>.
|
|
Any variable must not appear more
|
|
than once in the set of <formals>.</p>
|
|
<p>
|
|
<em>Semantics: </em>The <init>s are evaluated in the current environment
|
|
(in some unspecified order), and the variables occurring in the
|
|
<formals> are bound to fresh locations containing the values
|
|
returned by the <init>s, where the <formals> are matched
|
|
to the return values in the same way that the <formals> in a
|
|
<tt>lambda</tt> expression are matched to the arguments in a procedure call.
|
|
Then, the <body> is evaluated in the extended environment, and the
|
|
values of the last expression of <body> are returned.
|
|
Each binding of a variable has <body> as its
|
|
region.<a name="node_idx_412"></a>If the <formals> do not match, an exception with condition type
|
|
<tt>&assertion</tt> is raised.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let-values (((a b) (values 1 2))
|
|
<p class=nopadding> ((c d) (values 3 4)))</p>
|
|
|
|
<p class=nopadding> (list a b c d)) <span style="margin-left: 2em">‌</span>⇒ (1 2 3 4)</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(let-values (((a b . c) (values 1 2 3 4)))</p>
|
|
|
|
<p class=nopadding> (list a b c)) <span style="margin-left: 2em">‌</span>⇒ (1 2 (3 4))</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(let ((a 'a) (b 'b) (x 'x) (y 'y))</p>
|
|
|
|
<p class=nopadding> (let-values (((a b) (values x y))</p>
|
|
|
|
<p class=nopadding> ((x y) (values a b)))</p>
|
|
|
|
<p class=nopadding> (list a b x y))) <span style="margin-left: 2em">‌</span>⇒ (x y a b)</p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_414"></a>let*-values<i> <mv-bindings> <body></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
<em>Syntax: </em><Mv-bindings> must have the form
|
|
</p>
|
|
|
|
<tt>((<formals<sub>1</sub>> <init<sub>1</sub>>) <tt>...</tt>),<p></tt>
|
|
where each <init> is an expression, and <body>
|
|
is as described in section <a href="r6rs-Z-H-14.html#node_sec_11.3">11.3</a>.
|
|
In each <formals>, any variable must not appear more than once.</p>
|
|
<p>
|
|
<em>Semantics: </em>The <tt>let*-values</tt> form is similar to <tt>let-values</tt>, but the <init>s are
|
|
evaluated and bindings created sequentially from left to right, with
|
|
the region<a name="node_idx_416"></a>of the bindings of each <formals> including
|
|
the bindings to its right as well as <body>.
|
|
Thus the second <init> is evaluated in an environment in which the
|
|
bindings of the first <formals> is visible and initialized, and so
|
|
on.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let ((a 'a) (b 'b) (x 'x) (y 'y))
|
|
<p class=nopadding> (let*-values (((a b) (values x y))</p>
|
|
|
|
<p class=nopadding> ((x y) (values a b)))</p>
|
|
|
|
<p class=nopadding> (list a b x y))) <span style="margin-left: 2em">‌</span>⇒ (x y x y)</p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
While all of the variables bound by a <tt>let-values</tt> expression
|
|
must be distinct, the variables bound by different <formals> of a
|
|
<tt>let*-values</tt> expression need not be distinct.
|
|
</blockquote><p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.4.7"></a>
|
|
<h3 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.4.7">11.4.7 Sequencing</a></h3>
|
|
<p><div style="height: -9.0pt"></div>
|
|
<p style="margin-top: 0pt; margin-bottom: 0pt"></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_418"></a>begin<i> <form> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_420"></a>begin<i> <expression> <expression> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
The <begin> keyword has two different roles, depending on its
|
|
context:
|
|
</p>
|
|
<ul>
|
|
<li><p>It may appear as a form in a <body> (see
|
|
section <a href="r6rs-Z-H-14.html#node_sec_11.3">11.3</a>), <library body> (see
|
|
section <a href="r6rs-Z-H-10.html#node_sec_7.1">7.1</a>), or <top-level body> (see
|
|
chapter <a href="r6rs-Z-H-11.html#node_chap_8">8</a>), or directly nested in a <tt>begin</tt>
|
|
form that appears in a body. In this case, the <tt>begin</tt> form
|
|
must have the shape specified in the first header line. This use of
|
|
<tt>begin</tt> acts as a <a name="node_idx_422"></a><em>splicing</em> form—the forms inside
|
|
the <body> are spliced into the surrounding body, as if the
|
|
<tt>begin</tt> wrapper were not actually present.</p>
|
|
<p>
|
|
A <tt>begin</tt> form in a <body> or <library body> must
|
|
be non-empty if it appears after the first <expression>
|
|
within the body.
|
|
</p>
|
|
<li><p>It may appear as an ordinary expression and must have the shape
|
|
specified in the second header line. In this case, the
|
|
<expression>s are evaluated sequentially from left to right,
|
|
and the values of the last <expression> are returned.
|
|
This expression type is used to sequence side effects such as
|
|
assignments or input
|
|
and output.
|
|
</p>
|
|
</ul><p></p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(define x 0)
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(begin (set! x 5)</p>
|
|
|
|
<p class=nopadding> (+ x 1)) <span style="margin-left: 2em">‌</span>⇒ 6</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(begin (display "4 plus 1 equals ")</p>
|
|
|
|
<p class=nopadding> (display (+ 4 1))) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding> <em> and prints</em> 4 plus 1 equals 5</p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.5"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.5">11.5 Equivalence predicates</a></h2>
|
|
<p></p>
|
|
<p>
|
|
A <a name="node_idx_424"></a><em>predicate</em> is a procedure that always returns a boolean
|
|
value (<tt>#t</tt> or <tt>#f</tt>). An <a name="node_idx_426"></a><em>equivalence predicate</em> is
|
|
the computational analogue of a mathematical equivalence relation (it is
|
|
symmetric, reflexive, and transitive). Of the equivalence predicates
|
|
described in this section, <tt>eq?</tt> is the finest or most
|
|
discriminating, and <tt>equal?</tt> is the coarsest. The <tt>eqv?</tt> predicate is
|
|
slightly less discriminating than <tt>eq?</tt>. </p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_428"></a>eqv?<i> <i>obj<sub>1</sub></i> <i>obj<sub>2</sub></i></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
The <tt>eqv?</tt> procedure defines a useful equivalence relation on objects.
|
|
Briefly, it returns <tt>#t</tt> if <i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> should
|
|
normally be regarded as the same object and <tt>#f</tt> otherwise. This relation is left slightly
|
|
open to interpretation, but the following partial specification of
|
|
<tt>eqv?</tt> must hold for all implementations.</p>
|
|
<p>
|
|
The <tt>eqv?</tt> procedure returns <tt>#t</tt> if one of the following holds:</p>
|
|
<p>
|
|
</p>
|
|
<ul>
|
|
<li><p><i>Obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are both booleans and are the same
|
|
according to the <tt>boolean=?</tt> procedure (section <a href="r6rs-Z-H-14.html#node_sec_11.8">11.8</a>).</p>
|
|
<p>
|
|
</p>
|
|
<li><p><i>Obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are both symbols and are the same
|
|
according to the <tt>symbol=?</tt> procedure (section <a href="r6rs-Z-H-14.html#node_sec_11.10">11.10</a>).</p>
|
|
<p>
|
|
</p>
|
|
<li><p><i>Obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are both exact<a name="node_idx_430"></a>number objects
|
|
and are numerically equal (see <tt>=</tt>,
|
|
section <a href="r6rs-Z-H-14.html#node_sec_11.7">11.7</a>).</p>
|
|
<p>
|
|
</p>
|
|
<li><p><i>Obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are both inexact<a name="node_idx_432"></a>number objects, are numerically
|
|
equal (see <tt>=</tt>, section <a href="r6rs-Z-H-14.html#node_sec_11.7">11.7</a>), and
|
|
yield the same results (in the sense of <tt>eqv?</tt>) when passed
|
|
as arguments to any other procedure that can be defined
|
|
as a finite composition of Scheme's standard arithmetic
|
|
procedures.</p>
|
|
<p>
|
|
</p>
|
|
<li><p><i>Obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are both characters and are the same
|
|
character according to the <tt>char=?</tt> procedure
|
|
(section <a href="r6rs-Z-H-14.html#node_sec_11.11">11.11</a>).</p>
|
|
<p>
|
|
</p>
|
|
<li><p>Both <i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are the empty list.</p>
|
|
<p>
|
|
</p>
|
|
<li><p><i>Obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are objects such as pairs, vectors, bytevectors
|
|
(library chapter on “Bytevectors”),
|
|
strings, hashtables, records (library
|
|
chapter on “Records”), ports (library
|
|
section on “Port I/O”), or hashtables
|
|
(library chapter on “Hash tables”) that
|
|
refer to the same locations in the store (section <a href="r6rs-Z-H-8.html#node_sec_5.10">5.10</a>).</p>
|
|
<p>
|
|
</p>
|
|
<li><p><i>Obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are record-type descriptors that are
|
|
specified to be <tt>eqv?</tt> in library
|
|
section on “Procedural layer”.
|
|
</p>
|
|
</ul><p></p>
|
|
<p>
|
|
The <tt>eqv?</tt> procedure returns <tt>#f</tt> if one of the following holds:</p>
|
|
<p>
|
|
</p>
|
|
<ul>
|
|
<li><p><i>Obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are of different types
|
|
(section <a href="r6rs-Z-H-14.html#node_sec_11.1">11.1</a>).</p>
|
|
<p>
|
|
</p>
|
|
<li><p><i>Obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are booleans for which the <tt>boolean=?</tt> procedure returns <tt>#f</tt>.</p>
|
|
<p>
|
|
</p>
|
|
<li><p><i>Obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are symbols for which the <tt>symbol=?</tt> procedure returns <tt>#f</tt>.</p>
|
|
<p>
|
|
</p>
|
|
<li><p>One of <i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> is an exact number object but the other is
|
|
an inexact number object.</p>
|
|
<p>
|
|
</p>
|
|
<li><p><i>Obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are rational number objects for which the <tt>=</tt> procedure
|
|
returns <tt>#f</tt>.</p>
|
|
<p>
|
|
</p>
|
|
<li><p><i>Obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> yield different results (in the sense of
|
|
<tt>eqv?</tt>) when passed as arguments to any other procedure
|
|
that can be defined as a finite composition of Scheme's
|
|
standard arithmetic procedures.</p>
|
|
<p>
|
|
</p>
|
|
<li><p><i>Obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are characters for which the <tt>char=?</tt>
|
|
procedure returns <tt>#f</tt>.</p>
|
|
<p>
|
|
</p>
|
|
<li><p>One of <i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> is the empty list, but the other is not.</p>
|
|
<p>
|
|
</p>
|
|
<li><p><i>Obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are objects such as pairs, vectors,
|
|
bytevectors (library
|
|
chapter on “Bytevectors”), strings,
|
|
records (library
|
|
chapter on “Records”), ports (library
|
|
section on “Port I/O”), or hashtables
|
|
(library chapter on “Hashtables”) that
|
|
refer to distinct locations.</p>
|
|
<p>
|
|
</p>
|
|
<li><p><i>Obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are pairs, vectors, strings, or
|
|
records, or hashtables, where the applying the same accessor (i.e.
|
|
<tt>car</tt>, <tt>cdr</tt>, <tt>vector-ref</tt>, <tt>string-ref</tt>, or record
|
|
accessors) to both yields results for which <tt>eqv?</tt> returns
|
|
<tt>#f</tt>.</p>
|
|
<p>
|
|
</p>
|
|
<li><p><i>Obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are procedures that would behave differently
|
|
(return different values or have different side effects) for some arguments.</p>
|
|
<p>
|
|
</p>
|
|
</ul><p></p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
The <tt>eqv?</tt> procedure returning <tt>#t</tt> when <i>obj<sub>1</sub></i> and
|
|
<i>obj<sub>2</sub></i> are number objects does not imply that <tt>=</tt> would also
|
|
return <tt>#t</tt> when called with <i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> as
|
|
arguments.
|
|
</blockquote><p>
|
|
</p>
|
|
|
|
<tt>(eqv? 'a 'a) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt>
|
|
<p class=nopadding>(eqv? 'a 'b) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(eqv? 2 2) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(eqv? '() '()) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(eqv? 100000000 100000000) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(eqv? (cons 1 2) (cons 1 2)) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(eqv? (lambda () 1)</p>
|
|
|
|
<p class=nopadding> (lambda () 2)) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(eqv? #f 'nil) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
<p></tt></p>
|
|
<p>
|
|
The following examples illustrate cases in which the above rules do
|
|
not fully specify the behavior of <tt>eqv?</tt>. All that can be said
|
|
about such cases is that the value returned by <tt>eqv?</tt> must be a
|
|
boolean.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let ((p (lambda (x) x)))
|
|
<p class=nopadding> (eqv? p p)) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding>(eqv? "" "") <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding>(eqv? '#() '#()) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding>(eqv? (lambda (x) x)</p>
|
|
|
|
<p class=nopadding> (lambda (x) x)) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding>(eqv? (lambda (x) x)</p>
|
|
|
|
<p class=nopadding> (lambda (y) y)) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding>(eqv? +nan.0 +nan.0) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
<p></tt></p>
|
|
<p>
|
|
The next set of examples shows the use of <tt>eqv?</tt> with procedures
|
|
that have local state. Calls to <tt>gen-counter</tt> must return a
|
|
distinct procedure every time, since each procedure has its own
|
|
internal counter. Calls to <tt>gen-loser</tt> return procedures that
|
|
behave equivalently when called. However, <tt>eqv?</tt> may
|
|
not detect this equivalence.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(define gen-counter
|
|
<p class=nopadding> (lambda ()</p>
|
|
|
|
<p class=nopadding> (let ((n 0))</p>
|
|
|
|
<p class=nopadding> (lambda () (set! n (+ n 1)) n))))</p>
|
|
|
|
<p class=nopadding>(let ((g (gen-counter)))</p>
|
|
|
|
<p class=nopadding> (eqv? g g)) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding>(eqv? (gen-counter) (gen-counter))</p>
|
|
|
|
<p class=nopadding> <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(define gen-loser</p>
|
|
|
|
<p class=nopadding> (lambda ()</p>
|
|
|
|
<p class=nopadding> (let ((n 0))</p>
|
|
|
|
<p class=nopadding> (lambda () (set! n (+ n 1)) 27))))</p>
|
|
|
|
<p class=nopadding>(let ((g (gen-loser)))</p>
|
|
|
|
<p class=nopadding> (eqv? g g)) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding>(eqv? (gen-loser) (gen-loser))</p>
|
|
|
|
<p class=nopadding> <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(letrec ((f (lambda () (if (eqv? f g) 'both 'f)))</p>
|
|
|
|
<p class=nopadding> (g (lambda () (if (eqv? f g) 'both 'g))))</p>
|
|
|
|
<p class=nopadding> (eqv? f g)) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(letrec ((f (lambda () (if (eqv? f g) 'f 'both)))</p>
|
|
|
|
<p class=nopadding> (g (lambda () (if (eqv? f g) 'g 'both))))</p>
|
|
|
|
<p class=nopadding> (eqv? f g)) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
<p></tt></p>
|
|
<p>
|
|
Implementations may
|
|
share structure between constants where appropriate.
|
|
Furthermore, a constant may be copied at any time by the implementation so
|
|
as to exist simultaneously in different sets of locations, as noted in
|
|
section <a href="r6rs-Z-H-14.html#node_sec_11.4.1">11.4.1</a>.
|
|
Thus the value of <tt>eqv?</tt> on constants is sometimes
|
|
implementation-dependent.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(eqv? '(a) '(a)) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i>
|
|
<p class=nopadding>(eqv? "a" "a") <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding>(eqv? '(b) (cdr '(a b))) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding>(let ((x '(a)))</p>
|
|
|
|
<p class=nopadding> (eqv? x x)) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_434"></a>eq?<i> <i>obj<sub>1</sub></i> <i>obj<sub>2</sub></i></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
The <tt>eq?</tt> predicate is similar to <tt>eqv?</tt> except that in some cases it is
|
|
capable of discerning distinctions finer than those detectable by
|
|
<tt>eqv?</tt>.</p>
|
|
<p>
|
|
The <tt>eq?</tt> and <tt>eqv?</tt> predicates are guaranteed to have the
|
|
same behavior on symbols, booleans, the empty list, pairs, procedures,
|
|
non-empty strings, bytevectors, and vectors, and records. The
|
|
behavior of <tt>eq?</tt> on number objects and characters is
|
|
implementation-dependent, but it always returns either <tt>#t</tt> or
|
|
<tt>#f</tt>, and returns <tt>#t</tt> only when <tt>eqv?</tt> would also
|
|
return <tt>#t</tt>. The <tt>eq?</tt> predicate may also behave differently
|
|
from <tt>eqv?</tt> on empty vectors, empty bytevectors, and empty strings.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(eq? 'a 'a) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt>
|
|
<p class=nopadding>(eq? '(a) '(a)) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding>(eq? (list 'a) (list 'a)) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(eq? "a" "a") <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding>(eq? "" "") <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding>(eq? '() '()) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(eq? 2 2) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding>(eq? #<tt>\</tt>A #<tt>\</tt>A) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding>(eq? car car) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(let ((n (+ 2 3)))</p>
|
|
|
|
<p class=nopadding> (eq? n n)) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding>(let ((x '(a)))</p>
|
|
|
|
<p class=nopadding> (eq? x x)) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(let ((x '#()))</p>
|
|
|
|
<p class=nopadding> (eq? x x)) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding>(let ((p (lambda (x) x)))</p>
|
|
|
|
<p class=nopadding> (eq? p p)) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_436"></a>equal?<i> <i>obj<sub>1</sub></i> <i>obj<sub>2</sub></i></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
The <tt>equal?</tt> predicate returns <tt>#t</tt> if and only if the
|
|
(possibly infinite) unfoldings of its arguments into regular trees are
|
|
equal as ordered trees.</p>
|
|
<p>
|
|
The <tt>equal?</tt> predicate treats pairs and vectors
|
|
as nodes with outgoing edges, uses <tt>string=?</tt> to compare strings, uses <tt>bytevector=?</tt> to compare bytevectors (see library chapter on “Bytevectors”),
|
|
and uses <tt>eqv?</tt> to compare other nodes.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(equal? 'a 'a) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt>
|
|
<p class=nopadding>(equal? '(a) '(a)) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(equal? '(a (b) c)</p>
|
|
|
|
<p class=nopadding> '(a (b) c)) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(equal? "abc" "abc") <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(equal? 2 2) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(equal? (make-vector 5 'a)</p>
|
|
|
|
<p class=nopadding> (make-vector 5 'a)) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(equal? '#vu8(1 2 3 4 5)</p>
|
|
|
|
<p class=nopadding> (u8-list->bytevector</p>
|
|
|
|
<p class=nopadding> '(1 2 3 4 5)) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(equal? (lambda (x) x)</p>
|
|
|
|
<p class=nopadding> (lambda (y) y)) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(let* ((x (list 'a))</p>
|
|
|
|
<p class=nopadding> (y (list 'a))</p>
|
|
|
|
<p class=nopadding> (z (list x y)))</p>
|
|
|
|
<p class=nopadding> (list (equal? z (list y x))</p>
|
|
|
|
<p class=nopadding> (equal? z (list x x)))) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ (<tt>#t</tt> <tt>#t</tt>)</p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
The <tt>equal?</tt> procedure must always terminate, even if its
|
|
arguments contain cycles.
|
|
</blockquote><p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.6"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.6">11.6 Procedure predicate</a></h2>
|
|
<p></p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_438"></a>procedure?<i> obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns <tt>#t</tt> if <i>obj</i> is a procedure, otherwise returns <tt>#f</tt>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(procedure? car) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt>
|
|
<p class=nopadding>(procedure? 'car) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(procedure? (lambda (x) (* x x))) </p>
|
|
|
|
<p class=nopadding> <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(procedure? '(lambda (x) (* x x))) </p>
|
|
|
|
<p class=nopadding> <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.7"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.7">11.7 Arithmetic</a></h2>
|
|
<p></p>
|
|
<p>
|
|
The procedures described here implement arithmetic that is
|
|
generic over
|
|
the numerical tower described in chapter <a href="r6rs-Z-H-6.html#node_chap_3">3</a>.
|
|
The generic procedures described in this section
|
|
accept both exact and inexact number objects as arguments,
|
|
performing coercions and selecting the appropriate operations
|
|
as determined by the numeric subtypes of their arguments.</p>
|
|
<p>
|
|
Library chapter on “Arithmetic” describes
|
|
libraries that define other numerical procedures.</p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.7.1"></a>
|
|
<h3 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.7.1">11.7.1 Propagation of exactness and inexactness</a></h3>
|
|
<p></p>
|
|
<p>
|
|
The procedures listed below must return the mathematically correct exact result
|
|
provided all their arguments are exact:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>+ - *
|
|
<p class=nopadding>max min abs</p>
|
|
|
|
<p class=nopadding>numerator denominator gcd</p>
|
|
|
|
<p class=nopadding>lcm floor ceiling</p>
|
|
|
|
<p class=nopadding>truncate round rationalize</p>
|
|
|
|
<p class=nopadding>real-part imag-part make-rectangular</p>
|
|
<p></tt></p>
|
|
<p>
|
|
The procedures listed below must return the correct exact result
|
|
provided all their arguments are exact, and no divisors are zero:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>/
|
|
<p class=nopadding>div mod div-and-mod</p>
|
|
|
|
<p class=nopadding>div0 mod0 div0-and-mod0</p>
|
|
<p></tt></p>
|
|
<p>
|
|
Moreover, the procedure <tt>expt</tt> must return the correct exact
|
|
result provided its first argument is an exact real number object and
|
|
its second argument is an exact integer object.</p>
|
|
<p>
|
|
The general rule is that the generic operations return the correct
|
|
exact result when all of their arguments are exact and the result is
|
|
mathematically well-defined, but return an inexact result when any
|
|
argument is inexact. Exceptions to this rule include
|
|
<tt>sqrt</tt>, <tt>exp</tt>, <tt>log</tt>,
|
|
<tt>sin</tt>, <tt>cos</tt>, <tt>tan</tt>,
|
|
<tt>asin</tt>, <tt>acos</tt>, <tt>atan</tt>,
|
|
<tt>expt</tt>, <tt>make-polar</tt>, <tt>magnitude</tt>, and <tt>angle</tt>, which
|
|
may (but are not required to) return inexact results even when
|
|
given exact arguments, as indicated in the specification of these
|
|
procedures.</p>
|
|
<p>
|
|
One general exception to the rule above is that an implementation may
|
|
return an exact result despite inexact arguments if that exact result
|
|
would be the correct result for all possible substitutions of exact
|
|
arguments for the inexact ones. An example is <tt>(* 1.0 0)</tt> which
|
|
may return either <tt>0</tt> (exact) or <tt>0.0</tt> (inexact).</p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.7.2"></a>
|
|
<h3 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.7.2">11.7.2 Representability of infinities and NaNs</a></h3>
|
|
<p></p>
|
|
<p>
|
|
The specification of the numerical operations is written as though
|
|
infinities and NaNs are representable, and specifies many operations
|
|
with respect to these number objects in ways that are consistent with the
|
|
IEEE-754 standard for binary floating-point arithmetic.
|
|
An implementation of Scheme may or may not represent infinities and
|
|
NaNs; however,
|
|
an implementation must raise a continuable exception with
|
|
condition type <tt>&no-infinities</tt> or <tt>&no-nans</tt> (respectively;
|
|
see library section on “Flonums”)
|
|
whenever it is unable to represent an infinity or NaN as specified.
|
|
In this case, the continuation of the exception
|
|
handler is the continuation that otherwise would have received
|
|
the infinity or NaN value. This requirement also applies to
|
|
conversions between number objects and external representations, including
|
|
the reading of program source code.</p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.7.3"></a>
|
|
<h3 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.7.3">11.7.3 Semantics of common operations</a></h3>
|
|
<p>Some operations are the semantic basis for several arithmetic
|
|
procedures. The behavior of these operations is described in this
|
|
section for later reference.</p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.7.3.1"></a>
|
|
<h4 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.7.3.1">11.7.3.1 Integer division</a></h4>
|
|
<p></p>
|
|
<p>
|
|
Scheme's operations for performing integer
|
|
division rely on mathematical operations <em>d</em><em>i</em><em>v</em>,
|
|
<em>m</em><em>o</em><em>d</em>, <em>d</em><em>i</em><em>v</em><sub>0</sub>, and
|
|
<em>m</em><em>o</em><em>d</em><sub>0</sub>, that are defined as follows:</p>
|
|
<p>
|
|
<em>d</em><em>i</em><em>v</em>, <em>m</em><em>o</em><em>d</em>, <em>d</em><em>i</em><em>v</em><sub>0</sub>, and <em>m</em><em>o</em><em>d</em><sub>0</sub>
|
|
each accept two real numbers <em>x</em><sub>1</sub> and <em>x</em><sub>2</sub> as operands, where
|
|
<em>x</em><sub>2</sub> must be nonzero.</p>
|
|
<p>
|
|
<em>d</em><em>i</em><em>v</em> returns an integer, and <em>m</em><em>o</em><em>d</em> returns a real.
|
|
Their results are specified by
|
|
</p>
|
|
<a name="node_eqn_Temp_17"></a>
|
|
<div align=left><table width=100%>
|
|
<tr><td align=right>
|
|
<em>x</em><sub>1</sub> </td><td><table><tr><td align=center><em>d</em><em>i</em><em>v</em></td></tr></table></td><td> <em>x</em><sub>2</sub> </td><td align=center width=2%> = </td><td> <em>n</em><sub><em>d</em></sub></td></tr>
|
|
<tr><td align=right><em>x</em><sub>1</sub> </td><td><table><tr><td align=center><em>m</em><em>o</em><em>d</em></td></tr></table></td><td> <em>x</em><sub>2</sub> </td><td align=center width=2%> = </td><td> <em>x</em><sub><em>m</em></sub>
|
|
</td></tr>
|
|
</table></div><p>*
|
|
where
|
|
</p>
|
|
<div align=left><img src="r6rs-Z-G-2.gif" border="0" alt="[r6rs-Z-G-2.gif]"></div><p>
|
|
Examples:
|
|
</p>
|
|
<a name="node_eqn_Temp_18"></a>
|
|
<div align=left><table width=100%>
|
|
<tr><td align=right>
|
|
123 </td><td><table><tr><td align=center><em>d</em><em>i</em><em>v</em></td></tr></table></td><td> 10 </td><td align=center width=2%> = </td><td> 12</td></tr>
|
|
<tr><td align=right>123 </td><td><table><tr><td align=center><em>m</em><em>o</em><em>d</em></td></tr></table></td><td> 10 </td><td align=center width=2%> = </td><td> 3</td></tr>
|
|
<tr><td align=right>123 </td><td><table><tr><td align=center><em>d</em><em>i</em><em>v</em></td></tr></table></td><td> </td><td><table><tr><td align=center></td><td><table><tr><td align=center>−10</td></tr></table></td><td></td></tr></table></td><td> </td><td align=center width=2%> = </td><td> −12</td></tr>
|
|
<tr><td align=right>123 </td><td><table><tr><td align=center><em>m</em><em>o</em><em>d</em></td></tr></table></td><td> </td><td><table><tr><td align=center></td><td><table><tr><td align=center>−10</td></tr></table></td><td></td></tr></table></td><td> </td><td align=center width=2%> = </td><td> 3</td></tr>
|
|
<tr><td align=right>−123 </td><td><table><tr><td align=center><em>d</em><em>i</em><em>v</em></td></tr></table></td><td> 10 </td><td align=center width=2%> = </td><td> −13</td></tr>
|
|
<tr><td align=right>−123 </td><td><table><tr><td align=center><em>m</em><em>o</em><em>d</em></td></tr></table></td><td> 10 </td><td align=center width=2%> = </td><td> 7</td></tr>
|
|
<tr><td align=right>−123 </td><td><table><tr><td align=center><em>d</em><em>i</em><em>v</em></td></tr></table></td><td> </td><td><table><tr><td align=center></td><td><table><tr><td align=center>−10</td></tr></table></td><td></td></tr></table></td><td> </td><td align=center width=2%> = </td><td> 13</td></tr>
|
|
<tr><td align=right>−123 </td><td><table><tr><td align=center><em>m</em><em>o</em><em>d</em></td></tr></table></td><td> </td><td><table><tr><td align=center></td><td><table><tr><td align=center>−10</td></tr></table></td><td></td></tr></table></td><td> </td><td align=center width=2%> = </td><td> 7
|
|
</td></tr>
|
|
</table></div><p>*
|
|
<em>d</em><em>i</em><em>v</em><sub>0</sub> and <em>m</em><em>o</em><em>d</em><sub>0</sub> are like <em>d</em><em>i</em><em>v</em> and
|
|
<em>m</em><em>o</em><em>d</em>, except the result of <em>m</em><em>o</em><em>d</em><sub>0</sub> lies within a
|
|
half-open interval centered on zero. The results are specified by
|
|
</p>
|
|
<a name="node_eqn_Temp_19"></a>
|
|
<div align=left><table width=100%>
|
|
<tr><td align=right>
|
|
<em>x</em><sub>1</sub> </td><td><table><tr><td align=center><em>d</em><em>i</em><em>v</em></td></tr></table></td><td><sub>0</sub> <em>x</em><sub>2</sub> </td><td align=center width=2%> = </td><td> <em>n</em><sub><em>d</em></sub></td></tr>
|
|
<tr><td align=right><em>x</em><sub>1</sub> </td><td><table><tr><td align=center><em>m</em><em>o</em><em>d</em></td></tr></table></td><td><sub>0</sub> <em>x</em><sub>2</sub> </td><td align=center width=2%> = </td><td> <em>x</em><sub><em>m</em></sub>
|
|
</td></tr>
|
|
</table></div><p>*
|
|
where:
|
|
</p>
|
|
<div align=left><img src="r6rs-Z-G-3.gif" border="0" alt="[r6rs-Z-G-3.gif]"></div><p>
|
|
Examples:
|
|
</p>
|
|
<a name="node_eqn_Temp_20"></a>
|
|
<div align=left><table width=100%>
|
|
<tr><td align=right>
|
|
123 </td><td><table><tr><td align=center><em>d</em><em>i</em><em>v</em></td></tr></table></td><td><sub>0</sub> 10 </td><td align=center width=2%> = </td><td> 12</td></tr>
|
|
<tr><td align=right>123 </td><td><table><tr><td align=center><em>m</em><em>o</em><em>d</em></td></tr></table></td><td><sub>0</sub> 10 </td><td align=center width=2%> = </td><td> 3</td></tr>
|
|
<tr><td align=right>123 </td><td><table><tr><td align=center><em>d</em><em>i</em><em>v</em></td></tr></table></td><td><sub>0</sub> </td><td><table><tr><td align=center></td><td><table><tr><td align=center>−10</td></tr></table></td><td></td></tr></table></td><td> </td><td align=center width=2%> = </td><td> −12</td></tr>
|
|
<tr><td align=right>123 </td><td><table><tr><td align=center><em>m</em><em>o</em><em>d</em></td></tr></table></td><td><sub>0</sub> </td><td><table><tr><td align=center></td><td><table><tr><td align=center>−10</td></tr></table></td><td></td></tr></table></td><td> </td><td align=center width=2%> = </td><td> 3</td></tr>
|
|
<tr><td align=right>−123 </td><td><table><tr><td align=center><em>d</em><em>i</em><em>v</em></td></tr></table></td><td><sub>0</sub> 10 </td><td align=center width=2%> = </td><td> −12</td></tr>
|
|
<tr><td align=right>−123 </td><td><table><tr><td align=center><em>m</em><em>o</em><em>d</em></td></tr></table></td><td><sub>0</sub> 10 </td><td align=center width=2%> = </td><td> −3</td></tr>
|
|
<tr><td align=right>−123 </td><td><table><tr><td align=center><em>d</em><em>i</em><em>v</em></td></tr></table></td><td><sub>0</sub> </td><td><table><tr><td align=center></td><td><table><tr><td align=center>−10</td></tr></table></td><td></td></tr></table></td><td> </td><td align=center width=2%> = </td><td> 12</td></tr>
|
|
<tr><td align=right>−123 </td><td><table><tr><td align=center><em>m</em><em>o</em><em>d</em></td></tr></table></td><td><sub>0</sub> </td><td><table><tr><td align=center></td><td><table><tr><td align=center>−10</td></tr></table></td><td></td></tr></table></td><td> </td><td align=center width=2%> = </td><td> −3
|
|
</td></tr>
|
|
</table></div><p>*</p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.7.3.2"></a>
|
|
<h4 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.7.3.2">11.7.3.2 Transcendental functions</a></h4>
|
|
<p></p>
|
|
<p>
|
|
In general, the transcendental functions log, sin<sup>−1</sup>
|
|
(arcsine), cos<sup>−1</sup> (arccosine), and tan<sup>−1</sup> are multiply
|
|
defined. The value of log <em>z</em> is defined to be the one whose
|
|
imaginary part lies in the range from −<img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]"> (inclusive if −0.0 is
|
|
distinguished, exclusive otherwise) to <img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]"> (inclusive). log 0 is
|
|
undefined.</p>
|
|
<p>
|
|
The value of log <em>z</em> for non-real <em>z</em> is defined in terms of log on real numbers as </p>
|
|
<p>
|
|
</p>
|
|
<div align=left><img src="r6rs-Z-G-4.gif" border="0" alt="[r6rs-Z-G-4.gif]"></div><p>
|
|
where <em>a</em><em>n</em><em>g</em><em>l</em><em>e</em> <em>z</em> is the angle of <em>z</em> = <em>a</em> · <em>e</em><sup><em>i</em><em>b</em></sup> specified
|
|
as:
|
|
</p>
|
|
<div class=mathdisplay align=left><table><tr><td></td><td><table><tr><td align=center></td><td><table><tr><td align=center><em>a</em><em>n</em><em>g</em><em>l</em><em>e</em></td></tr></table></td><td> <em>z</em> = <em>b</em> + 2<img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]"> <em>n</em></td></tr></table></td><td></td></tr></table></div>
|
|
<p class=noindent>
|
|
with −<img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]"> ≤ <em>a</em><em>n</em><em>g</em><em>l</em><em>e</em> <em>z</em>≤ <img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]"> and <em>a</em><em>n</em><em>g</em><em>l</em><em>e</em> <em>z</em> =
|
|
<em>b</em> + 2<img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]"> <em>n</em> for some integer <em>n</em>.</p>
|
|
<p>
|
|
With the one-argument version of log defined this way, the values
|
|
of the two-argument-version of log, sin<sup>−1</sup> <em>z</em>, cos<sup>−1</sup> <em>z</em>,
|
|
tan<sup>−1</sup> <em>z</em>, and the two-argument version of tan<sup>−1</sup> are
|
|
according to the following formulæ:
|
|
</p>
|
|
<a name="node_eqn_Temp_21"></a>
|
|
<div align=left><table width=100%>
|
|
<tr><td align=right>
|
|
log <em>z</em> <em>b</em> </td><td align=center width=2%> = </td><td> (</td><td><table><tr><td align=center>log <em>z</em></td></tr></table></td><td>/</td><td><table><tr><td align=center>log <em>b</em></td></tr></table></td><td>)</td></tr>
|
|
<tr><td align=right>sin<sup>−1</sup> <em>z</em> </td><td align=center width=2%> = </td><td> −<em>i</em> log (<em>i</em> <em>z</em> + (1 − <em>z</em><sup>2</sup>)<sup>1/2</sup>)</td></tr>
|
|
<tr><td align=right>cos<sup>−1</sup> <em>z</em> </td><td align=center width=2%> = </td><td> <img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]"> / 2 − sin<sup>−1</sup> <em>z</em></td></tr>
|
|
<tr><td align=right>tan<sup>−1</sup> <em>z</em> </td><td align=center width=2%> = </td><td> (log (1 + <em>i</em> <em>z</em>) − log (1 − <em>i</em> <em>z</em>)) / (2 <em>i</em>)</td></tr>
|
|
<tr><td align=right>tan<sup>−1</sup> <em>x</em> <em>y</em> </td><td align=center width=2%> = </td><td> </td><td><table><tr><td align=center><em>a</em><em>n</em><em>g</em><em>l</em><em>e</em></td></tr></table></td><td>(<em>x</em> + <em>y</em><em>i</em>)
|
|
</td></tr>
|
|
</table></div><p>*</p>
|
|
<p>
|
|
The range of tan<sup>−1</sup> <em>x</em> <em>y</em> is as in the following table. The
|
|
asterisk (*) indicates that the entry applies to implementations that
|
|
distinguish minus zero.</p>
|
|
<p>
|
|
</p>
|
|
<div align=center><table><tr><td>
|
|
|
|
<table border=0><tr><td valign=top ></td><td valign=top ><em>y</em> condition </td><td valign=top ><em>x</em> condition </td><td valign=top >range of result <em>r</em></td></tr>
|
|
<tr><td valign=top ></td><td valign=top ><em>y</em> = 0.0 </td><td valign=top ><em>x</em> > 0.0 </td><td valign=top >0.0</td></tr>
|
|
<tr><td valign=top >∗ </td><td valign=top ><em>y</em> = + 0.0 </td><td valign=top ><em>x</em> > 0.0 </td><td valign=top > + 0.0</td></tr>
|
|
<tr><td valign=top >∗ </td><td valign=top ><em>y</em> = −0.0 </td><td valign=top ><em>x</em> > 0.0 </td><td valign=top >−0.0</td></tr>
|
|
<tr><td valign=top ></td><td valign=top ><em>y</em> > 0.0 </td><td valign=top ><em>x</em> > 0.0 </td><td valign=top >0.0 < <em>r</em> < (<img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]">/2)</td></tr>
|
|
<tr><td valign=top ></td><td valign=top ><em>y</em> > 0.0 </td><td valign=top ><em>x</em> = 0.0 </td><td valign=top >(<img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]">/2)</td></tr>
|
|
<tr><td valign=top ></td><td valign=top ><em>y</em> > 0.0 </td><td valign=top ><em>x</em> < 0.0 </td><td valign=top >(<img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]">/2) < <em>r</em> < <img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]"></td></tr>
|
|
<tr><td valign=top ></td><td valign=top ><em>y</em> = 0.0 </td><td valign=top ><em>x</em> < 0 </td><td valign=top ><img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]"></td></tr>
|
|
<tr><td valign=top >∗ </td><td valign=top ><em>y</em> = + 0.0 </td><td valign=top ><em>x</em> < 0.0 </td><td valign=top ><img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]"></td></tr>
|
|
<tr><td valign=top >∗ </td><td valign=top ><em>y</em> = −0.0 </td><td valign=top ><em>x</em> < 0.0 </td><td valign=top >−<img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]"></td></tr>
|
|
<tr><td valign=top ></td><td valign=top ><em>y</em> < 0.0 </td><td valign=top ><em>x</em> < 0.0 </td><td valign=top >−<img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]">< <em>r</em>< −(<img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]">/2)</td></tr>
|
|
<tr><td valign=top ></td><td valign=top ><em>y</em> < 0.0 </td><td valign=top ><em>x</em> = 0.0 </td><td valign=top >−(<img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]">/2)</td></tr>
|
|
<tr><td valign=top ></td><td valign=top ><em>y</em> < 0.0 </td><td valign=top ><em>x</em> > 0.0 </td><td valign=top >−(<img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]">/2) < <em>r</em>< 0.0</td></tr>
|
|
<tr><td valign=top ></td><td valign=top ><em>y</em> = 0.0 </td><td valign=top ><em>x</em> = 0.0 </td><td valign=top >undefined</td></tr>
|
|
<tr><td valign=top >∗</td><td valign=top ><em>y</em> = + 0.0 </td><td valign=top ><em>x</em> = + 0.0 </td><td valign=top > + 0.0</td></tr>
|
|
<tr><td valign=top >∗</td><td valign=top ><em>y</em> = −0.0 </td><td valign=top ><em>x</em> = + 0.0</td><td valign=top >−0.0</td></tr>
|
|
<tr><td valign=top >∗</td><td valign=top ><em>y</em> = + 0.0 </td><td valign=top ><em>x</em> = −0.0 </td><td valign=top ><img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]"></td></tr>
|
|
<tr><td valign=top >∗</td><td valign=top ><em>y</em> = −0.0 </td><td valign=top ><em>x</em> = −0.0 </td><td valign=top >−<img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]"></td></tr>
|
|
<tr><td valign=top >∗</td><td valign=top ><em>y</em> = + 0.0 </td><td valign=top ><em>x</em> = 0 </td><td valign=top >(<img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]">/2)</td></tr>
|
|
<tr><td valign=top >∗</td><td valign=top ><em>y</em> = −0.0 </td><td valign=top ><em>x</em> = 0 </td><td valign=top >−(<img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]">/2)
|
|
</td></tr></table>
|
|
</td></tr></table></div>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.7.4"></a>
|
|
<h3 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.7.4">11.7.4 Numerical operations</a></h3>
|
|
<p></p>
|
|
<a name="node_sec_11.7.4.1"></a>
|
|
<h4 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.7.4.1">11.7.4.1 Numerical type predicates</a></h4>
|
|
<p></p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_440"></a>number?<i> obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_442"></a>complex?<i> obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_444"></a>real?<i> obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_446"></a>rational?<i> obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_448"></a>integer?<i> obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
These numerical type predicates can be applied to any kind of
|
|
argument. They return <tt>#t</tt> if the object is a number object
|
|
of the named type, and <tt>#f</tt> otherwise.
|
|
In general, if a type predicate is true of a number object then all higher
|
|
type predicates are also true of that number object. Consequently, if a type
|
|
predicate is false of a number object, then all lower type predicates are
|
|
also false of that number object.</p>
|
|
<p>
|
|
If <i>z</i> is a complex number object, then <tt>(real? <i>z</i>)</tt> is true if
|
|
and only if <tt>(zero? (imag-part <i>z</i>))</tt> and <tt>(exact?
|
|
(imag-part <i>z</i>))</tt> are both true.</p>
|
|
<p>
|
|
If <i>x</i> is a real number object, then <tt>(rational? <i>x</i>)</tt> is true if
|
|
and only if there exist exact integer objects <i>k<sub>1</sub></i> and <i>k<sub>2</sub></i> such that
|
|
<tt>(= <i>x</i> (/ <i>k<sub>1</sub></i> <i>k<sub>2</sub></i>))</tt> and <tt>(= (numerator
|
|
<i>x</i>) <i>k<sub>1</sub></i>)</tt> and <tt>(= (denominator <i>x</i>) <i>k<sub>2</sub></i>)</tt> are
|
|
all true. Thus infinities and NaNs are not rational number objects.</p>
|
|
<p>
|
|
If <i>q</i> is a rational number objects, then <tt>(integer?
|
|
<i>q</i>)</tt> is true if and only if <tt>(= (denominator
|
|
<i>q</i>) 1)</tt> is true. If <i>q</i> is not a rational number object,
|
|
then <tt>(integer? <i>q</i>)</tt> is <tt>#f</tt>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(complex? 3+4i) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt>
|
|
<p class=nopadding>(complex? 3) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(real? 3) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(real? -2.5+0.0i) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(real? -2.5+0i) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(real? -2.5) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(real? <tt>#</tt>e1e10) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(rational? 6/10) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(rational? 6/3) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(rational? 2) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(integer? 3+0i) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(integer? 3.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(integer? 8/4) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(number? +nan.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(complex? +nan.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(real? +nan.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(rational? +nan.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(complex? +inf.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(real? -inf.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(rational? -inf.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(integer? -inf.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
Except for <tt>number?</tt>, the behavior of these type predicates
|
|
on inexact number objects is
|
|
unreliable, because any inaccuracy may
|
|
affect the result.
|
|
</blockquote>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_450"></a>real-valued?<i> obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_452"></a>rational-valued?<i> obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_454"></a>integer-valued?<i> obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
These numerical type predicates can be applied to any kind of
|
|
argument. The <tt>real-valued?</tt> procedure
|
|
returns <tt>#t</tt> if the object is a number object and is equal in the
|
|
sense of <tt>=</tt> to some real number object, or if the object is a NaN, or a
|
|
complex number object whose real part is a NaN and whose imaginary
|
|
part is zero
|
|
in the sense of <tt>zero?</tt>. The <tt>rational-valued?</tt> and <tt>integer-valued?</tt> procedures return <tt>#t</tt> if the object is a
|
|
number object and is equal in the sense of <tt>=</tt> to some object of the
|
|
named type, and otherwise they return <tt>#f</tt>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(real-valued? +nan.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt>
|
|
<p class=nopadding>(real-valued? +nan.0+0i) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(real-valued? -inf.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(real-valued? 3) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(real-valued? -2.5+0.0i) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(real-valued? -2.5+0i) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(real-valued? -2.5) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(real-valued? <tt>#</tt>e1e10) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(rational-valued? +nan.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(rational-valued? -inf.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(rational-valued? 6/10) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(rational-valued? 6/10+0.0i) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(rational-valued? 6/10+0i) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(rational-valued? 6/3) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(integer-valued? 3+0i) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(integer-valued? 3+0.0i) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(integer-valued? 3.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(integer-valued? 3.0+0.0i) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(integer-valued? 8/4) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
These procedures test whether a given number object can be coerced
|
|
to the specified type without loss of numerical accuracy.
|
|
Specifically, the behavior of these predicates differs from the
|
|
behavior of <tt>real?</tt>, <tt>rational?</tt>, and <tt>integer?</tt> on
|
|
complex number objects whose imaginary part is inexact zero.
|
|
</blockquote><p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
The behavior of these type predicates on inexact number objects is
|
|
unreliable, because any inaccuracy may
|
|
affect the result.
|
|
</blockquote>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_456"></a>exact?<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_458"></a>inexact?<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
These numerical predicates provide tests for the exactness of a
|
|
quantity. For any number object, precisely one of these predicates is
|
|
true.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(exact? 5) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt>
|
|
<p class=nopadding>(inexact? +inf.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.7.4.2"></a>
|
|
<h4 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.7.4.2">11.7.4.2 Generic conversions</a></h4>
|
|
<p></p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_460"></a>inexact<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_462"></a>exact<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
The <tt>inexact</tt> procedure returns an inexact representation of <i>z</i>. If
|
|
inexact number objects of the appropriate type have bounded precision, then
|
|
the value returned is an inexact number object that is nearest to the
|
|
argument. If an exact argument has no reasonably close inexact
|
|
equivalent, an exception with condition type
|
|
<tt>&implementation-violation</tt> may be
|
|
raised.</p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
For a real number object whose magnitude is finite but so large that it has
|
|
no reasonable finite approximation as an inexact number, a
|
|
reasonably close inexact equivalent may be <tt>+inf.0</tt> or <tt>-inf.0</tt>. Similarly, the inexact representation of a complex
|
|
number object whose components are finite may have infinite components.
|
|
</blockquote><p>
|
|
The <tt>exact</tt> procedure returns an exact representation of <i>z</i>. The value
|
|
returned is the exact number object that is numerically closest to the
|
|
argument; in most cases, the result of this procedure should be
|
|
numerically equal to its argument. If an inexact argument has no
|
|
reasonably close exact equivalent, an exception with condition type
|
|
<tt>&implementation-violation</tt> may be
|
|
raised.</p>
|
|
<p>
|
|
These procedures implement the natural one-to-one correspondence
|
|
between exact and inexact integer objects throughout an
|
|
implementation-dependent range.</p>
|
|
<p>
|
|
The <tt>inexact</tt> and <tt>exact</tt> procedures are idempotent.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.7.4.3"></a>
|
|
<h4 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.7.4.3">11.7.4.3 Arithmetic operations</a></h4>
|
|
<p></p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_464"></a>=<i> <i>z<sub>1</sub></i> <i>z<sub>2</sub></i> <i>z<sub>3</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_466"></a><<i> <i>x<sub>1</sub></i> <i>x<sub>2</sub></i> <i>x<sub>3</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_468"></a>><i> <i>x<sub>1</sub></i> <i>x<sub>2</sub></i> <i>x<sub>3</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_470"></a><=<i> <i>x<sub>1</sub></i> <i>x<sub>2</sub></i> <i>x<sub>3</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_472"></a>>=<i> <i>x<sub>1</sub></i> <i>x<sub>2</sub></i> <i>x<sub>3</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
These procedures return <tt>#t</tt> if their arguments are
|
|
(respectively): equal, monotonically increasing, monotonically
|
|
decreasing, monotonically nondecreasing, or monotonically
|
|
nonincreasing, and <tt>#f</tt> otherwise.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(= +inf.0 +inf.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt>
|
|
<p class=nopadding>(= -inf.0 +inf.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(= -inf.0 -inf.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
<p></tt></p>
|
|
<p>
|
|
For any real number object <i>x</i> that is neither infinite nor NaN:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(< -inf.0 <i>x</i> +inf.0)) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt>
|
|
<p class=nopadding>(> +inf.0 <i>x</i> -inf.0)) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
<p></tt></p>
|
|
<p>
|
|
For any number object <i>z</i>:
|
|
</p>
|
|
|
|
<tt>(= +nan.0 <i>z</i>) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt><p></tt>
|
|
For any real number object <i>x</i>:
|
|
</p>
|
|
|
|
<tt>(< +nan.0 <i>x</i>) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt>
|
|
<p class=nopadding>(> +nan.0 <i>x</i>) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
<p></tt></p>
|
|
<p>
|
|
These predicates must be transitive.</p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
The traditional implementations of these predicates in Lisp-like
|
|
languages are not transitive.
|
|
</blockquote><p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
While it is possible to compare inexact number objects using these
|
|
predicates, the results may be unreliable because a small inaccuracy
|
|
may affect the result; this is especially true of <tt>=</tt> and <tt>zero?</tt> (below).<p>
|
|
When in doubt, consult a numerical analyst.
|
|
</p>
|
|
</blockquote>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_474"></a>zero?<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_476"></a>positive?<i> x</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_478"></a>negative?<i> x</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_480"></a>odd?<i> n</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_482"></a>even?<i> n</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_484"></a>finite?<i> x</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_486"></a>infinite?<i> x</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_488"></a>nan?<i> x</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
These numerical predicates test a number object for a particular property,
|
|
returning <tt>#t</tt> or <tt>#f</tt>. The <tt>zero?</tt>
|
|
procedure
|
|
tests if the number object is <tt>=</tt> to zero, <tt>positive?</tt> tests whether it is
|
|
greater than zero, <tt>negative?</tt> tests whether it is less than zero, <tt>odd?</tt> tests whether it is odd, <tt>even?</tt> tests whether it is even, <tt>finite?</tt> tests whether it is not an infinity and not a NaN, <tt>infinite?</tt> tests whether it is an infinity, <tt>nan?</tt> tests whether it is a
|
|
NaN.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(zero? +0.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt>
|
|
<p class=nopadding>(zero? -0.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(zero? +nan.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(positive? +inf.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(negative? -inf.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(positive? +nan.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(negative? +nan.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(finite? +inf.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(finite? 5) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(finite? 5.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(infinite? 5.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(infinite? +inf.0) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
As with the predicates above, the results may be unreliable because
|
|
a small inaccuracy may affect the result.
|
|
</blockquote>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_490"></a>max<i> <i>x<sub>1</sub></i> <i>x<sub>2</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_492"></a>min<i> <i>x<sub>1</sub></i> <i>x<sub>2</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
These procedures return the maximum or minimum of their arguments.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(max 3 4) <span style="margin-left: 2em">‌</span>⇒ 4
|
|
<p class=nopadding>(max 3.9 4) <span style="margin-left: 2em">‌</span>⇒ 4.0</p>
|
|
<p></tt></p>
|
|
<p>
|
|
For any real number object <i>x</i>:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(max +inf.0 <i>x</i>) <span style="margin-left: 2em">‌</span>⇒ +inf.0
|
|
<p class=nopadding>(min -inf.0 <i>x</i>) <span style="margin-left: 2em">‌</span>⇒ -inf.0</p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
If any argument is inexact, then the result is also inexact (unless
|
|
the procedure can prove that the inaccuracy is not large enough to affect the
|
|
result, which is possible only in unusual implementations). If <tt>min</tt> or
|
|
<tt>max</tt> is used to compare number objects of mixed exactness, and the numerical
|
|
value of the result cannot be represented as an inexact number object without loss of
|
|
accuracy, then the procedure may raise an exception with condition
|
|
type <tt>&implementation-restriction</tt>.
|
|
</blockquote><p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_494"></a>+<i> <i>z<sub>1</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_496"></a>*<i> <i>z<sub>1</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
These procedures return the sum or product of their arguments.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(+ 3 4) <span style="margin-left: 2em">‌</span>⇒ 7
|
|
<p class=nopadding>(+ 3) <span style="margin-left: 2em">‌</span>⇒ 3</p>
|
|
|
|
<p class=nopadding>(+) <span style="margin-left: 2em">‌</span>⇒ 0</p>
|
|
|
|
<p class=nopadding>(+ +inf.0 +inf.0) <span style="margin-left: 2em">‌</span>⇒ +inf.0</p>
|
|
|
|
<p class=nopadding>(+ +inf.0 -inf.0) <span style="margin-left: 2em">‌</span>⇒ +nan.0</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(* 4) <span style="margin-left: 2em">‌</span>⇒ 4</p>
|
|
|
|
<p class=nopadding>(*) <span style="margin-left: 2em">‌</span>⇒ 1</p>
|
|
|
|
<p class=nopadding>(* 5 +inf.0) <span style="margin-left: 2em">‌</span>⇒ +inf.0</p>
|
|
|
|
<p class=nopadding>(* -5 +inf.0) <span style="margin-left: 2em">‌</span>⇒ -inf.0</p>
|
|
|
|
<p class=nopadding>(* +inf.0 +inf.0) <span style="margin-left: 2em">‌</span>⇒ +inf.0</p>
|
|
|
|
<p class=nopadding>(* +inf.0 -inf.0) <span style="margin-left: 2em">‌</span>⇒ -inf.0</p>
|
|
|
|
<p class=nopadding>(* 0 +inf.0) <span style="margin-left: 2em">‌</span>⇒ 0 <i>or</i> +nan.0</p>
|
|
|
|
<p class=nopadding>(* 0 +nan.0) <span style="margin-left: 2em">‌</span>⇒ 0 <i>or</i> +nan.0</p>
|
|
|
|
<p class=nopadding>(* 1.0 0) <span style="margin-left: 2em">‌</span>⇒ 0 <i>or</i> 0.0</p>
|
|
<p></tt></p>
|
|
<p>
|
|
For any real number object <i>x</i> that is neither infinite nor NaN:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(+ +inf.0 <i>x</i>) <span style="margin-left: 2em">‌</span>⇒ +inf.0
|
|
<p class=nopadding>(+ -inf.0 <i>x</i>) <span style="margin-left: 2em">‌</span>⇒ -inf.0</p>
|
|
<p></tt></p>
|
|
<p>
|
|
For any real number object <i>x</i>:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(+ +nan.0 <i>x</i>) <span style="margin-left: 2em">‌</span>⇒ +nan.0<p></tt></p>
|
|
<p>
|
|
For any real number object <i>x</i> that is not an exact 0:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(* +nan.0 <i>x</i>) <span style="margin-left: 2em">‌</span>⇒ +nan.0<p></tt></p>
|
|
<p>
|
|
If any of these procedures are applied to mixed non-rational real and
|
|
non-real complex arguments, they either raise an exception with
|
|
condition type <tt>&implementation-restriction</tt> or return an
|
|
unspecified number object.</p>
|
|
<p>
|
|
Implementations that distinguish −0.0 should adopt behavior
|
|
consistent with the following examples:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(+ 0.0 -0.0) <span style="margin-left: 2em">‌</span>⇒ 0.0
|
|
<p class=nopadding>(+ -0.0 0.0) <span style="margin-left: 2em">‌</span>⇒ 0.0</p>
|
|
|
|
<p class=nopadding>(+ 0.0 0.0) <span style="margin-left: 2em">‌</span>⇒ 0.0</p>
|
|
|
|
<p class=nopadding>(+ -0.0 -0.0) <span style="margin-left: 2em">‌</span>⇒ -0.0</p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_498"></a>-<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_500"></a>-<i> <i>z<sub>1</sub></i> <i>z<sub>2</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
With two or more arguments, this procedures returns the difference of
|
|
its arguments, associating to the left. With one argument, however,
|
|
it returns the additive inverse of its argument.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(- 3 4) <span style="margin-left: 2em">‌</span>⇒ -1
|
|
<p class=nopadding>(- 3 4 5) <span style="margin-left: 2em">‌</span>⇒ -6</p>
|
|
|
|
<p class=nopadding>(- 3) <span style="margin-left: 2em">‌</span>⇒ -3</p>
|
|
|
|
<p class=nopadding>(- +inf.0 +inf.0) <span style="margin-left: 2em">‌</span>⇒ +nan.0</p>
|
|
<p></tt></p>
|
|
<p>
|
|
If this procedure is applied to mixed non-rational real and
|
|
non-real complex arguments, it either raises an exception with
|
|
condition type <tt>&implementation-restriction</tt> or returns an
|
|
unspecified number object.</p>
|
|
<p>
|
|
Implementations that distinguish −0.0 should adopt behavior
|
|
consistent with the following examples:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(- 0.0) <span style="margin-left: 2em">‌</span>⇒ -0.0
|
|
<p class=nopadding>(- -0.0) <span style="margin-left: 2em">‌</span>⇒ 0.0</p>
|
|
|
|
<p class=nopadding>(- 0.0 -0.0) <span style="margin-left: 2em">‌</span>⇒ 0.0</p>
|
|
|
|
<p class=nopadding>(- -0.0 0.0) <span style="margin-left: 2em">‌</span>⇒ -0.0</p>
|
|
|
|
<p class=nopadding>(- 0.0 0.0) <span style="margin-left: 2em">‌</span>⇒ 0.0</p>
|
|
|
|
<p class=nopadding>(- -0.0 -0.0) <span style="margin-left: 2em">‌</span>⇒ 0.0</p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_502"></a>/<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_504"></a>/<i> <i>z<sub>1</sub></i> <i>z<sub>2</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
If all of the arguments are exact, then the divisors must all
|
|
be nonzero.
|
|
With two or more arguments, this procedure returns the
|
|
quotient of its arguments, associating to the left. With one
|
|
argument, however, it returns the multiplicative inverse
|
|
of its argument.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(/ 3 4 5) <span style="margin-left: 2em">‌</span>⇒ 3/20
|
|
<p class=nopadding>(/ 3) <span style="margin-left: 2em">‌</span>⇒ 1/3</p>
|
|
|
|
<p class=nopadding>(/ 0.0) <span style="margin-left: 2em">‌</span>⇒ +inf.0</p>
|
|
|
|
<p class=nopadding>(/ 1.0 0) <span style="margin-left: 2em">‌</span>⇒ +inf.0</p>
|
|
|
|
<p class=nopadding>(/ -1 0.0) <span style="margin-left: 2em">‌</span>⇒ -inf.0</p>
|
|
|
|
<p class=nopadding>(/ +inf.0) <span style="margin-left: 2em">‌</span>⇒ 0.0</p>
|
|
|
|
<p class=nopadding>(/ 0 0) <tt> &assertion</tt> <i>exception</i></p>
|
|
|
|
<p class=nopadding>(/ 3 0) <tt> &assertion</tt> <i>exception</i></p>
|
|
|
|
<p class=nopadding>(/ 0 3.5) <span style="margin-left: 2em">‌</span>⇒ 0.0</p>
|
|
|
|
<p class=nopadding>(/ 0 0.0) <span style="margin-left: 2em">‌</span>⇒ +nan.0</p>
|
|
|
|
<p class=nopadding>(/ 0.0 0) <span style="margin-left: 2em">‌</span>⇒ +nan.0</p>
|
|
|
|
<p class=nopadding>(/ 0.0 0.0) <span style="margin-left: 2em">‌</span>⇒ +nan.0</p>
|
|
<p></tt></p>
|
|
<p>
|
|
If this procedure is applied to mixed non-rational real and
|
|
non-real complex arguments, it either raises an exception with
|
|
condition type <tt>&implementation-restriction</tt> or returns an
|
|
unspecified number object.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_506"></a>abs<i> x</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns the absolute value of its argument.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(abs -7) <span style="margin-left: 2em">‌</span>⇒ 7
|
|
<p class=nopadding>(abs -inf.0) <span style="margin-left: 2em">‌</span>⇒ +inf.0</p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_508"></a>div-and-mod<i> <i>x<sub>1</sub></i> <i>x<sub>2</sub></i></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_510"></a>div<i> <i>x<sub>1</sub></i> <i>x<sub>2</sub></i></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_512"></a>mod<i> <i>x<sub>1</sub></i> <i>x<sub>2</sub></i></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_514"></a>div0-and-mod0<i> <i>x<sub>1</sub></i> <i>x<sub>2</sub></i></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_516"></a>div0<i> <i>x<sub>1</sub></i> <i>x<sub>2</sub></i></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_518"></a>mod0<i> <i>x<sub>1</sub></i> <i>x<sub>2</sub></i></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
These procedures implement number-theoretic integer division and
|
|
return the results of the corresponding mathematical operations
|
|
specified in section <a href="r6rs-Z-H-14.html#node_sec_11.7.3.1">11.7.3.1</a>. In each case, <i>x<sub>1</sub></i>
|
|
must be neither infinite nor a NaN, and <i>x<sub>2</sub></i> must be nonzero;
|
|
otherwise, an exception with condition type <tt>&assertion</tt> is raised.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(div <i>x<sub>1</sub></i> <i>x<sub>2</sub></i>) <span style="margin-left: 2em">‌</span>⇒ <i>x<sub>1</sub></i> <em>d</em><em>i</em><em>v</em> <i>x<sub>2</sub></i>
|
|
<p class=nopadding>(mod <i>x<sub>1</sub></i> <i>x<sub>2</sub></i>) <span style="margin-left: 2em">‌</span>⇒ <i>x<sub>1</sub></i> <em>m</em><em>o</em><em>d</em> <i>x<sub>2</sub></i></p>
|
|
|
|
<p class=nopadding>(div-and-mod <i>x<sub>1</sub></i> <i>x<sub>2</sub></i>) <span style="margin-left: 2em">‌</span>⇒ <i>x<sub>1</sub></i> <em>d</em><em>i</em><em>v</em> <i>x<sub>2</sub></i>, <i>x<sub>1</sub></i> <em>m</em><em>o</em><em>d</em> <i>x<sub>2</sub></i><br>
|
|
; two return values</p>
|
|
|
|
<p class=nopadding>(div0 <i>x<sub>1</sub></i> <i>x<sub>2</sub></i>) <span style="margin-left: 2em">‌</span>⇒ <i>x<sub>1</sub></i> <em>d</em><em>i</em><em>v</em><sub>0</sub> <i>x<sub>2</sub></i></p>
|
|
|
|
<p class=nopadding>(mod0 <i>x<sub>1</sub></i> <i>x<sub>2</sub></i>) <span style="margin-left: 2em">‌</span>⇒ <i>x<sub>1</sub></i> <em>m</em><em>o</em><em>d</em><sub>0</sub> <i>x<sub>2</sub></i></p>
|
|
|
|
<p class=nopadding>(div0-and-mod0 <i>x<sub>1</sub></i> <i>x<sub>2</sub></i>) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ <i>x<sub>1</sub></i> <em>d</em><em>i</em><em>v</em><sub>0</sub> <i>x<sub>2</sub></i>, <i>x<sub>1</sub></i> <em>m</em><em>o</em><em>d</em><sub>0</sub> <i>x<sub>2</sub></i><br>
|
|
; two return values</p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_520"></a>gcd<i> <i>n<sub>1</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_522"></a>lcm<i> <i>n<sub>1</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
These procedures return the greatest common divisor or least common
|
|
multiple of their arguments. The result is always non-negative.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(gcd 32 -36) <span style="margin-left: 2em">‌</span>⇒ 4
|
|
<p class=nopadding>(gcd) <span style="margin-left: 2em">‌</span>⇒ 0</p>
|
|
|
|
<p class=nopadding>(lcm 32 -36) <span style="margin-left: 2em">‌</span>⇒ 288</p>
|
|
|
|
<p class=nopadding>(lcm 32.0 -36) <span style="margin-left: 2em">‌</span>⇒ 288.0</p>
|
|
|
|
<p class=nopadding>(lcm) <span style="margin-left: 2em">‌</span>⇒ 1</p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_524"></a>numerator<i> q</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_526"></a>denominator<i> q</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
These procedures return the numerator or denominator of their
|
|
argument; the result is computed as if the argument was represented as
|
|
a fraction in lowest terms. The denominator is always positive. The
|
|
denominator of 0 is defined to be 1.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(numerator (/ 6 4)) <span style="margin-left: 2em">‌</span>⇒ 3
|
|
<p class=nopadding>(denominator (/ 6 4)) <span style="margin-left: 2em">‌</span>⇒ 2</p>
|
|
|
|
<p class=nopadding>(denominator</p>
|
|
|
|
<p class=nopadding> (inexact (/ 6 4))) <span style="margin-left: 2em">‌</span>⇒ 2.0</p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_528"></a>floor<i> x</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_530"></a>ceiling<i> x</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_532"></a>truncate<i> x</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_534"></a>round<i> x</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
These procedures return inexact integer objects for inexact arguments that are
|
|
not infinities or NaNs, and exact integer objects for exact rational
|
|
arguments. For such arguments, <tt>floor</tt> returns the largest
|
|
integer object not larger than <i>x</i>. The <tt>ceiling</tt> procedure returns the smallest
|
|
integer object not smaller than <i>x</i>. The <tt>truncate</tt> procedure returns the integer
|
|
object closest to <i>x</i> whose absolute value is not larger than the
|
|
absolute value of <i>x</i>. The <tt>round</tt> procedure returns the
|
|
closest integer object to
|
|
<i>x</i>, rounding to even when <i>x</i> represents a number halfway between two
|
|
integers.</p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
If the argument to one of these procedures is inexact, then the result
|
|
is also inexact. If an exact value is needed, the
|
|
result should be passed to the <tt>exact</tt> procedure.
|
|
</blockquote><p>
|
|
Although infinities and NaNs are not integer objects, these procedures return
|
|
an infinity when given an infinity as an argument, and a NaN when
|
|
given a NaN.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(floor -4.3) <span style="margin-left: 2em">‌</span>⇒ -5.0
|
|
<p class=nopadding>(ceiling -4.3) <span style="margin-left: 2em">‌</span>⇒ -4.0</p>
|
|
|
|
<p class=nopadding>(truncate -4.3) <span style="margin-left: 2em">‌</span>⇒ -4.0</p>
|
|
|
|
<p class=nopadding>(round -4.3) <span style="margin-left: 2em">‌</span>⇒ -4.0</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(floor 3.5) <span style="margin-left: 2em">‌</span>⇒ 3.0</p>
|
|
|
|
<p class=nopadding>(ceiling 3.5) <span style="margin-left: 2em">‌</span>⇒ 4.0</p>
|
|
|
|
<p class=nopadding>(truncate 3.5) <span style="margin-left: 2em">‌</span>⇒ 3.0</p>
|
|
|
|
<p class=nopadding>(round 3.5) <span style="margin-left: 2em">‌</span>⇒ 4.0</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(round 7/2) <span style="margin-left: 2em">‌</span>⇒ 4</p>
|
|
|
|
<p class=nopadding>(round 7) <span style="margin-left: 2em">‌</span>⇒ 7</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(floor +inf.0) <span style="margin-left: 2em">‌</span>⇒ +inf.0</p>
|
|
|
|
<p class=nopadding>(ceiling -inf.0) <span style="margin-left: 2em">‌</span>⇒ -inf.0</p>
|
|
|
|
<p class=nopadding>(round +nan.0) <span style="margin-left: 2em">‌</span>⇒ +nan.0</p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_536"></a>rationalize<i> <i>x<sub>1</sub></i> <i>x<sub>2</sub></i></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
The <tt>rationalize</tt> procedure returns the a number object
|
|
representing the <em>simplest</em> rational
|
|
number differing from <i>x<sub>1</sub></i> by no more than <i>x<sub>2</sub></i>. A rational number <em>r</em><sub>1</sub> is
|
|
<em>simpler</em> <a name="node_idx_538"></a>than another rational number
|
|
<em>r</em><sub>2</sub> if <em>r</em><sub>1</sub> = <em>p</em><sub>1</sub>/<em>q</em><sub>1</sub> and <em>r</em><sub>2</sub> = <em>p</em><sub>2</sub>/<em>q</em><sub>2</sub> (in lowest terms) and |<em>p</em><sub>1</sub>|
|
|
≤ |<em>p</em><sub>2</sub>| and |<em>q</em><sub>1</sub>| ≤ |<em>q</em><sub>2</sub>|. Thus 3/5 is simpler than 4/7.
|
|
Although not all rationals are comparable in this ordering (consider 2/7
|
|
and 3/5) any interval contains a rational number that is simpler than
|
|
every other rational number in that interval (the simpler 2/5 lies
|
|
between 2/7 and 3/5). Note that 0 = 0/1 is the simplest rational of
|
|
all.
|
|
</p>
|
|
|
|
<tt>(rationalize (exact .3) 1/10) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ 1/3
|
|
<p class=nopadding>(rationalize .3 1/10) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ <tt>#</tt>i1/3 ; approximately</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(rationalize +inf.0 3) <span style="margin-left: 2em">‌</span>⇒ +inf.0</p>
|
|
|
|
<p class=nopadding>(rationalize +inf.0 +inf.0) <span style="margin-left: 2em">‌</span>⇒ +nan.0</p>
|
|
|
|
<p class=nopadding>(rationalize 3 +inf.0) <span style="margin-left: 2em">‌</span>⇒ 0.0</p>
|
|
<p></tt>
|
|
The first two examples hold only in implementations whose inexact real
|
|
number objects have sufficient precision.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_540"></a>exp<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_542"></a>log<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_544"></a>log<i> <i>z<sub>1</sub></i> <i>z<sub>2</sub></i></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_546"></a>sin<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_548"></a>cos<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_550"></a>tan<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_552"></a>asin<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_554"></a>acos<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_556"></a>atan<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_558"></a>atan<i> <i>x<sub>1</sub></i> <i>x<sub>2</sub></i></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
These procedures compute the usual transcendental functions. The <tt>exp</tt> procedure computes the base-<em>e</em> exponential of <i>z</i>.
|
|
The <tt>log</tt> procedure with a single argument computes the natural logarithm of
|
|
<i>z</i> (not the base-ten logarithm); <tt>(log <i>z<sub>1</sub></i>
|
|
<i>z<sub>2</sub></i>)</tt> computes the base-<i>z<sub>2</sub></i> logarithm of <i>z<sub>1</sub></i>.
|
|
The <tt>asin</tt>, <tt>acos</tt>, and <tt>atan</tt> procedures compute arcsine,
|
|
arccosine, and arctangent, respectively. The two-argument variant of
|
|
<tt>atan</tt> computes <tt>(angle (make-rectangular <i>x<sub>2</sub></i>
|
|
<i>x<sub>1</sub></i>))</tt>.</p>
|
|
<p>
|
|
See section <a href="r6rs-Z-H-14.html#node_sec_11.7.3.2">11.7.3.2</a> for the underlying
|
|
mathematical operations. These procedures may return inexact results
|
|
even when given exact arguments.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(exp +inf.0) <span style="margin-left: 2em">‌</span>⇒ +inf.0
|
|
<p class=nopadding>(exp -inf.0) <span style="margin-left: 2em">‌</span>⇒ 0.0</p>
|
|
|
|
<p class=nopadding>(log +inf.0) <span style="margin-left: 2em">‌</span>⇒ +inf.0</p>
|
|
|
|
<p class=nopadding>(log 0.0) <span style="margin-left: 2em">‌</span>⇒ -inf.0</p>
|
|
|
|
<p class=nopadding>(log 0) <tt> &assertion</tt> <i>exception</i></p>
|
|
|
|
<p class=nopadding>(log -inf.0) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ +inf.0+3.141592653589793i<br>
|
|
; approximately</p>
|
|
|
|
<p class=nopadding>(atan -inf.0) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ -1.5707963267948965 ; approximately</p>
|
|
|
|
<p class=nopadding>(atan +inf.0) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ 1.5707963267948965 ; approximately</p>
|
|
|
|
<p class=nopadding>(log -1.0+0.0i) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ 0.0+3.141592653589793i ; approximately</p>
|
|
|
|
<p class=nopadding>(log -1.0-0.0i) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ 0.0-3.141592653589793i ; approximately<br>
|
|
; if -0.0 is distinguished</p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_560"></a>sqrt<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns the principal square root of <i>z</i>. For rational <i>z</i>,
|
|
the result has either positive real part, or zero real part and
|
|
non-negative imaginary part. With log defined as in
|
|
section <a href="r6rs-Z-H-14.html#node_sec_11.7.3.2">11.7.3.2</a>, the value of <tt>(sqrt
|
|
<i>z</i>)</tt> could be expressed as <em>e</em><sup>(log <em>z</em>/2)</sup>.</p>
|
|
<p>
|
|
The <tt>sqrt</tt> procedure may return an inexact result even when given an exact
|
|
argument.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(sqrt -5) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ 0.0+2.23606797749979i ; approximately
|
|
<p class=nopadding>(sqrt +inf.0) <span style="margin-left: 2em">‌</span>⇒ +inf.0</p>
|
|
|
|
<p class=nopadding>(sqrt -inf.0) <span style="margin-left: 2em">‌</span>⇒ +inf.0i</p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_562"></a>exact-integer-sqrt<i> k</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
The <tt>exact-integer-sqrt</tt> procedure returns two non-negative exact
|
|
integer objects <em>s</em> and <em>r</em> where <i>k</i> = <em>s</em><sup>2</sup> +
|
|
<em>r</em> and <i>k</i> < (<em>s</em> + 1)<sup>2</sup>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(exact-integer-sqrt 4) <span style="margin-left: 2em">‌</span>⇒ 2 0<br>
|
|
; two return values
|
|
<p class=nopadding>(exact-integer-sqrt 5) <span style="margin-left: 2em">‌</span>⇒ 2 1<br>
|
|
; two return values</p>
|
|
|
|
<p class=nopadding></p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_564"></a>expt<i> <i>z<sub>1</sub></i> <i>z<sub>2</sub></i></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns <i>z<sub>1</sub></i> raised to the power <i>z<sub>2</sub></i>. For nonzero <i>z<sub>1</sub></i>,
|
|
this is <em>e</em><sup><em>z</em><sub>2</sub> log <em>z</em><sub>1</sub></sup>.
|
|
0.0<sup><em>z</em></sup> is 1.0 if <i>z</i> = 0.0, and 0.0 if <tt>(real-part <i>z</i>)</tt> is positive. For other cases in which
|
|
the first argument is zero, either an exception is raised with
|
|
condition type <tt>&implementation-restriction</tt>, or an unspecified
|
|
number object is returned.</p>
|
|
<p>
|
|
For an exact real number object <i>z<sub>1</sub></i> and an exact
|
|
integer object <i>z<sub>2</sub></i>, <tt>(expt <i>z<sub>1</sub></i>
|
|
<i>z<sub>2</sub></i>)</tt> must return an exact result. For all other
|
|
values of <i>z<sub>1</sub></i> and <i>z<sub>2</sub></i>, <tt>(expt <i>z<sub>1</sub></i>
|
|
<i>z<sub>2</sub></i>)</tt> may return an inexact result, even when both
|
|
<i>z<sub>1</sub></i> and <i>z<sub>2</sub></i> are exact.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(expt 5 3) <span style="margin-left: 2em">‌</span>⇒ 125
|
|
<p class=nopadding>(expt 5 -3) <span style="margin-left: 2em">‌</span>⇒ 1/125</p>
|
|
|
|
<p class=nopadding>(expt 5 0) <span style="margin-left: 2em">‌</span>⇒ 1</p>
|
|
|
|
<p class=nopadding>(expt 0 5) <span style="margin-left: 2em">‌</span>⇒ 0</p>
|
|
|
|
<p class=nopadding>(expt 0 5+.0000312i) <span style="margin-left: 2em">‌</span>⇒ 0</p>
|
|
|
|
<p class=nopadding>(expt 0 -5) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding>(expt 0 -5+.0000312i) <span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding>(expt 0 0) <span style="margin-left: 2em">‌</span>⇒ 1</p>
|
|
|
|
<p class=nopadding>(expt 0.0 0.0) <span style="margin-left: 2em">‌</span>⇒ 1.0</p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_566"></a>make-rectangular<i> <i>x<sub>1</sub></i> <i>x<sub>2</sub></i></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_568"></a>make-polar<i> <i>x<sub>3</sub></i> <i>x<sub>4</sub></i></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_570"></a>real-part<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_572"></a>imag-part<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_574"></a>magnitude<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_576"></a>angle<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Suppose <em>a</em><sub>1</sub>, <em>a</em><sub>2</sub>, <em>a</em><sub>3</sub>, and <em>a</em><sub>4</sub> are real
|
|
numbers, and <em>c</em> is a complex number such that the
|
|
following holds:
|
|
</p>
|
|
<div align=left><img src="r6rs-Z-G-5.gif" border="0" alt="[r6rs-Z-G-5.gif]"></div><p></p>
|
|
<p>
|
|
Then, if <i>x<sub>1</sub></i>, <i>x<sub>2</sub></i>, <i>x<sub>3</sub></i>, and <i>x<sub>4</sub></i> are number
|
|
objects representing <em>a</em><sub>1</sub>, <em>a</em><sub>2</sub>, <em>a</em><sub>3</sub>, and <em>a</em><sub>4</sub>, respectively,
|
|
<tt>(make-rectangular <i>x<sub>1</sub></i> <i>x<sub>2</sub></i>)</tt> returns <em>c</em>, and <tt>(make-polar <i>x<sub>3</sub></i> <i>x<sub>4</sub></i>)</tt> returns <em>c</em>.
|
|
</p>
|
|
|
|
<tt>(make-rectangular 1.1 2.2) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ 1.1+2.2i ; approximately
|
|
<p class=nopadding>(make-polar 1.1 2.2) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ 1.1@2.2 ; approximately</p>
|
|
|
|
<p class=nopadding></p>
|
|
<p></tt>
|
|
Conversely, if −<img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]"> ≤ <em>a</em><sub>4</sub> ≤ <img src="r6rs-Z-G-D-3.gif" border="0" alt="[r6rs-Z-G-D-3.gif]">, and if <em>z</em> is a number object
|
|
representing <em>c</em>, then <tt>(real-part <i>z</i>)</tt> returns <em>a</em><sub>1</sub> <tt>(imag-part <i>z</i>)</tt> returns <em>a</em><sub>2</sub>, <tt>(magnitude <i>z</i>)</tt>
|
|
returns <em>a</em><sub>3</sub>, and <tt>(angle <i>z</i>)</tt> returns <em>a</em><sub>4</sub>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(real-part 1.1+2.2i) <span style="margin-left: 2em">‌</span>⇒ 1.1 ; approximately
|
|
<p class=nopadding>(imag-part 1.1+2.2i) <span style="margin-left: 2em">‌</span>⇒ 2.2i ; approximately</p>
|
|
|
|
<p class=nopadding>(magnitude 1.1@2.2) <span style="margin-left: 2em">‌</span>⇒ 1.1 ; approximately</p>
|
|
|
|
<p class=nopadding>(angle 1.1@2.2) <span style="margin-left: 2em">‌</span>⇒ 2.2 ; approximately</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(angle -1.0) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ 3.141592653589793 ; approximately</p>
|
|
|
|
<p class=nopadding>(angle -1.0+0.0i) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ 3.141592653589793 ; approximately</p>
|
|
|
|
<p class=nopadding>(angle -1.0-0.0i) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ -3.141592653589793 ; approximately<br>
|
|
; if -0.0 is distinguished</p>
|
|
|
|
<p class=nopadding>(angle +inf.0) <span style="margin-left: 2em">‌</span>⇒ 0.0</p>
|
|
|
|
<p class=nopadding>(angle -inf.0) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ 3.141592653589793 ; approximately</p>
|
|
<p></tt></p>
|
|
<p>
|
|
Moreover, suppose <i>x<sub>1</sub></i>, <i>x<sub>2</sub></i> are such that either <i>x<sub>1</sub></i>
|
|
or <i>x<sub>2</sub></i> is an infinity, then
|
|
</p>
|
|
|
|
<tt>(make-rectangular <i>x<sub>1</sub></i> <i>x<sub>2</sub></i>) <span style="margin-left: 2em">‌</span>⇒ <i>z</i>
|
|
<p class=nopadding>(magnitude <i>z</i>) <span style="margin-left: 2em">‌</span>⇒ +inf.0</p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
The <tt>make-polar</tt>, <tt>magnitude</tt>, and
|
|
<tt>angle</tt> procedures may return inexact results even when given exact
|
|
arguments.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(angle -1) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ 3.141592653589793 ; approximately
|
|
<p class=nopadding></p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.7.4.4"></a>
|
|
<h4 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.7.4.4">11.7.4.4 Numerical Input and Output</a></h4>
|
|
<p></p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_578"></a>number->string<i> z</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_580"></a>number->string<i> z radix</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_582"></a>number->string<i> z radix precision</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
<i>Radix</i> must be an exact integer object, either 2, 8, 10, or 16. If
|
|
omitted, <i>radix</i> defaults to 10. If a <i>precision</i> is
|
|
specified, then <i>z</i> must be an inexact complex number object,
|
|
<i>precision</i> must be an exact positive integer object, and <i>radix</i>
|
|
must be 10. The <tt>number->string</tt> procedure takes a number object and a
|
|
radix and returns as a string an external representation of the given
|
|
number object in the given radix such that
|
|
</p>
|
|
|
|
<tt>(let ((number <i>z</i>) (radix <i>radix</i>))
|
|
<p class=nopadding> (eqv? (string->number</p>
|
|
|
|
<p class=nopadding> (number->string number radix)</p>
|
|
|
|
<p class=nopadding> radix)</p>
|
|
|
|
<p class=nopadding> number))</p>
|
|
<p></tt>
|
|
is true. If no possible result makes this expression
|
|
true, an exception with condition type
|
|
<tt>&implementation-restriction</tt> is raised.</p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
The error case can occur only when <i>z</i> is not a complex number object
|
|
or is a complex number object with a non-rational real or imaginary part.
|
|
</blockquote><p>
|
|
If a <i>precision</i> is specified, then the representations of the
|
|
inexact real components of the result, unless they are infinite or
|
|
NaN, specify an explicit <mantissa width> <i>p</i>, and <i>p</i> is the
|
|
least <i>p</i> ≥ <i>precision</i> for which the above expression is
|
|
true.</p>
|
|
<p>
|
|
If <i>z</i> is inexact, the radix is 10, and the above expression and
|
|
condition can be satisfied by a result that contains a decimal point,
|
|
then the result contains a decimal point and is expressed using the
|
|
minimum number of digits (exclusive of exponent, trailing zeroes, and
|
|
mantissa width) needed to make the above expression and condition
|
|
true [<a href="r6rs-Z-H-21.html#node_bib_4">4</a>, <a href="r6rs-Z-H-21.html#node_bib_7">7</a>]; otherwise the format of the result
|
|
is unspecified.</p>
|
|
<p>
|
|
The result returned by <tt>number->string</tt> never contains an explicit
|
|
radix prefix.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_584"></a>string->number<i> string</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_586"></a>string->number<i> string radix</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns a number object with maximally precise representation expressed by the
|
|
given <i>string</i>. <i>Radix</i> must be an exact integer object, either 2, 8, 10,
|
|
or 16. If supplied, <i>radix</i> is a default radix that may be overridden
|
|
by an explicit radix prefix in <i>string</i> (e.g., <tt>"#o177"</tt>). If <i>radix</i>
|
|
is not supplied, then the default radix is 10. If <i>string</i> is not
|
|
a syntactically valid notation for a number object or a notation for a
|
|
rational number object with a zero denominator, then <tt>string->number</tt>
|
|
returns <tt>#f</tt>.
|
|
</p>
|
|
|
|
<tt>(string->number "100") <span style="margin-left: 2em">‌</span>⇒ 100
|
|
<p class=nopadding>(string->number "100" 16) <span style="margin-left: 2em">‌</span>⇒ 256</p>
|
|
|
|
<p class=nopadding>(string->number "1e2") <span style="margin-left: 2em">‌</span>⇒ 100.0</p>
|
|
|
|
<p class=nopadding>(string->number "0/0") <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(string->number "+inf.0") <span style="margin-left: 2em">‌</span>⇒ +inf.0</p>
|
|
|
|
<p class=nopadding>(string->number "-inf.0") <span style="margin-left: 2em">‌</span>⇒ -inf.0</p>
|
|
|
|
<p class=nopadding>(string->number "+nan.0") <span style="margin-left: 2em">‌</span>⇒ +nan.0</p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
The <tt>string->number</tt> procedure always returns a number object or
|
|
<tt>#f</tt>; it never raises an exception.
|
|
</blockquote>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.8"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.8">11.8 Booleans</a></h2>
|
|
<p></p>
|
|
<p>
|
|
The standard boolean objects for true and false have external representations
|
|
<tt>#t</tt> and <tt>#f</tt>.<a name="node_idx_588"></a><a name="node_idx_590"></a>However, of all
|
|
objects, only <tt>#f</tt> counts as false in
|
|
conditional expressions. See section <a href="r6rs-Z-H-8.html#node_sec_5.7">5.7</a>.</p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
Programmers accustomed to other dialects of Lisp should be aware that
|
|
Scheme distinguishes both <tt>#f</tt> and the empty list <a name="node_idx_592"></a>from each other and from the symbol <tt>nil</tt>.
|
|
</blockquote><p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_594"></a>not<i> obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns <tt>#t</tt> if <i>obj</i> is <tt>#f</tt>, and returns
|
|
<tt>#f</tt> otherwise.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(not <tt>#t</tt>) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt>
|
|
<p class=nopadding>(not 3) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(not (list 3)) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(not <tt>#f</tt>) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(not '()) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(not (list)) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(not 'nil) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_596"></a>boolean?<i> obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns <tt>#t</tt> if <i>obj</i> is either <tt>#t</tt> or
|
|
<tt>#f</tt> and returns <tt>#f</tt> otherwise.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(boolean? <tt>#f</tt>) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt>
|
|
<p class=nopadding>(boolean? 0) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(boolean? '()) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_598"></a>boolean=?<i> <i>bool<sub>1</sub></i> <i>bool<sub>2</sub></i> <i>bool<sub>3</sub></i>
|
|
<tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns <tt>#t</tt> if the booleans are the same.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.9"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.9">11.9 Pairs and lists</a></h2>
|
|
<p></p>
|
|
<p>
|
|
A <a name="node_idx_600"></a><em>pair</em> is a
|
|
compound structure with two fields called the car and cdr fields (for
|
|
historical reasons). Pairs are created by the procedure <tt>cons</tt>.
|
|
The car and cdr fields are accessed by the procedures <tt>car</tt> and
|
|
<tt>cdr</tt>.</p>
|
|
<p>
|
|
Pairs are used primarily to represent lists. A list can
|
|
be defined recursively as either the empty list<a name="node_idx_602"></a>or a pair whose
|
|
cdr is a list. More precisely, the set of lists is defined as the smallest
|
|
set <i>X</i> such that</p>
|
|
<p>
|
|
</p>
|
|
<ul>
|
|
<li><p>The empty list is in <i>X</i>.
|
|
</p>
|
|
<li><p>If <i>list</i> is in <i>X</i>, then any pair whose cdr field contains
|
|
<i>list</i> is also in <i>X</i>.
|
|
</p>
|
|
</ul><p></p>
|
|
<p>
|
|
The objects in the car fields of successive pairs of a list are the
|
|
elements of the list. For example, a two-element list is a pair whose car
|
|
is the first element and whose cdr is a pair whose car is the second element
|
|
and whose cdr is the empty list. The length of a list is the number of
|
|
elements, which is the same as the number of pairs.</p>
|
|
<p>
|
|
The empty list<a name="node_idx_604"></a>is a special object of its own type.
|
|
It is not a pair. It has no elements and its length is zero.</p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
The above definitions imply that all lists have finite length and are
|
|
terminated by the empty list.
|
|
</blockquote><p>
|
|
A chain of pairs not ending in the empty list is called an
|
|
<a name="node_idx_606"></a><em>improper list</em>. Note that an improper list is not a list.
|
|
The list and dotted notations can be combined to represent
|
|
improper lists:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(a b c . d)<p></tt></p>
|
|
<p>
|
|
is equivalent to</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(a . (b . (c . d)))<p></tt></p>
|
|
<p>
|
|
Whether a given pair is a list depends upon what is stored in the cdr
|
|
field.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_608"></a>pair?<i> obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns <tt>#t</tt> if <i>obj</i> is a pair, and otherwise
|
|
returns <tt>#f</tt>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(pair? '(a . b)) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt>
|
|
<p class=nopadding>(pair? '(a b c)) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(pair? '()) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(pair? '#(a b)) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_610"></a>cons<i> <i>obj<sub>1</sub></i> <i>obj<sub>2</sub></i></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns a newly allocated pair whose car is <i>obj<sub>1</sub></i> and whose cdr is
|
|
<i>obj<sub>2</sub></i>. The pair is guaranteed to be different (in the sense of
|
|
<tt>eqv?</tt>) from every existing object.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(cons 'a '()) <span style="margin-left: 2em">‌</span>⇒ (a)
|
|
<p class=nopadding>(cons '(a) '(b c d)) <span style="margin-left: 2em">‌</span>⇒ ((a) b c d)</p>
|
|
|
|
<p class=nopadding>(cons "a" '(b c)) <span style="margin-left: 2em">‌</span>⇒ ("a" b c)</p>
|
|
|
|
<p class=nopadding>(cons 'a 3) <span style="margin-left: 2em">‌</span>⇒ (a . 3)</p>
|
|
|
|
<p class=nopadding>(cons '(a b) 'c) <span style="margin-left: 2em">‌</span>⇒ ((a b) . c)</p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_612"></a>car<i> pair</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns the contents of the car field of <i>pair</i>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(car '(a b c)) <span style="margin-left: 2em">‌</span>⇒ a
|
|
<p class=nopadding>(car '((a) b c d)) <span style="margin-left: 2em">‌</span>⇒ (a)</p>
|
|
|
|
<p class=nopadding>(car '(1 . 2)) <span style="margin-left: 2em">‌</span>⇒ 1</p>
|
|
|
|
<p class=nopadding>(car '()) <tt> &assertion</tt> <i>exception</i></p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_614"></a>cdr<i> pair</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns the contents of the cdr field of <i>pair</i>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(cdr '((a) b c d)) <span style="margin-left: 2em">‌</span>⇒ (b c d)
|
|
<p class=nopadding>(cdr '(1 . 2)) <span style="margin-left: 2em">‌</span>⇒ 2</p>
|
|
|
|
<p class=nopadding>(cdr '()) <tt> &assertion</tt> <i>exception</i></p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_616"></a>caar<i> pair</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_618"></a>cadr<i> pair</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<img src="r6rs-Z-G-D-1.gif" border="0" alt="[r6rs-Z-G-D-1.gif]"> <div align=left><tt>(<a name="node_idx_620"></a>cdddar<i> pair</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_622"></a>cddddr<i> pair</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
These procedures are compositions of <tt>car</tt> and <tt>cdr</tt>, where
|
|
for example <tt>caddr</tt> could be defined by</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(define caddr (lambda (x) (car (cdr (cdr x))))).<p></tt></p>
|
|
<p>
|
|
Arbitrary compositions, up to four deep, are provided. There are
|
|
twenty-eight of these procedures in all.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_624"></a>null?<i> obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns <tt>#t</tt> if <i>obj</i> is the empty list<a name="node_idx_626"></a>,
|
|
<tt>#f</tt>otherwise.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_628"></a>list?<i> obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns <tt>#t</tt> if <i>obj</i> is a list, <tt>#f</tt> otherwise.
|
|
By definition, all lists are chains of pairs that have finite length and are terminated by
|
|
the empty list.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(list? '(a b c)) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt>
|
|
<p class=nopadding>(list? '()) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(list? '(a . b)) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_630"></a>list<i> <i>obj</i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns a newly allocated list of its arguments.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(list 'a (+ 3 4) 'c) <span style="margin-left: 2em">‌</span>⇒ (a 7 c)
|
|
<p class=nopadding>(list) <span style="margin-left: 2em">‌</span>⇒ ()</p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_632"></a>length<i> list</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns the length of <i>list</i>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(length '(a b c)) <span style="margin-left: 2em">‌</span>⇒ 3
|
|
<p class=nopadding>(length '(a (b) (c d e))) <span style="margin-left: 2em">‌</span>⇒ 3</p>
|
|
|
|
<p class=nopadding>(length '()) <span style="margin-left: 2em">‌</span>⇒ 0</p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_634"></a>append<i> list <tt>...</tt> obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns a possibly improper list consisting of the elements of the first <i>list</i>
|
|
followed by the elements of the other <i>list</i>s, with <i>obj</i> as
|
|
the cdr of the final pair.
|
|
An improper list results if <i>obj</i> is not a
|
|
list.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(append '(x) '(y)) <span style="margin-left: 2em">‌</span>⇒ (x y)
|
|
<p class=nopadding>(append '(a) '(b c d)) <span style="margin-left: 2em">‌</span>⇒ (a b c d)</p>
|
|
|
|
<p class=nopadding>(append '(a (b)) '((c))) <span style="margin-left: 2em">‌</span>⇒ (a (b) (c))</p>
|
|
|
|
<p class=nopadding>(append '(a b) '(c . d)) <span style="margin-left: 2em">‌</span>⇒ (a b c . d)</p>
|
|
|
|
<p class=nopadding>(append '() 'a) <span style="margin-left: 2em">‌</span>⇒ a</p>
|
|
<p></tt></p>
|
|
<p>
|
|
If <tt>append</tt> constructs a nonempty chain of pairs, it is always
|
|
newly allocated. If no pairs are allocated, <i>obj</i> is returned.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_636"></a>reverse<i> list</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns a newly allocated list consisting of the elements of <i>list</i>
|
|
in reverse order.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(reverse '(a b c)) <span style="margin-left: 2em">‌</span>⇒ (c b a)
|
|
<p class=nopadding>(reverse '(a (b c) d (e (f)))) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ ((e (f)) d (b c) a)</p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_638"></a>list-tail<i> list k</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
<i>List</i> should be a list of size at least <i>k</i>.
|
|
The <tt>list-tail</tt> procedure returns the subchain of pairs of <i>list</i>
|
|
obtained by omitting the first <i>k</i> elements.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(list-tail '(a b c d) 2) <span style="margin-left: 2em">‌</span>⇒ (c d)<p></tt></p>
|
|
<p>
|
|
<em>Implementation responsibilities: </em>The implementation must check that <i>list</i> is a chain of
|
|
pairs whose length is at least <i>k</i>. It should not check that it is a chain
|
|
of pairs beyond this length.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_640"></a>list-ref<i> list k</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
<i>List</i> must be a list whose length is at least <i>k</i> + 1.
|
|
The <tt>list-tail</tt> procedure returns the <i>k</i>th element of <i>list</i>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(list-ref '(a b c d) 2) <span style="margin-left: 2em">‌</span>⇒ c<p></tt></p>
|
|
<p>
|
|
<em>Implementation responsibilities: </em>The implementation must check that <i>list</i> is a chain of
|
|
pairs whose length is at least <i>k</i> + 1. It should not check that it is a list
|
|
of pairs beyond this length.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_642"></a>map<i> proc <i>list<sub>1</sub></i> <i>list<sub>2</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
The <i>list</i>s should all have the same length. <i>Proc</i>
|
|
should accept as many arguments as there are
|
|
<i>list</i>s and return a single value. <i>Proc</i> should not mutate
|
|
any of the <i>list</i>s.</p>
|
|
<p>
|
|
The <tt>map</tt> procedure applies <i>proc</i> element-wise to the elements of the
|
|
<i>list</i>s and returns a list of the results, in order.
|
|
<i>Proc</i> is always called in the same dynamic environment
|
|
as <tt>map</tt> itself.
|
|
The order in which <i>proc</i> is applied to the elements of the
|
|
<i>list</i>s is unspecified.
|
|
If multiple returns occur from <tt>map</tt>, the
|
|
values returned by earlier returns are not mutated.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(map cadr '((a b) (d e) (g h))) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ (b e h)
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(map (lambda (n) (expt n n))</p>
|
|
|
|
<p class=nopadding> '(1 2 3 4 5)) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ (1 4 27 256 3125)</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(map + '(1 2 3) '(4 5 6)) <span style="margin-left: 2em">‌</span>⇒ (5 7 9)</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(let ((count 0))</p>
|
|
|
|
<p class=nopadding> (map (lambda (ignored)</p>
|
|
|
|
<p class=nopadding> (set! count (+ count 1))</p>
|
|
|
|
<p class=nopadding> count)</p>
|
|
|
|
<p class=nopadding> '(a b))) <span style="margin-left: 2em">‌</span>⇒ (1 2) <i>or</i> (2 1)</p>
|
|
<p></tt></p>
|
|
<p>
|
|
<em>Implementation responsibilities: </em>The implementation should check that the <i>list</i>s all
|
|
have the same length. The implementation must check the restrictions
|
|
on <i>proc</i> to the extent performed by applying it as described. An
|
|
implementation may check whether <i>proc</i> is an appropriate argument
|
|
before applying it.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_644"></a>for-each<i> proc <i>list<sub>1</sub></i> <i>list<sub>2</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
The <i>list</i>s should all have the same length. <i>Proc</i>
|
|
should accept as many arguments as there are
|
|
<i>list</i>s. <i>Proc</i> should not mutate
|
|
any of the <i>list</i>s.</p>
|
|
<p>
|
|
The <tt>for-each</tt> procedure applies <i>proc</i>
|
|
element-wise to the elements of the
|
|
<i>list</i>s for its side effects, in order from the first elements to the
|
|
last.
|
|
<i>Proc</i> is always called in the same dynamic environment
|
|
as <tt>for-each</tt> itself.
|
|
The return values of <tt>for-each</tt> are unspecified.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let ((v (make-vector 5)))
|
|
<p class=nopadding> (for-each (lambda (i)</p>
|
|
|
|
<p class=nopadding> (vector-set! v i (* i i)))</p>
|
|
|
|
<p class=nopadding> '(0 1 2 3 4))</p>
|
|
|
|
<p class=nopadding> v) <span style="margin-left: 2em">‌</span>⇒ #(0 1 4 9 16)</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(for-each (lambda (x) x) '(1 2 3 4)) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ <em>unspecified</em></p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(for-each even? '()) <span style="margin-left: 2em">‌</span>⇒ <em>unspecified</em></p>
|
|
<p></tt></p>
|
|
<p>
|
|
<em>Implementation responsibilities: </em>The implementation should check that the <i>list</i>s all
|
|
have the same length. The implementation must check the restrictions
|
|
on <i>proc</i> to the extent performed by applying it as described.
|
|
An implementation may check whether <i>proc</i> is an appropriate argument
|
|
before applying it.</p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
Implementations of <tt>for-each</tt> may or may not tail-call
|
|
<i>proc</i> on the last elements.
|
|
</blockquote><p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.10"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.10">11.10 Symbols</a></h2>
|
|
<p></p>
|
|
<p>
|
|
Symbols are objects whose usefulness rests on the fact that two
|
|
symbols are identical (in the sense of <tt>eq?</tt>, <tt>eqv?</tt> and <tt>equal?</tt>) if and only if their
|
|
names are spelled the same way.
|
|
A symbol literal is formed using <tt>quote</tt>.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_646"></a>symbol?<i> obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns <tt>#t</tt> if <i>obj</i> is a symbol, otherwise returns <tt>#f</tt>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(symbol? 'foo) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt>
|
|
<p class=nopadding>(symbol? (car '(a b))) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(symbol? "bar") <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(symbol? 'nil) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(symbol? '()) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding>(symbol? <tt>#f</tt>) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_648"></a>symbol->string<i> symbol</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns the name of <i>symbol</i> as an immutable string. </p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(symbol->string 'flying-fish)
|
|
<p class=nopadding> <span style="margin-left: 2em">‌</span>⇒ "flying-fish"</p>
|
|
|
|
<p class=nopadding>(symbol->string 'Martin) <span style="margin-left: 2em">‌</span>⇒ "Martin"</p>
|
|
|
|
<p class=nopadding>(symbol->string</p>
|
|
|
|
<p class=nopadding> (string->symbol "Malvina")) </p>
|
|
|
|
<p class=nopadding> <span style="margin-left: 2em">‌</span>⇒ "Malvina"</p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_650"></a>symbol=?<i> <i>symbol<sub>1</sub></i> <i>symbol<sub>2</sub></i> <i>symbol<sub>3</sub></i>
|
|
<tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns <tt>#t</tt> if the symbols are the same, i.e., if their names
|
|
are spelled the same.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_652"></a>string->symbol<i> string</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns the symbol whose name is <i>string</i>. </p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(eq? 'mISSISSIppi 'mississippi) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt>
|
|
<p class=nopadding>(string->symbol "mISSISSIppi") <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒the symbol with name "mISSISSIppi"</p>
|
|
|
|
<p class=nopadding>(eq? 'bitBlt (string->symbol "bitBlt")) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(eq? 'JollyWog</p>
|
|
|
|
<p class=nopadding> (string->symbol</p>
|
|
|
|
<p class=nopadding> (symbol->string 'JollyWog))) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(string=? "K. Harper, M.D."</p>
|
|
|
|
<p class=nopadding> (symbol->string</p>
|
|
|
|
<p class=nopadding> (string->symbol "K. Harper, M.D."))) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.11"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.11">11.11 Characters</a></h2>
|
|
<p></p>
|
|
<p>
|
|
<a name="node_idx_654"></a><a name="node_idx_656"></a></p>
|
|
<p>
|
|
<a name="node_idx_658"></a><em>Characters</em> are objects that represent Unicode scalar
|
|
values [<a href="r6rs-Z-H-21.html#node_bib_27">27</a>].</p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
Unicode defines a standard mapping between sequences of
|
|
<i>Unicode scalar values</i><a name="node_idx_660"></a><a name="node_idx_662"></a>(integers in the range 0 to
|
|
#x10FFFF, excluding the range #xD800 to #xDFFF) in the latest
|
|
version of the standard and human-readable “characters”. More
|
|
precisely, Unicode distinguishes between glyphs, which are printed
|
|
for humans to read, and characters, which are abstract entities that
|
|
map to glyphs (sometimes in a way that's sensitive to surrounding
|
|
characters). Furthermore, different sequences of scalar values
|
|
sometimes correspond to the same character. The relationships among
|
|
scalar, characters, and glyphs are subtle and complex.<p>
|
|
Despite this complexity, most things that a literate human would
|
|
call a “character” can be represented by a single Unicode scalar
|
|
value (although several sequences of Unicode scalar values may
|
|
represent that same character). For example, Roman letters, Cyrillic
|
|
letters, Hebrew consonants, and most Chinese characters fall into
|
|
this category.</p>
|
|
<p>
|
|
Unicode scalar values exclude the range #xD800 to #xDFFF, which
|
|
are part of the range of Unicode <i>code points</i><a name="node_idx_664"></a>. However, the Unicode code points in this range,
|
|
the so-called <i>surrogates</i><a name="node_idx_666"></a>, are an
|
|
artifact of the UTF-16 encoding, and can only appear in specific
|
|
Unicode encodings, and even then only in pairs that encode scalar
|
|
values. Consequently, all characters represent code points, but the
|
|
surrogate code points do not have representations as characters.
|
|
</p>
|
|
</blockquote><p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_668"></a>char?<i> obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns <tt>#t</tt> if <i>obj</i> is a character, otherwise returns <tt>#f</tt>.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_670"></a>char->integer<i> char</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_672"></a>integer->char<i>
|
|
<p class=noindent><em>s</em><em>v</em></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure</p>
|
|
</div>
|
|
<p>
|
|
<i>Sv</i> must be a Unicode scalar value, i.e., a non-negative exact
|
|
integer object in [0, #<em>x</em>D7FF] ∪
|
|
[#<em>x</em>E000, #<em>x</em>10FFFF].</p>
|
|
<p>
|
|
Given a character, <tt>char->integer</tt> returns its Unicode scalar value
|
|
as an exact integer object.
|
|
For a Unicode scalar value <i>sv</i>, <tt>integer->char</tt>
|
|
returns its associated character.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(integer->char 32) <span style="margin-left: 2em">‌</span>⇒ <tt>#</tt><tt>\</tt>space
|
|
<p class=nopadding>(char->integer (integer->char 5000))</p>
|
|
|
|
<p class=nopadding><span style="margin-left: 2em">‌</span>⇒ 5000</p>
|
|
|
|
<p class=nopadding>(integer->char <tt>#</tt><tt>\</tt>xD800) <tt> &assertion</tt> <i>exception</i></p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_674"></a>char=?<i> <i>char<sub>1</sub></i> <i>char<sub>2</sub></i> <i>char<sub>3</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_676"></a>char<?<i> <i>char<sub>1</sub></i> <i>char<sub>2</sub></i> <i>char<sub>3</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_678"></a>char>?<i> <i>char<sub>1</sub></i> <i>char<sub>2</sub></i> <i>char<sub>3</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_680"></a>char<=?<i> <i>char<sub>1</sub></i> <i>char<sub>2</sub></i> <i>char<sub>3</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_682"></a>char>=?<i> <i>char<sub>1</sub></i> <i>char<sub>2</sub></i> <i>char<sub>3</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
|
|
These procedures impose a total ordering on the set of characters
|
|
according to their Unicode scalar values.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(char<? <tt>#</tt><tt>\</tt>z <tt>#</tt><tt>\</tt>ß) <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt>
|
|
<p class=nopadding>(char<? <tt>#</tt><tt>\</tt>z <tt>#</tt><tt>\</tt>Z) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.12"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.12">11.12 Strings</a></h2>
|
|
<p></p>
|
|
<p>
|
|
Strings are sequences of characters. </p>
|
|
<p>
|
|
The <em>length</em> of a string is the number of characters that it
|
|
contains. This number is fixed when the
|
|
string is created. The <a name="node_idx_684"></a><em>valid indices</em> of a string are the
|
|
integers less than the length of the string. The first
|
|
character of a string has index 0, the second has index 1, and so on.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_686"></a>string?<i> obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns <tt>#t</tt> if <i>obj</i> is a string, otherwise returns <tt>#f</tt>.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_688"></a>make-string<i> k</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_690"></a>make-string<i> k char</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns a newly allocated string of
|
|
length <i>k</i>. If <i>char</i> is given, then all elements of the string
|
|
are initialized to <i>char</i>, otherwise the contents of the
|
|
<i>string</i> are unspecified.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_692"></a>string<i> char <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns a newly allocated string composed of the arguments.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_694"></a>string-length<i> string</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns the number of characters in the given <i>string</i> as an exact
|
|
integer object.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_696"></a>string-ref<i> string k</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
<i>K</i> must be a valid index of <i>string</i>.
|
|
The <tt>string-ref</tt> procedure returns character </p>
|
|
|
|
<p class=noindent><em>k</em> of <i>string</i> using zero-origin indexing.</p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
Implementors should make <tt>string-ref</tt> run in constant
|
|
time.
|
|
</blockquote>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_698"></a>string=?<i> <i>string<sub>1</sub></i> <i>string<sub>2</sub></i> <i>string<sub>3</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns <tt>#t</tt> if the strings are the same length and contain the same
|
|
characters in the same positions. Otherwise, the <tt>string=?</tt>
|
|
procedure returns <tt>#f</tt>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(string=? "Straße" "Strasse") <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt><p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_700"></a>string<?<i> <i>string<sub>1</sub></i> <i>string<sub>2</sub></i> <i>string<sub>3</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_702"></a>string>?<i> <i>string<sub>1</sub></i> <i>string<sub>2</sub></i> <i>string<sub>3</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_704"></a>string<=?<i> <i>string<sub>1</sub></i> <i>string<sub>2</sub></i> <i>string<sub>3</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_706"></a>string>=?<i> <i>string<sub>1</sub></i> <i>string<sub>2</sub></i> <i>string<sub>3</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
These procedures are the lexicographic extensions to strings of the
|
|
corresponding orderings on characters. For example, <tt>string<?</tt> is
|
|
the lexicographic ordering on strings induced by the ordering
|
|
<tt>char<?</tt> on characters. If two strings differ in length but
|
|
are the same up to the length of the shorter string, the shorter string
|
|
is considered to be lexicographically less than the longer string.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(string<? "z" "ß") <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt>
|
|
<p class=nopadding>(string<? "z" "zz") <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
|
|
<p class=nopadding>(string<? "z" "Z") <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_708"></a>substring<i> string start end</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
<i>String</i> must be a string, and <i>start</i> and <i>end</i>
|
|
must be exact integer objects satisfying
|
|
</p>
|
|
<div class=mathdisplay align=left><table><tr><td></td><td><table><tr><td align=center>0 ≤ </td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>start</td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td> ≤ </td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>end</td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td> ≤ </td><td><table><tr><td align=center><tt>(string-length <i>string</i>).</tt></td></tr></table></td><td></td></tr></table></td><td></td></tr></table></div>
|
|
<p class=noindent>
|
|
The <tt>substring</tt> procedure returns a newly allocated string formed from the characters of
|
|
<i>string</i> beginning with index <i>start</i> (inclusive) and ending with index
|
|
<i>end</i> (exclusive).
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_710"></a>string-append<i> <i>string</i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns a newly allocated string whose characters form the concatenation of the
|
|
given strings.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_712"></a>string->list<i> string</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_714"></a>list->string<i> list</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
<i>List</i> must be a list of characters.
|
|
The <tt>string->list</tt> procedure returns a newly allocated list of the
|
|
characters that make up the given string. The <tt>list->string</tt> procedure
|
|
returns a newly allocated string formed from the characters in
|
|
<i>list</i>. The <tt>string->list</tt>
|
|
and <tt>list->string</tt> procedures are
|
|
inverses so far as <tt>equal?</tt> is concerned.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_716"></a>string-for-each<i> proc <i>string<sub>1</sub></i> <i>string<sub>2</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
The <i>string</i>s must all have the same length. <i>Proc</i>
|
|
should accept as many arguments as there are <i>string</i>s.
|
|
The <tt>string-for-each</tt> procedure applies <i>proc</i>
|
|
element-wise to the characters of the
|
|
<i>string</i>s for its side effects, in order from the first characters to the
|
|
last.
|
|
<i>Proc</i> is always called in the same dynamic environment
|
|
as <tt>string-for-each</tt> itself.
|
|
The return values of <tt>string-for-each</tt> are unspecified.</p>
|
|
<p>
|
|
Analogous to <tt>for-each</tt>.</p>
|
|
<p>
|
|
<em>Implementation responsibilities: </em>The implementation must check the restrictions
|
|
on <i>proc</i> to the extent performed by applying it as described.
|
|
An
|
|
implementation may check whether <i>proc</i> is an appropriate argument
|
|
before applying it.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_718"></a>string-copy<i> string</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns a newly allocated copy of the given <i>string</i>.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.13"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.13">11.13 Vectors</a></h2>
|
|
<p></p>
|
|
<p>
|
|
Vectors are heterogeneous structures whose elements are indexed
|
|
by integers. A vector typically occupies less space than a list
|
|
of the same length, and the average time needed to access a randomly
|
|
chosen element is typically less for the vector than for the list.</p>
|
|
<p>
|
|
The <em>length</em> of a vector is the number of elements that it
|
|
contains. This number is a non-negative integer that is fixed when the
|
|
vector is created.
|
|
The <em>valid indices</em><a name="node_idx_720"></a>of a
|
|
vector are the exact non-negative integer objects less than the length of the
|
|
vector. The first element in a vector is indexed by zero, and the last
|
|
element is indexed by one less than the length of the vector.</p>
|
|
<p>
|
|
Like list constants, vector constants must be quoted:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>'#(0 (2 2 2 2) "Anna") <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ #(0 (2 2 2 2) "Anna")<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_722"></a>vector?<i> obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns <tt>#t</tt> if <i>obj</i> is a vector. Otherwise the procedure
|
|
returns <tt>#f</tt>.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_724"></a>make-vector<i> k</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_726"></a>make-vector<i> k fill</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns a newly allocated vector of <i>k</i> elements. If a second
|
|
argument is given, then each element is initialized to <i>fill</i>.
|
|
Otherwise the initial contents of each element is unspecified.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_728"></a>vector<i> obj <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns a newly allocated vector whose elements contain the given
|
|
arguments. Analogous to <tt>list</tt>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(vector 'a 'b 'c) <span style="margin-left: 2em">‌</span>⇒ #(a b c)<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_730"></a>vector-length<i> vector</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Returns the number of elements in <i>vector</i> as an exact integer object.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_732"></a>vector-ref<i> vector k</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
<i>K</i> must be a valid index of <i>vector</i>.
|
|
The <tt>vector-ref</tt> procedure returns the contents of element </p>
|
|
|
|
<p class=noindent><em>k</em> of
|
|
<i>vector</i>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(vector-ref '#(1 1 2 3 5 8 13 21) 5) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ 8<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_734"></a>vector-set!<i> vector k obj</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
<i>K</i> must be a valid index of <i>vector</i>.
|
|
The <tt>vector-set!</tt> procedure stores <i>obj</i> in element </p>
|
|
|
|
<p class=noindent><em>k</em> of
|
|
<i>vector</i>, and returns unspecified values.</p>
|
|
<p>
|
|
Passing an immutable vector to <tt>vector-set!</tt> should cause an exception
|
|
with condition type <tt>&assertion</tt> to be raised.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let ((vec (vector 0 '(2 2 2 2) "Anna")))
|
|
<p class=nopadding> (vector-set! vec 1 '("Sue" "Sue"))</p>
|
|
|
|
<p class=nopadding> vec) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ #(0 ("Sue" "Sue") "Anna")</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(vector-set! '#(0 1 2) 1 "doe") <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ <i>unspecified</i></p>
|
|
|
|
<p class=nopadding> ; constant vector</p>
|
|
|
|
<p class=nopadding> ; should raise <tt> &assertion</tt> <i>exception</i></p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_736"></a>vector->list<i> vector</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_738"></a>list->vector<i> list</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
The <tt>vector->list</tt> procedure returns a newly allocated list of the objects contained
|
|
in the elements of <i>vector</i>. The <tt>list->vector</tt> procedure returns a newly
|
|
created vector initialized to the elements of the list <i>list</i>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(vector->list '#(dah dah didah)) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ (dah dah didah)
|
|
<p class=nopadding>(list->vector '(dididit dah)) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ #(dididit dah)</p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_740"></a>vector-fill!<i> vector fill</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Stores <i>fill</i> in every element of <i>vector</i>
|
|
and returns unspecified values.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_742"></a>vector-map<i> proc <i>vector<sub>1</sub></i> <i>vector<sub>2</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
The <i>vector</i>s must all have the same length. <i>Proc</i>
|
|
should accept as many arguments as there are <i>vector</i>s and return a
|
|
single value.</p>
|
|
<p>
|
|
The <tt>vector-map</tt> procedure applies <i>proc</i> element-wise to the elements of the
|
|
<i>vector</i>s and returns a vector of the results, in order.
|
|
<i>Proc</i> is always called in the same dynamic environment
|
|
as <tt>vector-map</tt> itself.
|
|
The order in which <i>proc</i> is applied to the elements of the
|
|
<i>vector</i>s is unspecified.
|
|
If multiple returns occur from <tt>vector-map</tt>, the return
|
|
values returned by earlier returns are not mutated.</p>
|
|
<p>
|
|
Analogous to <tt>map</tt>.</p>
|
|
<p>
|
|
<em>Implementation responsibilities: </em>The implementation must check the restrictions
|
|
on <i>proc</i> to the extent performed by applying it as described.
|
|
An
|
|
implementation may check whether <i>proc</i> is an appropriate argument
|
|
before applying it.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_744"></a>vector-for-each<i> proc <i>vector<sub>1</sub></i> <i>vector<sub>2</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
The <i>vector</i>s must all have the same length. <i>Proc</i>
|
|
should accept as many arguments as there are <i>vector</i>s.
|
|
The <tt>vector-for-each</tt> procedure applies <i>proc</i>
|
|
element-wise to the elements of the
|
|
<i>vector</i>s for its side effects, in order from the first elements to the
|
|
last.
|
|
<i>Proc</i> is always called in the same dynamic environment
|
|
as <tt>vector-for-each</tt> itself.
|
|
The return values of <tt>vector-for-each</tt> are unspecified.</p>
|
|
<p>
|
|
Analogous to <tt>for-each</tt>.</p>
|
|
<p>
|
|
<em>Implementation responsibilities: </em>The implementation must check the restrictions
|
|
on <i>proc</i> to the extent performed by applying it as described.
|
|
An
|
|
implementation may check whether <i>proc</i> is an appropriate argument
|
|
before applying it.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.14"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.14">11.14 Errors and violations</a></h2>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_746"></a>error<i> who message <i>irritant<sub>1</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_748"></a>assertion-violation<i> who message <i>irritant<sub>1</sub></i> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
<i>Who</i> must be a string or a symbol or <tt>#f</tt>.
|
|
<i>Message</i> must be a string.
|
|
The <i>irritant</i>s are arbitrary objects.</p>
|
|
<p>
|
|
These procedures raise an exception. The <tt>error</tt>
|
|
procedure should be called when an error has occurred, typically caused by
|
|
something that has gone wrong in the interaction of the program with the
|
|
external world or the user. The <tt>assertion-violation</tt> procedure
|
|
should be called when an invalid call to a procedure was made, either passing an
|
|
invalid number of arguments, or passing an argument that it is not
|
|
specified to handle.</p>
|
|
<p>
|
|
The <i>who</i> argument should describe the procedure or operation that
|
|
detected the exception. The <i>message</i> argument should describe
|
|
the exceptional situation. The <i>irritant</i>s should be the arguments
|
|
to the operation that detected the operation.</p>
|
|
<p>
|
|
The condition object provided with the exception (see
|
|
library chapter on “Exceptions
|
|
and conditions”) has the following condition types:
|
|
</p>
|
|
<ul>
|
|
<li><p>If <i>who</i> is not <tt>#f</tt>, the condition has condition type
|
|
<tt>&who</tt>, with <i>who</i> as the value of its field. In
|
|
that case, <i>who</i> should be the name of the procedure or entity that
|
|
detected the exception. If it is <tt>#f</tt>, the condition does not
|
|
have condition type <tt>&who</tt>.
|
|
</p>
|
|
<li><p>The condition has condition type <tt>&message</tt>, with
|
|
<i>message</i> as the value of its field.
|
|
</p>
|
|
<li><p>The condition has condition type <tt>&irritants</tt>, and its
|
|
field has as its value a list of the <i>irritant</i>s.
|
|
</p>
|
|
</ul><p>
|
|
Moreover, the condition created by <tt>error</tt> has condition type
|
|
<tt>&error</tt>, and the condition created by <tt>assertion-violation</tt> has
|
|
condition type <tt>&assertion</tt>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(define (fac n)
|
|
<p class=nopadding> (if (not (integer-valued? n))</p>
|
|
|
|
<p class=nopadding> (assertion-violation</p>
|
|
|
|
<p class=nopadding> 'fac "non-integral argument" n))</p>
|
|
|
|
<p class=nopadding> (if (negative? n)</p>
|
|
|
|
<p class=nopadding> (assertion-violation</p>
|
|
|
|
<p class=nopadding> 'fac "negative argument" n))</p>
|
|
|
|
<p class=nopadding> (letrec</p>
|
|
|
|
<p class=nopadding> ((loop (lambda (n r)</p>
|
|
|
|
<p class=nopadding> (if (zero? n)</p>
|
|
|
|
<p class=nopadding> r</p>
|
|
|
|
<p class=nopadding> (loop (- n 1) (* r n))))))</p>
|
|
|
|
<p class=nopadding> (loop n 1)))</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(fac 5) <span style="margin-left: 2em">‌</span>⇒ 120</p>
|
|
|
|
<p class=nopadding>(fac 4.5) <tt> &assertion</tt> <i>exception</i></p>
|
|
|
|
<p class=nopadding>(fac -3) <tt> &assertion</tt> <i>exception</i></p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_750"></a>assert<i> <expression></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
An <tt>assert</tt> form is evaluated by evaluating <expression>.
|
|
If <expression> returns a true value, that value is returned
|
|
from the <tt>assert</tt> expression. If <expression> returns
|
|
<tt>#f</tt>, an exception with condition types <tt>&assertion</tt> and
|
|
<tt>&message</tt> is raised. The message provided in the condition
|
|
object is implementation-dependent.</p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
Implementations should exploit the fact that
|
|
<tt>assert</tt> is syntax to provide as much information as possible
|
|
about the location of the assertion failure.
|
|
</blockquote>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.15"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.15">11.15 Control features</a></h2>
|
|
<p>
|
|
</p>
|
|
<p>
|
|
This chapter describes various primitive procedures which control the
|
|
flow of program execution in special ways.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_752"></a>apply<i> proc <i>arg<sub>1</sub></i> <tt>...</tt> rest-args</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
<i>Rest-args</i> must be a list.
|
|
<i>Proc</i> should accept <em>n</em> arguments, where <em>n</em> is
|
|
number of <i>arg</i>s plus the length of <i>rest-args</i>.
|
|
The <tt>apply</tt> procedure calls <i>proc</i> with the elements of the list
|
|
<tt>(append (list <i>arg<sub>1</sub></i> <tt>...</tt>) <i>rest-args</i>)</tt> as the actual
|
|
arguments.</p>
|
|
<p>
|
|
If a call to <tt>apply</tt> occurs in a tail context, the call
|
|
to <i>proc</i> is also in a tail context.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(apply + (list 3 4)) <span style="margin-left: 2em">‌</span>⇒ 7
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(define compose</p>
|
|
|
|
<p class=nopadding> (lambda (f g)</p>
|
|
|
|
<p class=nopadding> (lambda args</p>
|
|
|
|
<p class=nopadding> (f (apply g args)))))</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>((compose sqrt *) 12 75) <span style="margin-left: 2em">‌</span>⇒ 30</p>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_754"></a>call-with-current-continuation<i> proc</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_756"></a>call/cc<i> proc</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
<i>Proc</i> should accept one
|
|
argument. The procedure <tt>call-with-current-continuation</tt>
|
|
(which is the same as the procedure <tt>call/cc</tt>) packages
|
|
the current continuation as an “escape
|
|
procedure”<a name="node_idx_758"></a>and passes it as an argument to
|
|
<i>proc</i>. The escape procedure is a Scheme procedure that, if it is
|
|
later called, will abandon whatever continuation is in effect at that later
|
|
time and will instead reinstate the continuation that was in effect
|
|
when the escape procedure was created. Calling the escape procedure
|
|
may cause the invocation of <i>before</i> and <i>after</i> procedures installed using
|
|
<tt>dynamic-wind</tt>.</p>
|
|
<p>
|
|
The escape procedure accepts the same number of arguments as the
|
|
continuation of the original call to <tt>call-with-current-continuation</tt>.</p>
|
|
<p>
|
|
The escape procedure that is passed to <i>proc</i> has
|
|
unlimited extent just like any other procedure in Scheme. It may be stored
|
|
in variables or data structures and may be called as many times as desired.</p>
|
|
<p>
|
|
If a call to <tt>call-with-current-continuation</tt> occurs in a tail
|
|
context, the call to <i>proc</i> is also in a tail context.</p>
|
|
<p>
|
|
The following examples show only some ways in which
|
|
<tt>call-with-current-continuation</tt> is used. If all real uses were as
|
|
simple as these examples, there would be no need for a procedure with
|
|
the power of <tt>call-with-current-continuation</tt>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(call-with-current-continuation
|
|
<p class=nopadding> (lambda (exit)</p>
|
|
|
|
<p class=nopadding> (for-each (lambda (x)</p>
|
|
|
|
<p class=nopadding> (if (negative? x)</p>
|
|
|
|
<p class=nopadding> (exit x)))</p>
|
|
|
|
<p class=nopadding> '(54 0 37 -3 245 19))</p>
|
|
|
|
<p class=nopadding> <tt>#t</tt>)) <span style="margin-left: 2em">‌</span>⇒ -3</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(define list-length</p>
|
|
|
|
<p class=nopadding> (lambda (obj)</p>
|
|
|
|
<p class=nopadding> (call-with-current-continuation</p>
|
|
|
|
<p class=nopadding> (lambda (return)</p>
|
|
|
|
<p class=nopadding> (letrec ((r</p>
|
|
|
|
<p class=nopadding> (lambda (obj)</p>
|
|
|
|
<p class=nopadding> (cond ((null? obj) 0)</p>
|
|
|
|
<p class=nopadding> ((pair? obj)</p>
|
|
|
|
<p class=nopadding> (+ (r (cdr obj)) 1))</p>
|
|
|
|
<p class=nopadding> (else (return <tt>#f</tt>))))))</p>
|
|
|
|
<p class=nopadding> (r obj))))))</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(list-length '(1 2 3 4)) <span style="margin-left: 2em">‌</span>⇒ 4</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(list-length '(a b . c)) <span style="margin-left: 2em">‌</span>⇒ <tt>#f</tt></p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(call-with-current-continuation procedure?)</p>
|
|
|
|
<p class=nopadding> <span style="margin-left: 2em">‌</span>⇒ <tt>#t</tt></p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
Calling an escape procedure reenters the dynamic extent of the call
|
|
to <tt>call-with-current-continuation</tt>, and thus restores its
|
|
dynamic environment; see section <a href="r6rs-Z-H-8.html#node_sec_5.12">5.12</a>.
|
|
</blockquote><p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_760"></a>values<i> obj <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
Delivers all of its arguments to its continuation.
|
|
The <tt>values</tt> procedure might be defined as follows:
|
|
</p>
|
|
|
|
<tt>(define (values . things)
|
|
<p class=nopadding> (call-with-current-continuation </p>
|
|
|
|
<p class=nopadding> (lambda (cont) (apply cont things))))</p>
|
|
<p></tt></p>
|
|
<p>
|
|
The continuations of all non-final expressions within a sequence of
|
|
expressions, such as in <tt>lambda</tt>, <tt>begin</tt>, <tt>let</tt>, <tt>let*</tt>, <tt>letrec</tt>, <tt>letrec*</tt>, <tt>let-values</tt>, <tt>let*-values</tt>, <tt>case</tt>, and <tt>cond</tt> forms, usually take an
|
|
arbitrary number of values.</p>
|
|
<p>
|
|
Except for these and the continuations created by <tt>call-with-values</tt>, <tt>let-values</tt>, and <tt>let*-values</tt>,
|
|
continuations implicitly accepting a single value, such as the
|
|
continuations of <operator> and <operand>s of procedure
|
|
calls or the <test> expressions in conditionals, take exactly
|
|
one value. The effect of passing an inappropriate number of values to
|
|
such a continuation is undefined.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_762"></a>call-with-values<i> producer consumer</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
<i>Producer</i> must be a procedure and should accept zero
|
|
arguments. <i>Consumer</i> must be a procedure and should accept as many
|
|
values as <i>producer</i> returns.
|
|
The <tt>call-with-values</tt> procedure calls <i>producer</i> with no arguments and
|
|
a continuation that, when passed some values, calls the
|
|
<i>consumer</i> procedure with those values as arguments.
|
|
The continuation for the call to <i>consumer</i> is the
|
|
continuation of the call to <tt>call-with-values</tt>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(call-with-values (lambda () (values 4 5))
|
|
<p class=nopadding> (lambda (a b) b))</p>
|
|
|
|
<p class=nopadding> <span style="margin-left: 2em">‌</span>⇒ 5</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(call-with-values * -) <span style="margin-left: 2em">‌</span>⇒ -1</p>
|
|
<p></tt></p>
|
|
<p>
|
|
If a call to <tt>call-with-values</tt> occurs in a tail context, the call
|
|
to <i>consumer</i> is also in a tail context.</p>
|
|
<p>
|
|
<em>Implementation responsibilities: </em>After <i>producer</i> returns, the implementation must check
|
|
that <i>consumer</i> accepts as many values as <i>consumer</i> has
|
|
returned.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_764"></a>dynamic-wind<i> before thunk after</i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>procedure </div>
|
|
<p>
|
|
<i>Before</i>, <i>thunk</i>, and <i>after</i> must be procedures,
|
|
and each should accept zero arguments. These procedures may return
|
|
any number of values. The <tt>dynamic-wind</tt> procedure calls
|
|
<i>thunk</i> without arguments, returning the results of this call.
|
|
Moreover, <tt>dynamic-wind</tt> calls <i>before</i> without arguments
|
|
whenever the dynamic extent of the call to <i>thunk</i> is entered, and
|
|
<i>after</i> without arguments whenever the dynamic extent of the call
|
|
to <i>thunk</i> is exited. Thus, in the absence of calls to escape
|
|
procedures created by <tt>call-with-current-continuation</tt>, <tt>dynamic-wind</tt> calls <i>before</i>, <i>thunk</i>, and <i>after</i>, in
|
|
that order.</p>
|
|
<p>
|
|
While the calls to <i>before</i> and <i>after</i> are not considered to be
|
|
within the dynamic extent of the call to <i>thunk</i>, calls to the <i>before</i>
|
|
and <i>after</i> procedures of any other calls to <tt>dynamic-wind</tt> that occur
|
|
within the dynamic extent of the call to <i>thunk</i> are considered to be
|
|
within the dynamic extent of the call to <i>thunk</i>.</p>
|
|
<p>
|
|
More precisely, an escape procedure transfers control out of the
|
|
dynamic extent of a set of zero or more active <tt>dynamic-wind</tt>
|
|
calls <em>x</em> <tt>...</tt> and transfer control into the dynamic extent
|
|
of a set of zero or more active <tt>dynamic-wind</tt> calls
|
|
<em>y</em> <tt>...</tt>.
|
|
It leaves the dynamic extent of the most recent <em>x</em> and calls without
|
|
arguments the corresponding <i>after</i> procedure.
|
|
If the <i>after</i> procedure returns, the escape procedure proceeds to
|
|
the next most recent <em>x</em>, and so on.
|
|
Once each <em>x</em> has been handled in this manner,
|
|
the escape procedure calls without arguments the <i>before</i> procedure
|
|
corresponding to the least recent <em>y</em>.
|
|
If the <i>before</i> procedure returns, the escape procedure reenters the
|
|
dynamic extent of the least recent <em>y</em> and proceeds with the next least
|
|
recent <em>y</em>, and so on.
|
|
Once each <em>y</em> has been handled in this manner, control is transferred to
|
|
the continuation packaged in the escape procedure.</p>
|
|
<p>
|
|
<em>Implementation responsibilities: </em>The implementation must check the restrictions on
|
|
<i>thunk</i> and <i>after</i> only if they are actually called.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let ((path '())
|
|
<p class=nopadding> (c #f))</p>
|
|
|
|
<p class=nopadding> (let ((add (lambda (s)</p>
|
|
|
|
<p class=nopadding> (set! path (cons s path)))))</p>
|
|
|
|
<p class=nopadding> (dynamic-wind</p>
|
|
|
|
<p class=nopadding> (lambda () (add 'connect))</p>
|
|
|
|
<p class=nopadding> (lambda ()</p>
|
|
|
|
<p class=nopadding> (add (call-with-current-continuation</p>
|
|
|
|
<p class=nopadding> (lambda (c0)</p>
|
|
|
|
<p class=nopadding> (set! c c0)</p>
|
|
|
|
<p class=nopadding> 'talk1))))</p>
|
|
|
|
<p class=nopadding> (lambda () (add 'disconnect)))</p>
|
|
|
|
<p class=nopadding> (if (< (length path) 4)</p>
|
|
|
|
<p class=nopadding> (c 'talk2)</p>
|
|
|
|
<p class=nopadding> (reverse path))))</p>
|
|
|
|
<p class=nopadding> <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ (connect talk1 disconnect</p>
|
|
|
|
<p class=nopadding> connect talk2 disconnect)</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(let ((n 0))</p>
|
|
|
|
<p class=nopadding> (call-with-current-continuation</p>
|
|
|
|
<p class=nopadding> (lambda (k)</p>
|
|
|
|
<p class=nopadding> (dynamic-wind</p>
|
|
|
|
<p class=nopadding> (lambda ()</p>
|
|
|
|
<p class=nopadding> (set! n (+ n 1))</p>
|
|
|
|
<p class=nopadding> (k))</p>
|
|
|
|
<p class=nopadding> (lambda ()</p>
|
|
|
|
<p class=nopadding> (set! n (+ n 2)))</p>
|
|
|
|
<p class=nopadding> (lambda ()</p>
|
|
|
|
<p class=nopadding> (set! n (+ n 4))))))</p>
|
|
|
|
<p class=nopadding> n) <span style="margin-left: 2em">‌</span>⇒ 1</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(let ((n 0))</p>
|
|
|
|
<p class=nopadding> (call-with-current-continuation</p>
|
|
|
|
<p class=nopadding> (lambda (k)</p>
|
|
|
|
<p class=nopadding> (dynamic-wind</p>
|
|
|
|
<p class=nopadding> values</p>
|
|
|
|
<p class=nopadding> (lambda ()</p>
|
|
|
|
<p class=nopadding> (dynamic-wind</p>
|
|
|
|
<p class=nopadding> values</p>
|
|
|
|
<p class=nopadding> (lambda ()</p>
|
|
|
|
<p class=nopadding> (set! n (+ n 1))</p>
|
|
|
|
<p class=nopadding> (k))</p>
|
|
|
|
<p class=nopadding> (lambda ()</p>
|
|
|
|
<p class=nopadding> (set! n (+ n 2))</p>
|
|
|
|
<p class=nopadding> (k))))</p>
|
|
|
|
<p class=nopadding> (lambda ()</p>
|
|
|
|
<p class=nopadding> (set! n (+ n 4))))))</p>
|
|
|
|
<p class=nopadding> n) <span style="margin-left: 2em">‌</span>⇒ 7</p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
Entering a dynamic extent restores its dynamic environment; see
|
|
section <a href="r6rs-Z-H-8.html#node_sec_5.12">5.12</a>.
|
|
</blockquote>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.16"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.16">11.16 Iteration</a></h2>
|
|
<p><div style="height: -9.0pt"></div>
|
|
<p style="margin-top: 0pt; margin-bottom: 0pt"></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_766"></a>let<i> <variable> <bindings> <body></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
|
|
“Named <tt>let</tt>” is a variant on the syntax of <tt>let</tt> that provides
|
|
a general looping construct and may also be used to express
|
|
recursion.
|
|
It has the same syntax and semantics as ordinary <tt>let</tt>
|
|
except that <variable> is bound within <body> to a procedure
|
|
whose parameters are the bound variables and whose body is
|
|
<body>. Thus the execution of <body> may be repeated by
|
|
invoking the procedure named by <variable>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let loop ((numbers '(3 -2 1 6 -5))
|
|
<p class=nopadding> (nonneg '())</p>
|
|
|
|
<p class=nopadding> (neg '()))</p>
|
|
|
|
<p class=nopadding> (cond ((null? numbers) (list nonneg neg))</p>
|
|
|
|
<p class=nopadding> ((>= (car numbers) 0)</p>
|
|
|
|
<p class=nopadding> (loop (cdr numbers)</p>
|
|
|
|
<p class=nopadding> (cons (car numbers) nonneg)</p>
|
|
|
|
<p class=nopadding> neg))</p>
|
|
|
|
<p class=nopadding> ((< (car numbers) 0)</p>
|
|
|
|
<p class=nopadding> (loop (cdr numbers)</p>
|
|
|
|
<p class=nopadding> nonneg</p>
|
|
|
|
<p class=nopadding> (cons (car numbers) neg))))) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ ((6 1 3) (-5 -2))</p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.17"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.17">11.17 Quasiquotation</a></h2>
|
|
<p><div style="height: -9.0pt"></div>
|
|
<p style="margin-top: 0pt; margin-bottom: 0pt"></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_768"></a>quasiquote<i> <qq template></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
|
|
<a name="node_idx_770"></a><div align=left><tt>unquote</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>auxiliary syntax </div>
|
|
|
|
<a name="node_idx_772"></a><div align=left><tt>unquote-splicing</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>auxiliary syntax </div>
|
|
<p>
|
|
“Backquote” or “quasiquote”<a name="node_idx_774"></a>expressions are useful
|
|
for constructing a list or vector structure when some but not all of the
|
|
desired structure is known in advance. </p>
|
|
<p>
|
|
<em>Syntax: </em><Qq template> should be as specified by the grammar at
|
|
the end of this entry.</p>
|
|
<p>
|
|
<em>Semantics: </em>If no
|
|
<tt>unquote</tt> or <tt>unquote-splicing</tt> forms
|
|
appear within the <qq template>, the result of
|
|
evaluating
|
|
<tt>(quasiquote <qq template>)</tt> is equivalent to the result of evaluating
|
|
<tt>(quote <qq template>)</tt>.</p>
|
|
<p>
|
|
If an <tt>(unquote <expression> <tt>...</tt>)</tt> form appears inside a
|
|
<qq template>, however, the <expression>s are evaluated
|
|
(“unquoted”) and their results are inserted into the structure instead
|
|
of the <tt>unquote</tt> form.</p>
|
|
<p>
|
|
If an <tt>(unquote-splicing <expression> <tt>...</tt>)</tt> form
|
|
appears inside a <qq template>, then the <expression>s must
|
|
evaluate to lists; the opening and closing parentheses of the lists are
|
|
then “stripped away” and the elements of the lists are inserted in
|
|
place of the <tt>unquote-splicing</tt> form.</p>
|
|
<p>
|
|
Any <tt>unquote-splicing</tt> or multi-operand <tt>unquote</tt> form must
|
|
appear only within a list or vector <qq template>.</p>
|
|
<p>
|
|
As noted in section <a href="r6rs-Z-H-7.html#node_sec_4.3.5">4.3.5</a>,
|
|
<tt>(quasiquote <qq template>)</tt> may be abbreviated
|
|
<tt>`</tt><qq template>,
|
|
<tt>(unquote <expression>)</tt> may be abbreviated
|
|
<tt>,</tt><expression>, and
|
|
<tt>(unquote-splicing <expression>)</tt> may be abbreviated
|
|
<tt>,</tt><tt>@</tt><expression>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>`(list ,(+ 1 2) 4) <span style="margin-left: 2em">‌</span>⇒ (list 3 4)
|
|
<p class=nopadding>(let ((name 'a)) `(list ,name ',name)) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ (list a (quote a))</p>
|
|
|
|
<p class=nopadding>`(a ,(+ 1 2) ,@(map abs '(4 -5 6)) b) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ (a 3 4 5 6 b)</p>
|
|
|
|
<p class=nopadding>`((<tt> foo</tt> ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons))) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ ((foo 7) . cons)</p>
|
|
|
|
<p class=nopadding>`#(10 5 ,(sqrt 4) ,@(map sqrt '(16 9)) 8) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ #(10 5 2 4 3 8)</p>
|
|
|
|
<p class=nopadding>(let ((name 'foo))</p>
|
|
|
|
<p class=nopadding> `((unquote name name name)))<br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ (foo foo foo)</p>
|
|
|
|
<p class=nopadding>(let ((name '(foo)))</p>
|
|
|
|
<p class=nopadding> `((unquote-splicing name name name)))<br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ (foo foo foo)</p>
|
|
|
|
<p class=nopadding>(let ((q '((append x y) (sqrt 9))))</p>
|
|
|
|
<p class=nopadding> ``(foo ,,@q)) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ `(foo</p>
|
|
|
|
<p class=nopadding> (unquote (append x y) (sqrt 9)))</p>
|
|
|
|
<p class=nopadding>(let ((x '(2 3))</p>
|
|
|
|
<p class=nopadding> (y '(4 5)))</p>
|
|
|
|
<p class=nopadding> `(foo (unquote (append x y) (sqrt 9)))) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ (foo (2 3 4 5) 3)</p>
|
|
<p></tt></p>
|
|
<p>
|
|
Quasiquote forms may be nested. Substitutions are made only for
|
|
unquoted components appearing at the same nesting level
|
|
as the outermost <tt>quasiquote</tt>. The nesting level increases by one inside
|
|
each successive quasiquotation, and decreases by one inside each
|
|
unquotation.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>`(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ (a `(b ,(+ 1 2) ,(foo 4 d) e) f)
|
|
<p class=nopadding>(let ((name1 'x)</p>
|
|
|
|
<p class=nopadding> (name2 'y))</p>
|
|
|
|
<p class=nopadding> `(a `(b ,,name1 ,',name2 d) e)) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ (a `(b ,x ,'y d) e)</p>
|
|
<p></tt></p>
|
|
<p>
|
|
A <tt>quasiquote</tt> expression may return either fresh, mutable objects
|
|
or literal structure for any structure that is constructed at run time
|
|
during the evaluation of the expression. Portions that do not need to
|
|
be rebuilt are always literal. Thus,
|
|
</p>
|
|
|
|
<tt>(let ((a 3)) `((1 2) ,a ,4 ,'five 6))<p></tt>
|
|
may be equivalent to either of the following expressions:
|
|
</p>
|
|
|
|
<tt>'((1 2) 3 4 five 6)
|
|
<p class=nopadding>(let ((a 3)) </p>
|
|
|
|
<p class=nopadding> (cons '(1 2)</p>
|
|
|
|
<p class=nopadding> (cons a (cons 4 (cons 'five '(6))))))</p>
|
|
<p></tt>
|
|
However, it is not equivalent to this expression:
|
|
</p>
|
|
|
|
<tt>(let ((a 3)) (list (list 1 2) a 4 'five 6))
|
|
<p class=nopadding></p>
|
|
<p></tt>
|
|
It is a syntax violation if any of the identifiers
|
|
<tt>quasiquote</tt>, <tt>unquote</tt>, or <tt>unquote-splicing</tt> appear in
|
|
positions within a <qq template> otherwise than as described above.</p>
|
|
<p>
|
|
The following grammar for quasiquote expressions is not context-free.
|
|
It is presented as a recipe for generating an infinite number of
|
|
production rules. Imagine a copy of the following rules for <em>D</em> = 1, 2,
|
|
3, <tt>...</tt>. <em>D</em> keeps track of the nesting depth.</p>
|
|
<p>
|
|
</p>
|
|
<tt><qq template> → <qq template 1>
|
|
<p class=nopadding><qq template 0> → <expression></p>
|
|
|
|
<p class=nopadding><quasiquotation <em>D</em>> → (quasiquote <qq template <em>D</em>>)</p>
|
|
|
|
<p class=nopadding><qq template <em>D</em>> → <lexeme datum></p>
|
|
|
|
<p class=nopadding><span style="margin-left: 2em">‌</span> ∣ <list qq template <em>D</em>></p>
|
|
|
|
<p class=nopadding> ∣ <vector qq template <em>D</em>></p>
|
|
|
|
<p class=nopadding> ∣ <unquotation <em>D</em>></p>
|
|
|
|
<p class=nopadding><list qq template <em>D</em>> → (<qq template or splice <em>D</em>>*)</p>
|
|
|
|
<p class=nopadding> ∣ (<qq template or splice <em>D</em>><sup>+</sup> . <qq template <em>D</em>>)</p>
|
|
|
|
<p class=nopadding> ∣ <quasiquotation <em>D</em> + 1></p>
|
|
|
|
<p class=nopadding><vector qq template <em>D</em>> → #(<qq template or splice <em>D</em>>*)</p>
|
|
|
|
<p class=nopadding><unquotation <em>D</em>> → (unquote <qq template <em>D</em>−1>)</p>
|
|
|
|
<p class=nopadding><qq template or splice <em>D</em>> → <qq template <em>D</em>></p>
|
|
|
|
<p class=nopadding> ∣ <splicing unquotation <em>D</em>></p>
|
|
|
|
<p class=nopadding><splicing unquotation <em>D</em>> →</p>
|
|
|
|
<p class=nopadding> (unquote-splicing <qq template <em>D</em>−1>*)</p>
|
|
|
|
<p class=nopadding> ∣ (unquote <qq template <em>D</em>−1>*) </p>
|
|
</tt><p>
|
|
In <quasiquotation>s, a <list qq template <em>D</em>> can sometimes
|
|
be confused with either an <unquotation <em>D</em>> or a <splicing
|
|
unquotation <em>D</em>>. The interpretation as an
|
|
<unquotation> or <splicing
|
|
unquotation <em>D</em>> takes precedence.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.18"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.18">11.18 Binding constructs for syntactic keywords</a></h2>
|
|
<p></p>
|
|
<p>
|
|
The <tt>let-syntax</tt> and <tt>letrec-syntax</tt> forms
|
|
bind keywords.
|
|
Like a <tt>begin</tt> form, a <tt>let-syntax</tt> or <tt>letrec-syntax</tt> form
|
|
may appear in a definition context, in which case it is treated as a
|
|
definition, and the forms in the body must also be
|
|
definitions.
|
|
A <tt>let-syntax</tt> or <tt>letrec-syntax</tt> form may also appear in an
|
|
expression context, in which case the forms within their bodies must be
|
|
expressions.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_776"></a>let-syntax<i> <bindings> <form> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
<em>Syntax: </em><Bindings> must have the form
|
|
</p>
|
|
|
|
<tt>((<keyword> <expression>) <tt>...</tt>)<p></tt>
|
|
Each <keyword> is an identifier,
|
|
and each <expression> is
|
|
an expression that evaluates, at macro-expansion
|
|
time, to a <i>transformer</i><a name="node_idx_778"></a><a name="node_idx_780"></a>.
|
|
Transformers may be created by <tt>syntax-rules</tt> or <tt>identifier-syntax</tt>
|
|
(see section <a href="r6rs-Z-H-14.html#node_sec_11.19">11.19</a>) or by one of the other mechanisms
|
|
described in library chapter on “<tt>syntax-case</tt>”. It is a
|
|
syntax violation for <keyword> to appear more than once in the list of keywords
|
|
being bound.</p>
|
|
<p>
|
|
<em>Semantics: </em>The <form>s are expanded in the syntactic environment
|
|
obtained by extending the syntactic environment of the
|
|
<tt>let-syntax</tt> form with macros whose keywords are
|
|
the <keyword>s, bound to the specified transformers.
|
|
Each binding of a <keyword> has the <form>s as its region.</p>
|
|
<p>
|
|
The <form>s of a <tt>let-syntax</tt>
|
|
form are treated, whether in definition or expression context, as if
|
|
wrapped in an implicit <tt>begin</tt>; see section <a href="r6rs-Z-H-14.html#node_sec_11.4.7">11.4.7</a>.
|
|
Thus definitions in the result of expanding the <form>s have
|
|
the same region as any definition appearing in place of the <tt>let-syntax</tt> form would have.</p>
|
|
<p>
|
|
<em>Implementation responsibilities: </em>The implementation should detect if the value of
|
|
<expression> cannot possibly be a transformer.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let-syntax ((when (syntax-rules ()
|
|
<p class=nopadding> ((when test stmt1 stmt2 ...)</p>
|
|
|
|
<p class=nopadding> (if test</p>
|
|
|
|
<p class=nopadding> (begin stmt1</p>
|
|
|
|
<p class=nopadding> stmt2 ...))))))</p>
|
|
|
|
<p class=nopadding> (let ((if <tt>#t</tt>))</p>
|
|
|
|
<p class=nopadding> (when if (set! if 'now))</p>
|
|
|
|
<p class=nopadding> if)) <span style="margin-left: 2em">‌</span>⇒ now</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(let ((x 'outer))</p>
|
|
|
|
<p class=nopadding> (let-syntax ((m (syntax-rules () ((m) x))))</p>
|
|
|
|
<p class=nopadding> (let ((x 'inner))</p>
|
|
|
|
<p class=nopadding> (m)))) <span style="margin-left: 2em">‌</span>⇒ outer</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(let ()</p>
|
|
|
|
<p class=nopadding> (let-syntax</p>
|
|
|
|
<p class=nopadding> ((def (syntax-rules ()</p>
|
|
|
|
<p class=nopadding> ((def stuff ...) (define stuff ...)))))</p>
|
|
|
|
<p class=nopadding> (def foo 42))</p>
|
|
|
|
<p class=nopadding> foo) <span style="margin-left: 2em">‌</span>⇒ 42</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(let ()</p>
|
|
|
|
<p class=nopadding> (let-syntax ())</p>
|
|
|
|
<p class=nopadding> 5) <span style="margin-left: 2em">‌</span>⇒ 5</p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_782"></a>letrec-syntax<i> <bindings> <form> <tt>...</tt></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax </div>
|
|
<p>
|
|
<em>Syntax: </em>Same as for <tt>let-syntax</tt>.</p>
|
|
<p>
|
|
<em>Semantics: </em>The <form>s are
|
|
expanded in the syntactic environment obtained by
|
|
extending the syntactic environment of the <tt>letrec-syntax</tt>
|
|
form with macros whose keywords are the
|
|
<keyword>s, bound to the specified transformers.
|
|
Each binding of a <keyword> has the <bindings>
|
|
as well as the <form>s within its region,
|
|
so the transformers can
|
|
transcribe forms into uses of the macros
|
|
introduced by the <tt>letrec-syntax</tt> form.</p>
|
|
<p>
|
|
The <form>s of a <tt>letrec-syntax</tt>
|
|
form are treated, whether in definition or expression context, as if
|
|
wrapped in an implicit <tt>begin</tt>; see section <a href="r6rs-Z-H-14.html#node_sec_11.4.7">11.4.7</a>.
|
|
Thus definitions in the result of expanding the <form>s have
|
|
the same region as any definition appearing in place of the <tt>letrec-syntax</tt> form would have.</p>
|
|
<p>
|
|
<em>Implementation responsibilities: </em>The implementation should detect if the value of
|
|
<expression> cannot possibly be a transformer.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(letrec-syntax
|
|
<p class=nopadding> ((my-or (syntax-rules ()</p>
|
|
|
|
<p class=nopadding> ((my-or) <tt>#f</tt>)</p>
|
|
|
|
<p class=nopadding> ((my-or e) e)</p>
|
|
|
|
<p class=nopadding> ((my-or e1 e2 ...)</p>
|
|
|
|
<p class=nopadding> (let ((temp e1))</p>
|
|
|
|
<p class=nopadding> (if temp</p>
|
|
|
|
<p class=nopadding> temp</p>
|
|
|
|
<p class=nopadding> (my-or e2 ...)))))))</p>
|
|
|
|
<p class=nopadding> (let ((x <tt>#f</tt>)</p>
|
|
|
|
<p class=nopadding> (y 7)</p>
|
|
|
|
<p class=nopadding> (temp 8)</p>
|
|
|
|
<p class=nopadding> (let odd?)</p>
|
|
|
|
<p class=nopadding> (if even?))</p>
|
|
|
|
<p class=nopadding> (my-or x</p>
|
|
|
|
<p class=nopadding> (let temp)</p>
|
|
|
|
<p class=nopadding> (if y)</p>
|
|
|
|
<p class=nopadding> y))) <span style="margin-left: 2em">‌</span>⇒ 7</p>
|
|
<p></tt></p>
|
|
<p>
|
|
The following example highlights how <tt>let-syntax</tt>
|
|
and <tt>letrec-syntax</tt> differ.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let ((f (lambda (x) (+ x 1))))
|
|
<p class=nopadding> (let-syntax ((f (syntax-rules ()</p>
|
|
|
|
<p class=nopadding> ((f x) x)))</p>
|
|
|
|
<p class=nopadding> (g (syntax-rules ()</p>
|
|
|
|
<p class=nopadding> ((g x) (f x)))))</p>
|
|
|
|
<p class=nopadding> (list (f 1) (g 1)))) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ (1 2)</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(let ((f (lambda (x) (+ x 1))))</p>
|
|
|
|
<p class=nopadding> (letrec-syntax ((f (syntax-rules ()</p>
|
|
|
|
<p class=nopadding> ((f x) x)))</p>
|
|
|
|
<p class=nopadding> (g (syntax-rules ()</p>
|
|
|
|
<p class=nopadding> ((g x) (f x)))))</p>
|
|
|
|
<p class=nopadding> (list (f 1) (g 1)))) <br><span style="margin-left: 2em">‌</span><span style="margin-left: 2em">‌</span>⇒ (1 1)</p>
|
|
<p></tt></p>
|
|
<p>
|
|
The two expressions are identical except that the <tt>let-syntax</tt> form
|
|
in the first expression is a <tt>letrec-syntax</tt> form in the second.
|
|
In the first expression, the <tt>f</tt> occurring in <tt>g</tt> refers to
|
|
the <tt>let</tt>-bound variable <tt>f</tt>, whereas in the second it refers
|
|
to the keyword <tt>f</tt> whose binding is established by the
|
|
<tt>letrec-syntax</tt> form.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.19"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.19">11.19 Macro transformers</a></h2>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(syntax-rules (<literal> <tt>...</tt>) <syntax rule> <tt>...</tt>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax (<tt>expand</tt>) </div>
|
|
|
|
<div align=left><tt>_</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>auxiliary syntax (<tt>expand)</tt> </div>
|
|
|
|
<a name="node_idx_784"></a><div align=left><tt>...</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>auxiliary syntax (<tt>expand)</tt> </div>
|
|
|
|
<a name="node_idx_786"></a><a name="node_idx_788"></a><p>
|
|
<em>Syntax: </em>Each <literal> must be an identifier.
|
|
Each <syntax rule> must have the following form:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(<srpattern> <template>)<p></tt></p>
|
|
<p>
|
|
An <srpattern> is a restricted form of <pattern>,
|
|
namely, a nonempty <pattern> in one of four parenthesized forms below
|
|
whose first subform is an identifier or an underscore <tt>_</tt><a name="node_idx_790"></a>.
|
|
A <pattern> is an identifier, constant, or one of the following.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(<pattern> <tt>...</tt>)
|
|
<p class=nopadding>(<pattern> <pattern> <tt>...</tt> . <pattern>)</p>
|
|
|
|
<p class=nopadding>(<pattern> <tt>...</tt> <pattern> <ellipsis> <pattern> <tt>...</tt>)</p>
|
|
|
|
<p class=nopadding>(<pattern> <tt>...</tt> <pattern> <ellipsis> <pattern> <tt>...</tt> . <pattern>)</p>
|
|
|
|
<p class=nopadding>#(<pattern> <tt>...</tt>)</p>
|
|
|
|
<p class=nopadding>#(<pattern> <tt>...</tt> <pattern> <ellipsis> <pattern> <tt>...</tt>)</p>
|
|
<p></tt></p>
|
|
<p>
|
|
An <ellipsis> is the identifier “<tt>...</tt>” (three periods).<a name="node_idx_792"></a></p>
|
|
<p>
|
|
A <template> is a pattern variable, an identifier that
|
|
is not a pattern
|
|
variable, a pattern datum, or one of the following.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(<subtemplate> <tt>...</tt>)
|
|
<p class=nopadding>(<subtemplate> <tt>...</tt> . <template>)</p>
|
|
|
|
<p class=nopadding>#(<subtemplate> <tt>...</tt>)</p>
|
|
<p></tt></p>
|
|
<p>
|
|
A <subtemplate> is a <template> followed by zero or more ellipses.</p>
|
|
<p>
|
|
<em>Semantics: </em>An instance of <tt>syntax-rules</tt> evaluates, at
|
|
macro-expansion time, to a new macro
|
|
transformer by specifying a sequence of hygienic rewrite rules. A use
|
|
of a macro whose keyword is associated with a transformer specified by
|
|
<tt>syntax-rules</tt> is matched against the patterns contained in the
|
|
<syntax rule>s, beginning with the leftmost <syntax rule>.
|
|
When a match is found, the macro use is transcribed hygienically
|
|
according to the template. It is a syntax violation when no match is found.</p>
|
|
<p>
|
|
An identifier appearing within a <pattern> may be an underscore
|
|
( <tt>_</tt> ), a literal identifier listed in the list of literals
|
|
<tt>(<literal> <tt>...</tt>)</tt>, or an ellipsis ( <tt>...</tt> ).
|
|
All other identifiers appearing within a <pattern> are
|
|
<i>pattern variables<a name="node_idx_794"></a></i>.
|
|
It is a syntax violation if an ellipsis or underscore appears in <tt>(<literal> <tt>...</tt>)</tt>.</p>
|
|
<p>
|
|
While the first subform of <srpattern> may be an identifier, the
|
|
identifier is not involved in the matching and
|
|
is not considered a pattern variable or literal identifier.</p>
|
|
<p>
|
|
Pattern variables match arbitrary input subforms and
|
|
are used to refer to elements of the input.
|
|
It is a syntax violation if the same pattern variable appears more than once in a
|
|
<pattern>.</p>
|
|
<p>
|
|
Underscores also match arbitrary input subforms but are not pattern variables
|
|
and so cannot be used to refer to those elements.
|
|
Multiple underscores may appear in a <pattern>.</p>
|
|
<p>
|
|
A literal identifier matches an input subform if and only if the input
|
|
subform is an identifier and either both its occurrence in the input
|
|
expression and its occurrence in the list of literals have the same
|
|
lexical binding, or the two identifiers have the same name and both have
|
|
no lexical binding.</p>
|
|
<p>
|
|
A subpattern followed by an ellipsis can match zero or more elements of
|
|
the input.</p>
|
|
<p>
|
|
More formally, an input form <em>F</em> matches a pattern <em>P</em> if and only if
|
|
one of the following holds:</p>
|
|
<p>
|
|
</p>
|
|
<ul>
|
|
<li><p><em>P</em> is an underscore ( <tt>_</tt> ).</p>
|
|
<p>
|
|
</p>
|
|
<li><p><em>P</em> is a pattern variable.</p>
|
|
<p>
|
|
</p>
|
|
<li><p><em>P</em> is a literal identifier
|
|
and <em>F</em> is an identifier such that both <em>P</em> and <em>F</em> would refer to the
|
|
same binding if both were to appear in the output of the macro outside
|
|
of any bindings inserted into the output of the macro.
|
|
(If neither of two like-named identifiers refers to any binding, i.e., both
|
|
are undefined, they are considered to refer to the same binding.)</p>
|
|
<p>
|
|
</p>
|
|
<li><p><em>P</em> is of the form
|
|
<tt>(<em>P</em><sub>1</sub> <tt>...</tt> <em>P</em><sub><em>n</em></sub>)</tt>
|
|
and <em>F</em> is a list of <em>n</em> elements that match <em>P</em><sub>1</sub> through
|
|
<em>P</em><sub><em>n</em></sub>.</p>
|
|
<p>
|
|
</p>
|
|
<li><p><em>P</em> is of the form
|
|
<tt>(<em>P</em><sub>1</sub> <tt>...</tt> <em>P</em><sub><em>n</em></sub> . <em>P</em><sub><em>x</em></sub>)</tt>
|
|
and <em>F</em> is a list or improper list of <em>n</em> or more elements
|
|
whose first <em>n</em> elements match <em>P</em><sub>1</sub> through <em>P</em><sub><em>n</em></sub>
|
|
and
|
|
whose <em>n</em>th cdr matches <em>P</em><sub><em>x</em></sub>.</p>
|
|
<p>
|
|
</p>
|
|
<li><p><em>P</em> is of the form
|
|
<tt>(<em>P</em><sub>1</sub> <tt>...</tt> <em>P</em><sub><em>k</em></sub> <em>P</em><sub><em>e</em></sub> <ellipsis> <em>P</em><sub><em>m</em>+1</sub> <tt>...</tt> <em>P</em><sub><em>n</em></sub>)</tt>,
|
|
where <ellipsis> is the identifier <tt>...</tt>
|
|
and <em>F</em> is a list of <em>n</em>
|
|
elements whose first <em>k</em> elements match <em>P</em><sub>1</sub> through <em>P</em><sub><em>k</em></sub>,
|
|
whose next <em>m</em>−<em>k</em> elements each match <em>P</em><sub><em>e</em></sub>,
|
|
and
|
|
whose remaining <em>n</em>−<em>m</em> elements match <em>P</em><sub><em>m</em>+1</sub> through <em>P</em><sub><em>n</em></sub>.</p>
|
|
<p>
|
|
</p>
|
|
<li><p><em>P</em> is of the form
|
|
<tt>(<em>P</em><sub>1</sub> <tt>...</tt> <em>P</em><sub><em>k</em></sub> <em>P</em><sub><em>e</em></sub> <ellipsis> <em>P</em><sub><em>m</em>+1</sub> <tt>...</tt> <em>P</em><sub><em>n</em></sub> . <em>P</em><sub><em>x</em></sub>)</tt>,
|
|
where <ellipsis> is the identifier <tt>...</tt>
|
|
and <em>F</em> is a list or improper list of <em>n</em>
|
|
elements whose first <em>k</em> elements match <em>P</em><sub>1</sub> through <em>P</em><sub><em>k</em></sub>,
|
|
whose next <em>m</em>−<em>k</em> elements each match <em>P</em><sub><em>e</em></sub>,
|
|
whose next <em>n</em>−<em>m</em> elements match <em>P</em><sub><em>m</em>+1</sub> through <em>P</em><sub><em>n</em></sub>,
|
|
and
|
|
whose <em>n</em>th and final cdr matches <em>P</em><sub><em>x</em></sub>.</p>
|
|
<p>
|
|
</p>
|
|
<li><p><em>P</em> is of the form
|
|
<tt>#(<em>P</em><sub>1</sub> <tt>...</tt> <em>P</em><sub><em>n</em></sub>)</tt>
|
|
and <em>F</em> is a vector of <em>n</em> elements that match <em>P</em><sub>1</sub> through
|
|
<em>P</em><sub><em>n</em></sub>.</p>
|
|
<p>
|
|
</p>
|
|
<li><p><em>P</em> is of the form
|
|
<tt>#(<em>P</em><sub>1</sub> <tt>...</tt> <em>P</em><sub><em>k</em></sub> <em>P</em><sub><em>e</em></sub> <ellipsis> <em>P</em><sub><em>m</em>+1</sub> <tt>...</tt> <em>P</em><sub><em>n</em></sub>)</tt>,
|
|
where <ellipsis> is the identifier <tt>...</tt>
|
|
and <em>F</em> is a vector of <em>n</em> or more elements
|
|
whose first <em>k</em> elements match <em>P</em><sub>1</sub> through <em>P</em><sub><em>k</em></sub>,
|
|
whose next <em>m</em>−<em>k</em> elements each match <em>P</em><sub><em>e</em></sub>,
|
|
and
|
|
whose remaining <em>n</em>−<em>m</em> elements match <em>P</em><sub><em>m</em>+1</sub> through <em>P</em><sub><em>n</em></sub>.</p>
|
|
<p>
|
|
</p>
|
|
<li><p><em>P</em> is a pattern datum (any nonlist, nonvector, nonsymbol
|
|
datum) and <em>F</em> is equal to <em>P</em> in the sense of the
|
|
<tt>equal?</tt> procedure.
|
|
</p>
|
|
</ul><p></p>
|
|
<p>
|
|
When a macro use is transcribed according to the template of the
|
|
matching <syntax rule>, pattern variables that occur in the
|
|
template are replaced by the subforms they match in the input.</p>
|
|
<p>
|
|
Pattern data and identifiers that are not pattern variables
|
|
or ellipses are copied into the output.
|
|
A subtemplate followed by an ellipsis expands
|
|
into zero or more occurrences of the subtemplate.
|
|
Pattern variables that occur in subpatterns followed by one or more
|
|
ellipses may occur only in subtemplates that are
|
|
followed by (at least) as many ellipses.
|
|
These pattern variables are replaced in the output by the input
|
|
subforms to which they are bound, distributed as specified.
|
|
If a pattern variable is followed by more ellipses in the subtemplate
|
|
than in the associated subpattern, the input form is replicated as
|
|
necessary.
|
|
The subtemplate must contain at least one pattern variable from a
|
|
subpattern followed by an ellipsis, and for at least one such pattern
|
|
variable, the subtemplate must be followed by exactly as many ellipses as
|
|
the subpattern in which the pattern variable appears.
|
|
(Otherwise, the expander would not be able to determine how many times the
|
|
subform should be repeated in the output.)
|
|
It is a syntax violation if the constraints of this paragraph are not met.</p>
|
|
<p>
|
|
A template of the form
|
|
<tt>(<ellipsis> <template>)</tt> is identical to <template>, except that
|
|
ellipses within the template have no special meaning.
|
|
That is, any ellipses contained within <template> are
|
|
treated as ordinary identifiers.
|
|
In particular, the template <tt>(... ...)</tt> produces a single
|
|
ellipsis, <tt>...</tt>.
|
|
This allows syntactic abstractions to expand into forms containing
|
|
ellipses.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(define-syntax be-like-begin
|
|
<p class=nopadding> (syntax-rules ()</p>
|
|
|
|
<p class=nopadding> ((be-like-begin name)</p>
|
|
|
|
<p class=nopadding> (define-syntax name</p>
|
|
|
|
<p class=nopadding> (syntax-rules ()</p>
|
|
|
|
<p class=nopadding> ((name expr (... ...))</p>
|
|
|
|
<p class=nopadding> (begin expr (... ...))))))))</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(be-like-begin sequence)</p>
|
|
|
|
<p class=nopadding>(sequence 1 2 3 4) <span style="margin-left: 2em">‌</span>⇒ 4</p>
|
|
<p></tt></p>
|
|
<p>
|
|
As an example for hygienic use of auxiliary identifier,
|
|
if <tt>let</tt> and <tt>cond</tt> are defined as in
|
|
section <a href="r6rs-Z-H-14.html#node_sec_11.16">11.16</a> and appendix <a href="r6rs-Z-H-16.html#node_chap_B">B</a> then they
|
|
are hygienic (as required) and the following is not an error.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let ((=> <tt>#f</tt>))
|
|
<p class=nopadding> (cond (<tt>#t</tt> => 'ok))) <span style="margin-left: 2em">‌</span>⇒ ok</p>
|
|
<p></tt></p>
|
|
<p>
|
|
The macro transformer for <tt>cond</tt> recognizes <tt>=></tt>
|
|
as a local variable, and hence an expression, and not as the
|
|
identifier <tt>=></tt>, which the macro transformer treats
|
|
as a syntactic keyword. Thus the example expands into</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let ((=> <tt>#f</tt>))
|
|
<p class=nopadding> (if <tt>#t</tt> (begin => 'ok)))</p>
|
|
<p></tt></p>
|
|
<p>
|
|
instead of</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let ((=> <tt>#f</tt>))
|
|
<p class=nopadding> (let ((temp <tt>#t</tt>))</p>
|
|
|
|
<p class=nopadding> (if temp ('ok temp))))</p>
|
|
<p></tt></p>
|
|
<p>
|
|
which would result in an assertion violation.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_796"></a>identifier-syntax<i> <template></i>)</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax (<tt>expand</tt>) </div>
|
|
|
|
<div align=left><tt>(identifier-syntax</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>syntax (<tt>expand</tt>) </div>
|
|
<br>
|
|
<tt>(<id<sub>1</sub>> <template<sub>1</sub>>)<br>
|
|
|
|
((set! <id<sub>2</sub>> <pattern>)<br>
|
|
|
|
<template<sub>2</sub>>))<br>
|
|
|
|
<div align=left><tt>set!</tt><span style="margin-left: .5em">‌</span><span style="margin-left: .5em">‌</span>auxiliary syntax (<tt> expand)</tt> </div>
|
|
</tt><p>
|
|
<em>Syntax: </em>The <id>s must be identifiers. The <template>s
|
|
must be as for <tt>syntax-rules</tt>.</p>
|
|
<p>
|
|
<em>Semantics: </em>When a keyword is bound to a transformer produced by the first form of
|
|
<tt>identifier-syntax</tt>, references to the keyword within the scope
|
|
of the binding are replaced by <template>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(define p (cons 4 5))
|
|
<p class=nopadding>(define-syntax p.car (identifier-syntax (car p)))</p>
|
|
|
|
<p class=nopadding>p.car <span style="margin-left: 2em">‌</span>⇒ 4</p>
|
|
|
|
<p class=nopadding>(set! p.car 15) <span style="margin-left: 2em">‌</span>⇒ <tt> &syntax</tt> <i>exception</i></p>
|
|
<p></tt></p>
|
|
<p>
|
|
The second, more general, form of <tt>identifier-syntax</tt> permits
|
|
the transformer to determine what happens when <tt>set!</tt> is used.
|
|
In this case, uses of the identifier by itself are replaced by
|
|
<template<sub>1</sub>>, and uses of <tt>set!</tt> with the identifier are
|
|
replaced by <template<sub>2</sub>>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(define p (cons 4 5))
|
|
<p class=nopadding>(define-syntax p.car</p>
|
|
|
|
<p class=nopadding> (identifier-syntax</p>
|
|
|
|
<p class=nopadding> (_ (car p))</p>
|
|
|
|
<p class=nopadding> ((set! _ e) (set-car! p e))))</p>
|
|
|
|
<p class=nopadding>(set! p.car 15)</p>
|
|
|
|
<p class=nopadding>p.car <span style="margin-left: 2em">‌</span>⇒ 15</p>
|
|
|
|
<p class=nopadding>p <span style="margin-left: 2em">‌</span>⇒ (15 5)</p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<a name="node_sec_11.20"></a>
|
|
<h2 class=section><a href="r6rs-Z-H-2.html#node_toc_node_sec_11.20">11.20 Tail calls and tail contexts</a></h2>
|
|
<p></p>
|
|
<p>
|
|
A <em>tail call</em><a name="node_idx_798"></a>is a procedure call that occurs
|
|
in a <em>tail context</em>. Tail contexts are defined inductively. Note
|
|
that a tail context is always determined with respect to a particular lambda
|
|
expression.</p>
|
|
<p>
|
|
</p>
|
|
<ul>
|
|
<li><p>The last expression within the body of a lambda expression,
|
|
shown as <tail expression> below, occurs in a tail context.
|
|
</p>
|
|
|
|
<tt>(lāmbda <formals>
|
|
<p class=nopadding> <definition>* </p>
|
|
|
|
<p class=nopadding> <expression>* <tail expression>)</p>
|
|
<p></tt>
|
|
</p>
|
|
<li><p>If one of the following expressions is in a tail context,
|
|
then the subexpressions shown as <tail expression> are in a tail context.
|
|
These were derived from specifications of the syntax of the forms described in
|
|
this chapter by replacing some occurrences of <expression>
|
|
with <tail expression>. Only those rules that contain tail contexts
|
|
are shown here.
|
|
</p>
|
|
|
|
<tt>(if <expression> <tail expression> <tail expression>)
|
|
<p class=nopadding>(if <expression> <tail expression>)</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(cond <cond clause><sup>+</sup>)</p>
|
|
|
|
<p class=nopadding>(cond <cond clause>* (else <tail sequence>))</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(cāse <expression></p>
|
|
|
|
<p class=nopadding> <case clause><sup>+</sup>)</p>
|
|
|
|
<p class=nopadding>(cāse <expression></p>
|
|
|
|
<p class=nopadding> <case clause>*</p>
|
|
|
|
<p class=nopadding> (else <tail sequence>))</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(and <expression>* <tail expression>)</p>
|
|
|
|
<p class=nopadding>(or <expression>* <tail expression>)</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(let <bindings> <tail body>)</p>
|
|
|
|
<p class=nopadding>(let <variable> <bindings> <tail body>)</p>
|
|
|
|
<p class=nopadding>(let* <bindings> <tail body>)</p>
|
|
|
|
<p class=nopadding>(letrec* <bindings> <tail body>)</p>
|
|
|
|
<p class=nopadding>(letrec <bindings> <tail body>)</p>
|
|
|
|
<p class=nopadding>(let-values <mv-bindings> <tail body>)</p>
|
|
|
|
<p class=nopadding>(let*-values <mv-bindings> <tail body>)</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(let-syntax <bindings> <tail body>)</p>
|
|
|
|
<p class=nopadding>(letrec-syntax <bindings> <tail body>)</p>
|
|
|
|
<p class=nopadding></p>
|
|
|
|
<p class=nopadding>(begin <tail sequence>)</p>
|
|
<p></tt>
|
|
A <cond clause> is
|
|
</p>
|
|
|
|
<tt>(<test> <tail sequence>),<p></tt>
|
|
a <case clause> is
|
|
</p>
|
|
|
|
<tt>((<datum>*) <tail sequence>),<p></tt>
|
|
a <tail body> is
|
|
</p>
|
|
|
|
<tt><definition>* <tail sequence>,<p></tt>
|
|
and a <tail sequence> is
|
|
</p>
|
|
|
|
<tt><expression>* <tail expression>.<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<li><p>If a <tt>cond</tt> expression is in a tail context, and has a clause of
|
|
the form <tt>(<expression<sub>1</sub>> => <expression<sub>2</sub>>)</tt>
|
|
then the (implied) call to
|
|
the procedure that results from the evaluation of <expression<sub>2</sub>> is in a
|
|
tail context. <Expression<sub>2</sub>> itself is not in a tail context.</p>
|
|
<p>
|
|
</p>
|
|
</ul><p></p>
|
|
<p>
|
|
Certain built-in procedures must also perform tail calls.
|
|
The first argument passed to <tt>apply</tt> and to
|
|
<tt>call-with-current-continuation</tt>, and the second argument passed to
|
|
<tt>call-with-values</tt>, must be called via a tail call.</p>
|
|
<p>
|
|
In the following example the only tail call is the call to <tt>f</tt>.
|
|
None of the calls to <tt>g</tt> or <tt>h</tt> are tail calls. The reference to
|
|
<tt>x</tt> is in a tail context, but it is not a call and thus is not a
|
|
tail call.
|
|
</p>
|
|
|
|
<tt>(lambda ()
|
|
<p class=nopadding> (if (g)</p>
|
|
|
|
<p class=nopadding> (let ((x (h)))</p>
|
|
|
|
<p class=nopadding> x)</p>
|
|
|
|
<p class=nopadding> (and (g) (f))))</p>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note:<span style="margin-left: .5em">‌</span></em>
|
|
Implementations may
|
|
recognize that some non-tail calls, such as the call to <tt>h</tt>
|
|
above, can be evaluated as though they were tail calls.
|
|
In the example above, the <tt>let</tt> expression could be compiled
|
|
as a tail call to <tt>h</tt>. (The possibility of <tt>h</tt> returning
|
|
an unexpected number of values can be ignored, because in that
|
|
case the effect of the <tt>let</tt> is explicitly unspecified and
|
|
implementation-dependent.)
|
|
</blockquote><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.html">first</a>, <a href="r6rs-Z-H-13.html">previous</a></span><span>, <a href="r6rs-Z-H-15.html">next</a></span> page<span>; </span><span><a href="r6rs-Z-H-2.html#node_toc_start">contents</a></span><span><span>; </span><a href="r6rs-Z-H-21.html#node_index_start">index</a></span>]</div>
|
|
</p>
|
|
<p></p>
|
|
</div>
|
|
</body>
|
|
</html>
|