Sophie

Sophie

distrib > Fedora > 13 > i386 > media > os > by-pkgid > 52a37fb77746ef557a2ec666070d732e > files > 63

bigloo-doc-3.2b-3.fc12.i686.rpm

<!-- 95% W3C COMPLIANT, 95% CSS FREE, RAW HTML -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<title>R5RsScheme Revised(5) Report on the Algorithmic Language Scheme</title>
 <style type="text/css">
  <!--
  pre { font-family: monospace }
  tt { font-family: monospace }
  code { font-family: monospace }
  p.flushright { text-align: right }
  p.flushleft { text-align: left }
  span.sc { font-variant: small-caps }
  span.sf { font-family: sans-serif }
  span.skribetitle { font-family: sans-serif; font-weight: bolder; font-size: x-large; }
  span.refscreen { }
  span.refprint { display: none; }
  -->
 </style>
</head>

<body class="chapter" bgcolor="#ffffff">
<table width="100%" class="skribetitle" cellspacing="0" cellpadding="0"><tbody>
<tr><td align="center" bgcolor="#8381de"><div class="skribetitle"><strong><big><big><big>4. R5Rs<br/>Scheme Revised(5) Report on the Algorithmic Language Scheme -- Expressions</big></big></big></strong></div><center>
</center>
</td></tr></tbody></table>
<table cellpadding="3" cellspacing="0" width="100%" class="skribe-margins"><tr>
<td align="left" valign="top" class="skribe-left-margin" width="20%" bgcolor="#dedeff"><div class="skribe-left-margin">
<br/><center id='center7666'
><table width="97%" border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse;" frame="box" rules="none"><tbody>
<tr bgcolor="#8381de"><th id="tc7656" align="center" colspan="1"><font color="#ffffff"><strong id='bold7654'
>main page</strong></font></th></tr>
<tr bgcolor="#ffffff"><td id="tc7663" align="center" colspan="1"><table width="100%" border="0" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc7659" align="left" valign="top" colspan="1"><strong id='bold7658'
>top:</strong></td><td id="tc7660" align="right" valign="top" colspan="1"><a href="r5rs.html#R5Rs-Scheme-Revised(5)-Report-on-the-Algorithmic-Language-Scheme" class="inbound">R5Rs<br/>Scheme Revised(5) Report on the Algorithmic Language Scheme</a></td></tr>
</tbody></table>
</td></tr>
</tbody></table>
</center>
<br/><br/><center id='center7676'
><table width="97%" border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse;" frame="box" rules="none"><tbody>
<tr bgcolor="#8381de"><th id="tc7670" align="center" colspan="1"><font color="#ffffff"><strong id='bold7668'
>Expressions</strong></font></th></tr>
<tr bgcolor="#ffffff"><td id="tc7673" align="center" colspan="1"><table cellspacing="1" cellpadding="1" width="100%" class="toc">
<tbody>
 <tr><td valign="top" align="left">4.1</td><td colspan="4" width="100%"><a href="r5rs-7.html#Primitive-expression-types">Primitive expression types</a></td></tr>
 <tr><td></td><td valign="top" align="left">4.1.1</td><td colspan="3" width="100%"><a href="r5rs-7.html#Variable-references">Variable references</a></td></tr>
 <tr><td></td><td valign="top" align="left">4.1.2</td><td colspan="3" width="100%"><a href="r5rs-7.html#Literal-expressions">Literal expressions</a></td></tr>
 <tr><td></td><td valign="top" align="left">4.1.3</td><td colspan="3" width="100%"><a href="r5rs-7.html#Procedure-calls">Procedure calls</a></td></tr>
 <tr><td></td><td valign="top" align="left">4.1.4</td><td colspan="3" width="100%"><a href="r5rs-7.html#Procedures">Procedures</a></td></tr>
 <tr><td></td><td valign="top" align="left">4.1.5</td><td colspan="3" width="100%"><a href="r5rs-7.html#Conditionals">Conditionals</a></td></tr>
 <tr><td></td><td valign="top" align="left">4.1.6</td><td colspan="3" width="100%"><a href="r5rs-7.html#Assignments">Assignments</a></td></tr>
 <tr><td valign="top" align="left">4.2</td><td colspan="4" width="100%"><a href="r5rs-7.html#Derived-expression-types">Derived expression types</a></td></tr>
 <tr><td></td><td valign="top" align="left">4.2.1</td><td colspan="3" width="100%"><a href="r5rs-7.html#Conditionals">Conditionals</a></td></tr>
 <tr><td></td><td valign="top" align="left">4.2.2</td><td colspan="3" width="100%"><a href="r5rs-7.html#Binding-constructs">Binding constructs</a></td></tr>
 <tr><td></td><td valign="top" align="left">4.2.3</td><td colspan="3" width="100%"><a href="r5rs-7.html#Sequencing">Sequencing</a></td></tr>
 <tr><td></td><td valign="top" align="left">4.2.4</td><td colspan="3" width="100%"><a href="r5rs-7.html#Iteration">Iteration</a></td></tr>
 <tr><td></td><td valign="top" align="left">4.2.5</td><td colspan="3" width="100%"><a href="r5rs-7.html#Delayed-evaluation">Delayed evaluation</a></td></tr>
 <tr><td></td><td valign="top" align="left">4.2.6</td><td colspan="3" width="100%"><a href="r5rs-7.html#Quasiquotation">Quasiquotation</a></td></tr>
 <tr><td valign="top" align="left">4.3</td><td colspan="4" width="100%"><a href="r5rs-7.html#Macros">Macros</a></td></tr>
 <tr><td></td><td valign="top" align="left">4.3.1</td><td colspan="3" width="100%"><a href="r5rs-7.html#Binding-constructs-for-syntactic-keywords">Binding constructs for syntactic keywords</a></td></tr>
 <tr><td></td><td valign="top" align="left">4.3.2</td><td colspan="3" width="100%"><a href="r5rs-7.html#Pattern-language">Pattern language</a></td></tr>
</tbody>
</table>
</td></tr>
</tbody></table>
</center>
<br/><br/><center id='center7686'
><table width="97%" border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse;" frame="box" rules="none"><tbody>
<tr bgcolor="#8381de"><th id="tc7680" align="center" colspan="1"><font color="#ffffff"><strong id='bold7678'
>Chapters</strong></font></th></tr>
<tr bgcolor="#ffffff"><td id="tc7683" align="center" colspan="1"><table cellspacing="1" cellpadding="1" width="100%" class="toc">
<tbody>
 <tr><td valign="top" align="left"></td><td colspan="4" width="100%"><a href="r5rs-1.html#Summary">Summary</a></td></tr>
 <tr><td valign="top" align="left"></td><td colspan="4" width="100%"><a href="r5rs-2.html#Introduction">Introduction</a></td></tr>
 <tr><td valign="top" align="left"></td><td colspan="4" width="100%"><a href="r5rs-3.html#Table-of-contents">Table of contents</a></td></tr>
 <tr><td valign="top" align="left">1</td><td colspan="4" width="100%"><a href="r5rs-4.html#Overview-of-Scheme">Overview of Scheme</a></td></tr>
 <tr><td valign="top" align="left">2</td><td colspan="4" width="100%"><a href="r5rs-5.html#Lexical-conventions">Lexical conventions</a></td></tr>
 <tr><td valign="top" align="left">3</td><td colspan="4" width="100%"><a href="r5rs-6.html#Basic-concepts">Basic concepts</a></td></tr>
 <tr><td valign="top" align="left">4</td><td colspan="4" width="100%"><a href="r5rs-7.html#Expressions">Expressions</a></td></tr>
 <tr><td valign="top" align="left">5</td><td colspan="4" width="100%"><a href="r5rs-8.html#Program-structure">Program structure</a></td></tr>
 <tr><td valign="top" align="left">6</td><td colspan="4" width="100%"><a href="r5rs-9.html#Standard-procedures">Standard procedures</a></td></tr>
 <tr><td valign="top" align="left">7</td><td colspan="4" width="100%"><a href="r5rs-10.html#Formal-syntax-and-semantics">Formal syntax and semantics</a></td></tr>
 <tr><td valign="top" align="left">8</td><td colspan="4" width="100%"><a href="r5rs-11.html#Concepts">Concepts</a></td></tr>
 <tr><td valign="top" align="left">9</td><td colspan="4" width="100%"><a href="r5rs-12.html#Variables-and-Procedures">Variables and Procedures</a></td></tr>
 <tr><td valign="top" align="left"></td><td colspan="4" width="100%"><a href="r5rs-13.html#Notes">Notes</a></td></tr>
 <tr><td valign="top" align="left"></td><td colspan="4" width="100%"><a href="r5rs-14.html#Additional-material">Additional material</a></td></tr>
 <tr><td valign="top" align="left"></td><td colspan="4" width="100%"><a href="r5rs-15.html#Example">Example</a></td></tr>
 <tr><td valign="top" align="left"></td><td colspan="4" width="100%"><a href="r5rs-16.html#Bibliography">Bibliography</a></td></tr>
</tbody>
</table>
</td></tr>
</tbody></table>
</center>
</div></td>
<td align="left" valign="top" class="skribe-body"><div class="skribe-body">
<a name="Expressions" class="mark"></a>



Expression types are categorized as <em id='emph1654'
>primitive</em> or <em id='emph1655'
>derived</em>.
Primitive expression types include variables and procedure calls.
Derived expression types are not semantically primitive, but can instead
be defined as macros.
With the exception of <samp id='samp1656'
>quasiquote</samp>, whose macro definition is complex,
the derived expressions are classified as library features.
Suitable definitions are given in section <a href="r5rs-7.html#Derived-expression-types" class="inbound">Derived expression types</a>.<br/><br/><!-- Primitive expression types -->
<a name="Primitive-expression-types"></a>
<div class="section-atitle"><table width="100%"><tr><td bgcolor="#dedeff"><h3><font color="black">4.1 Primitive expression types</font>
</h3></td></tr></table>
</div><div class="section">
<a name="Primitive-expression-types" class="mark"></a>

<!-- Variable references -->
<a name="Variable-references"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">4.1.1 Variable references</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Variable-references" class="mark"></a>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc1659" align="left" colspan="1"><em id='it1658'
>&lt;variable&gt;</em></td><td id="tc1660" align="right" colspan="1">syntax</td></tr>
</tbody></table>
An expression consisting of a variable
<a name="g1664" class="mark"></a>(section <a href="r5rs-6.html#Variables;-syntactic-keywords;-and-regions" class="inbound">Variables; syntactic keywords; and regions</a>) is a variable reference.  The value of
the variable reference is the value stored in the location to which the
variable is bound.  It is an error to reference an
unbound variable.
<a name="g1667" class="mark"></a>
<center id='center1672'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog1670'
>(<font color="#6959cf"><strong id='bold7687'
>define</strong></font> <font color="#6959cf"><strong id='bold7689'
>x</strong></font> 28)
x                                      =&gt;  28
</pre>
</td></tr>
</tbody></table></center>
</td></tr>
</tbody></table><br/>
</div>
<!-- Literal expressions -->
<a name="Literal-expressions"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">4.1.2 Literal expressions</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Literal-expressions" class="mark"></a>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g1676" class="mark"></a><a name="quote" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc1680" align="left" colspan="1"><strong id='bold1678'
>quote</strong><em id='it1679'
>  &lt;datum&gt;</em></td><td id="tc1681" align="right" colspan="1">syntax</td></tr>
</tbody></table>
<table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc1686" align="left" colspan="1"><em id='it1685'
><tt id='tt1684'
>'</tt>&lt;datum&gt;</em></td><td id="tc1687" align="right" colspan="1">syntax</td></tr>
</tbody></table>
<table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc1691" align="left" colspan="1"><em id='it1690'
>&lt;constant&gt;</em></td><td id="tc1692" align="right" colspan="1">syntax</td></tr>
</tbody></table>
<samp id='samp1695'
>(quote &lt;datum&gt;)</samp> evaluates to &lt;datum&gt;.
<a name="g1697" class="mark"></a>&lt;Datum&gt;
may be any external representation of a Scheme object (see
section <a href="r5rs-6.html#External-representations" class="inbound">External representations</a>).  This notation is used to include literal
constants in Scheme code.<br/><br/><center id='center1705'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog1703'
>(<strong id='bold7691'
>quote</strong> a)                              =&gt;  a
(<strong id='bold7692'
>quote</strong> #(a b c))                       =&gt;  #(a b c)
(<strong id='bold7693'
>quote</strong> (+ 1 2))                        =&gt;  (+ 1 2)
</pre>
</td></tr>
</tbody></table></center>

<samp id='samp1706'
>(quote &lt;datum&gt;)</samp> may be abbreviated as
<tt id='tt1707'
>'</tt>&lt;datum&gt;.  The two notations are equivalent in all
respects.<br/><br/><center id='center1714'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog1712'
>'a                                     =&gt;  a
'#(a b c)                              =&gt;  #(a b c)
'()                                    =&gt;  ()
'(+ 1 2)                               =&gt;  (+ 1 2)
'(<strong id='bold7694'
>quote</strong> a)                             =&gt;  (<strong id='bold7695'
>quote</strong> a)
''a                                    =&gt;  (<strong id='bold7696'
>quote</strong> a)
</pre>
</td></tr>
</tbody></table></center>

Numerical constants, string constants, character constants, and boolean
constants evaluate ``to themselves''; they need not be quoted.<br/><br/><center id='center1722'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog1720'
>'<font color="red">&quot;abc&quot;</font>                                 =&gt;  <font color="red">&quot;abc&quot;</font>
<font color="red">&quot;abc&quot;</font>                                  =&gt;  <font color="red">&quot;abc&quot;</font>
'145932                                =&gt;  145932
145932                                 =&gt;  145932
'#t                                    =&gt;  #t
#t                                     =&gt;  #t
</pre>
</td></tr>
</tbody></table></center>

As noted in section <a href="r5rs-6.html#Storage-model" class="inbound">Storage model</a>, it is an error to alter a constant
(i.e. the value of a literal expression) using a mutation procedure like
<samp id='samp1723'
>set-car!</samp> or <samp id='samp1724'
>string-set!</samp>.
</td></tr>
</tbody></table><br/>
</div>
<!-- Procedure calls -->
<a name="Procedure-calls"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">4.1.3 Procedure calls</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Procedure-calls" class="mark"></a><table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc1728" align="left" colspan="1"><em id='it1727'
>&lt;operator&gt; &lt;operand1&gt; ...,</em></td><td id="tc1729" align="right" colspan="1">syntax</td></tr>
</tbody></table>

A procedure call is written by simply enclosing in parentheses
expressions for the procedure to be called and the arguments to be
passed to it.  The operator and operand expressions are evaluated (in an
unspecified order) and the resulting procedure is passed the resulting
arguments.
<a name="g1733" class="mark"></a><a name="g1736" class="mark"></a>
<center id='center1741'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog1739'
>(+ 3 4)                                =&gt;  7
((<strong id='bold7701'
>if</strong> #f + *) 3 4)                      =&gt;  12
</pre>
</td></tr>
</tbody></table></center>

A number of procedures are available as the values of variables in the
initial environment; for example, the addition and multiplication
procedures in the above examples are the values of the variables <samp id='samp1742'
>+</samp>
and <samp id='samp1743'
>*</samp>.  New procedures are created by evaluating lambda expressions
(see section <a href="r5rs-7.html#Procedures" class="inbound">Procedures</a>).


Procedure calls may return any number of values (see <code id='code1744'
>values</code> in
<a name="g1746" class="mark"></a>section <a href="r5rs-9.html#Control-features" class="inbound">Control features</a>).  With the exception of <samp id='samp1748'
>values</samp>
the procedures available in the initial environment return one
value or, for procedures such as <samp id='samp1749'
>apply</samp>, pass on the values returned
by a call to one of their arguments.<br/><br/>Procedure calls are also called <em id='emph1751'
>combinations</em>.<br/><br/><a name="g1754" class="mark"></a>
<font size="-1"><em id='emph1756'
>Note:</em> In contrast to other dialects of Lisp, the order of
evaluation is unspecified, and the operator expression and the operand
expressions are always evaluated with the same evaluation rules.
</font>
<font size="-1"><em id='emph1758'
>Note:</em>
Although the order of evaluation is otherwise unspecified, the effect of
any concurrent evaluation of the operator and operand expressions is
constrained to be consistent with some sequential order of evaluation.
The order of evaluation may be chosen differently for each procedure call.
</font>
<font size="-1"><em id='emph1760'
>Note:</em> In many dialects of Lisp, the empty combination, <tt id='tt1761'
>()</tt>, is a legitimate expression.  In Scheme, combinations must have at
least one subexpression, so <tt id='tt1762'
>()</tt> is not a syntactically valid
expression.  
</font>
</td></tr>
</tbody></table><br/>
</div>
<!-- Procedures -->
<a name="Procedures"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">4.1.4 Procedures</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Procedures" class="mark"></a><table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g1767" class="mark"></a><a name="lambda" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc1771" align="left" colspan="1"><strong id='bold1769'
>lambda</strong><em id='it1770'
>  &lt;formals&gt; &lt;body&gt;</em></td><td id="tc1772" align="right" colspan="1">syntax</td></tr>
</tbody></table>

<em id='emph1775'
>Syntax:</em>
&lt;Formals&gt; should be a formal arguments list as described below,
and &lt;body&gt; should be a sequence of one or more expressions.<br/><br/><em id='emph1777'
>Semantics:</em>
A lambda expression evaluates to a procedure.  The environment in
effect when the lambda expression was evaluated is remembered as part of the
procedure.  When the procedure is later called with some actual
arguments, the environment in which the lambda expression was evaluated will
be extended by binding the variables in the formal argument list to
fresh locations, the corresponding actual argument values will be stored
in those locations, and the expressions in the body of the lambda expression
will be evaluated sequentially in the extended environment.
The result(s) of the last expression in the body will be returned as
the result(s) of the procedure call.<br/><br/><center id='center1790'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog1788'
>(<strong id='bold7702'
>lambda</strong> (x) (+ x x))                   =&gt;  a procedure
((<strong id='bold7703'
>lambda</strong> (x) (+ x x)) 4)               =&gt;  8<br/><br/>(<font color="#6959cf"><strong id='bold7704'
>define</strong></font> <font color="#6959cf"><strong id='bold7706'
>reverse-subtract</strong></font>
  (<strong id='bold7708'
>lambda</strong> (x y) (- y x)))
(reverse-subtract 7 10)                =&gt;  3<br/><br/>(<font color="#6959cf"><strong id='bold7709'
>define</strong></font> <font color="#6959cf"><strong id='bold7711'
>add4</strong></font>
  (<strong id='bold7713'
>let</strong> ((x 4))
    (<strong id='bold7714'
>lambda</strong> (y) (+ x y))))
(add4 6)                               =&gt;  10
</pre>
</td></tr>
</tbody></table></center>

&lt;Formals&gt; should have one of the following forms:<br/><br/><ul class="itemize" id='itemize1801'
><li><tt id='tt1792'
>(&lt;variable1&gt; ...,)</tt>:
The procedure takes a fixed number of arguments; when the procedure is
called, the arguments will be stored in the bindings of the
corresponding variables.<br/><br/></li>
<li>&lt;variable&gt;:
The procedure takes any number of arguments; when the procedure is
called, the sequence of actual arguments is converted into a newly
allocated list, and the list is stored in the binding of the
&lt;variable&gt;.<br/><br/></li>
<li><tt id='tt1798'
>(&lt;variable1&gt; ..., &lt;variable_n&gt; <strong id='bold1797'
>.</strong>
&lt;variable_n+1&gt;)</tt>:
If a space-delimited period precedes the last variable, then
the procedure takes n or more arguments, where n is the
number of formal arguments before the period (there must
be at least one).
The value stored in the binding of the last variable will be a
newly allocated
list of the actual arguments left over after all the other actual
arguments have been matched up against the other formal arguments.<br/><br/></li>
</ul>
It is an error for a &lt;variable&gt; to appear more than once in
&lt;formals&gt;.<br/><br/><center id='center1807'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog1805'
>((<strong id='bold7715'
>lambda</strong> x x) 3 4 5 6)                 =&gt;  (3 4 5 6)
((<strong id='bold7716'
>lambda</strong> (x y . z) z)
 3 4 5 6)                              =&gt;  (5 6)
</pre>
</td></tr>
</tbody></table></center>

Each procedure created as the result of evaluating a lambda expression is
(conceptually) tagged
with a storage location, in order to make <code id='code1808'
>eqv?</code> and
<a name="g1810" class="mark"></a><code id='code1812'
>eq?</code> work on procedures (see section <a href="r5rs-9.html#Equivalence-predicates" class="inbound">Equivalence predicates</a>).
<a name="g1814" class="mark"></a></td></tr>
</tbody></table><br/>
</div>
<!-- Conditionals -->
<a name="Conditionals"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">4.1.5 Conditionals</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Conditionals" class="mark"></a>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g1819" class="mark"></a><a name="if" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc1823" align="left" colspan="1"><strong id='bold1821'
>if</strong><em id='it1822'
>  &lt;test&gt; &lt;consequent&gt; &lt;alternate&gt;</em></td><td id="tc1824" align="right" colspan="1">syntax</td></tr>
</tbody></table>
<table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc1829" align="left" colspan="1"><strong id='bold1827'
>if</strong><em id='it1828'
>  &lt;test&gt; &lt;consequent&gt;  </em></td><td id="tc1830" align="right" colspan="1">syntax</td></tr>
</tbody></table>

<em id='emph1833'
>Syntax:</em>
&lt;Test&gt;, &lt;consequent&gt;, and &lt;alternate&gt; may be arbitrary
expressions.<br/><br/><em id='emph1835'
>Semantics:</em>
An <samp id='samp1836'
>if</samp> expression is evaluated as follows: first,
&lt;test&gt; is evaluated.  If it yields a true value (see
<a name="g1838" class="mark"></a>section <a href="r5rs-9.html#Booleans" class="inbound">Booleans</a>), then &lt;consequent&gt; is evaluated and
its value(s) is(are) returned.  Otherwise &lt;alternate&gt; is evaluated and its
value(s) is(are) returned.  If &lt;test&gt; yields a false value and no
&lt;alternate&gt; is specified, then the result of the expression is
unspecified.<br/><br/><center id='center1846'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog1844'
>(<strong id='bold7717'
>if</strong> (&gt; 3 2) 'yes 'no)                  =&gt;  yes
(<strong id='bold7718'
>if</strong> (&gt; 2 3) 'yes 'no)                  =&gt;  no
(<strong id='bold7719'
>if</strong> (&gt; 3 2)
    (- 3 2)
    (+ 3 2))                           =&gt;  1
</pre>
</td></tr>
</tbody></table></center>
</td></tr>
</tbody></table><br/>
</div>
<!-- Assignments -->
<a name="Assignments"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">4.1.6 Assignments</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Assignments" class="mark"></a><table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g1850" class="mark"></a><a name="set!" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc1854" align="left" colspan="1"><strong id='bold1852'
>set!</strong><em id='it1853'
>  &lt;variable&gt; &lt;expression&gt;</em></td><td id="tc1855" align="right" colspan="1">syntax</td></tr>
</tbody></table>

&lt;Expression&gt; is evaluated, and the resulting value is stored in
the location to which &lt;variable&gt; is bound.  &lt;Variable&gt; must
be bound either in some region enclosing the <samp id='samp1858'
>set!</samp> expression
<a name="g1860" class="mark"></a>or at top level.  The result of the <samp id='samp1862'
>set!</samp> expression is
unspecified.<br/><br/><center id='center1869'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog1867'
>(<font color="#6959cf"><strong id='bold7720'
>define</strong></font> <font color="#6959cf"><strong id='bold7722'
>x</strong></font> 2)
(+ x 1)                                =&gt;  3
(<strong id='bold7724'
>set!</strong> x 4)                             =&gt;  <em id='emph1864'
>unspecified</em>
(+ x 1)                                =&gt;  5
</pre>
</td></tr>
</tbody></table></center>
</td></tr>
</tbody></table><br/>
</div>
</div><br>
<!-- Derived expression types -->
<a name="Derived-expression-types"></a>
<div class="section-atitle"><table width="100%"><tr><td bgcolor="#dedeff"><h3><font color="black">4.2 Derived expression types</font>
</h3></td></tr></table>
</div><div class="section">
<a name="Derived-expression-types" class="mark"></a>

The constructs in this section are hygienic, as discussed in
section <a href="r5rs-7.html#Macros" class="inbound">Macros</a>.
For reference purposes, section <a href="r5rs-7.html#Derived-expression-types" class="inbound">Derived expression types</a> gives macro definitions
that will convert most of the constructs described in this section 
into the primitive constructs described in the previous section.<br/><br/>
<!-- Conditionals -->
<a name="Conditionals"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">4.2.1 Conditionals</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Conditional" class="mark"></a>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g1874" class="mark"></a><a name="cond" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc1878" align="left" colspan="1"><strong id='bold1876'
>cond</strong><em id='it1877'
>  &lt;clause1&gt; &lt;clause2&gt; ...,</em></td><td id="tc1879" align="right" colspan="1">library syntax</td></tr>
</tbody></table>

<em id='emph1882'
>Syntax:</em>
Each &lt;clause&gt; should be of the form<br/><br/><center id='center1886'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog1884'
>(&lt;test&gt; &lt;expression1&gt; ...,)
</pre>
</td></tr>
</tbody></table></center>

where &lt;test&gt; is any expression.  Alternatively, a &lt;clause&gt; may be
of the form<br/><br/><center id='center1890'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog1888'
>(&lt;test&gt; =&gt; &lt;expression&gt;)
</pre>
</td></tr>
</tbody></table></center>

The last &lt;clause&gt; may be
an ``else clause,'' which has the form<br/><br/><center id='center1894'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog1892'
>(else &lt;expression1&gt; &lt;expression2&gt; ...,).
</pre>
</td></tr>
</tbody></table></center>

<a name="g1896" class="mark"></a><a name="g1899" class="mark"></a>
<em id='emph1901'
>Semantics:</em>
A <samp id='samp1902'
>cond</samp> expression is evaluated by evaluating the &lt;test&gt;
expressions of successive &lt;clause&gt;s in order until one of them
evaluates to a true value (see
<a name="g1904" class="mark"></a>section <a href="r5rs-9.html#Booleans" class="inbound">Booleans</a>).  When a &lt;test&gt; evaluates to a true
value, then the remaining &lt;expression&gt;s in its &lt;clause&gt; are
evaluated in order, and the result(s) of the last &lt;expression&gt; in the
&lt;clause&gt; is(are) returned as the result(s) of the entire <samp id='samp1906'
>cond</samp>
expression.  If the selected &lt;clause&gt; contains only the
&lt;test&gt; and no &lt;expression&gt;s, then the value of the
&lt;test&gt; is returned as the result.  If the selected &lt;clause&gt; uses the
<code id='code1907'
>=&gt;</code> alternate form, then the &lt;expression&gt; is evaluated.
<a name="g1909" class="mark"></a>Its value must be a procedure that accepts one argument; this procedure is then
called on the value of the &lt;test&gt; and the value(s) returned by this
procedure is(are) returned by the <samp id='samp1911'
>cond</samp> expression.
If all &lt;test&gt;s evaluate
to false values, and there is no else clause, then the result of
the conditional expression is unspecified; if there is an else
clause, then its &lt;expression&gt;s are evaluated, and the value(s) of
the last one is(are) returned.<br/><br/><center id='center1920'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog1918'
>(<strong id='bold7725'
>cond</strong> ((&gt; 3 2) 'greater)
      ((&lt; 3 2) 'less))                 =&gt;  greater<br/><br/>(<strong id='bold7726'
>cond</strong> ((&gt; 3 3) 'greater)
      ((&lt; 3 3) 'less)
      (else 'equal))                   =&gt;  equal<br/><br/>(<strong id='bold7727'
>cond</strong> ((assv 'b '((a 1) (b 2))) =&gt; cadr)
      (else #f))                       =&gt;  2
</pre>
</td></tr>
</tbody></table></center>
</td></tr>
</tbody></table><br/>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g1924" class="mark"></a><a name="case" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc1928" align="left" colspan="1"><strong id='bold1926'
>case</strong><em id='it1927'
>  &lt;key&gt; &lt;clause1&gt; &lt;clause2&gt; ...,</em></td><td id="tc1929" align="right" colspan="1">library syntax</td></tr>
</tbody></table>

<em id='emph1932'
>Syntax:</em>
&lt;Key&gt; may be any expression.  Each &lt;clause&gt; should have
the form<br/><br/><center id='center1936'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog1934'
>((&lt;datum1&gt; ...,) &lt;expression1&gt; &lt;expression2&gt; ...,),
</pre>
</td></tr>
</tbody></table></center>

where each &lt;datum&gt; is an external representation of some object.
All the &lt;datum&gt;s must be distinct.
The last &lt;clause&gt; may be an ``else clause,'' which has the form<br/><br/><center id='center1940'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog1938'
>(else &lt;expression1&gt; &lt;expression2&gt; ...,).
</pre>
</td></tr>
</tbody></table></center>
<a name="g1941" class="mark"></a>
<em id='emph1943'
>Semantics:</em>
A <samp id='samp1944'
>case</samp> expression is evaluated as follows.  &lt;Key&gt; is
evaluated and its result is compared against each &lt;datum&gt;.  If the
result of evaluating &lt;key&gt; is equivalent (in the sense of
<samp id='samp1945'
>eqv?</samp>; see section <a href="r5rs-9.html#Equivalence-predicates" class="inbound">Equivalence predicates</a>) to a &lt;datum&gt;, then the
expressions in the corresponding &lt;clause&gt; are evaluated from left
to right and the result(s) of the last expression in the &lt;clause&gt; is(are)
returned as the result(s) of the <samp id='samp1946'
>case</samp> expression.  If the result of
evaluating &lt;key&gt; is different from every &lt;datum&gt;, then if
there is an else clause its expressions are evaluated and the
result(s) of the last is(are) the result(s) of the <samp id='samp1947'
>case</samp> expression;
otherwise the result of the <samp id='samp1948'
>case</samp> expression is unspecified.<br/><br/><center id='center1956'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog1954'
>(<strong id='bold7728'
>case</strong> (* 2 3)
  ((2 3 5 7) 'prime)
  ((1 4 6 8 9) 'composite))            =&gt;  composite
(<strong id='bold7729'
>case</strong> (car '(c d))
  ((a) 'a)
  ((b) 'b))                            =&gt;  <em id='emph1950'
>unspecified</em>
(<strong id='bold7730'
>case</strong> (car '(c d))
  ((a e i o u) 'vowel)
  ((w y) 'semivowel)
  (else 'consonant))                   =&gt;  consonant
</pre>
</td></tr>
</tbody></table></center>
</td></tr>
</tbody></table><br/>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g1960" class="mark"></a><a name="and" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc1964" align="left" colspan="1"><strong id='bold1962'
>and</strong><em id='it1963'
>  &lt;test1&gt; ...,</em></td><td id="tc1965" align="right" colspan="1">library syntax</td></tr>
</tbody></table>

The &lt;test&gt; expressions are evaluated from left to right, and the
value of the first expression that evaluates to a false value (see
section <a href="r5rs-9.html#Booleans" class="inbound">Booleans</a>) is returned.  Any remaining expressions
are not evaluated.  If all the expressions evaluate to true values, the
value of the last expression is returned.  If there are no expressions
then <tt id='tt1968'
>#t</tt> is returned.<br/><br/><center id='center1972'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog1970'
>(and (= 2 2) (&gt; 2 1))                  =&gt;  #t
(and (= 2 2) (&lt; 2 1))                  =&gt;  #f
(and 1 2 'c '(f g))                    =&gt;  (f g)
(and)                                  =&gt;  #t
</pre>
</td></tr>
</tbody></table></center>
</td></tr>
</tbody></table><br/>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g1976" class="mark"></a><a name="or" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc1980" align="left" colspan="1"><strong id='bold1978'
>or</strong><em id='it1979'
>  &lt;test1&gt; ...,</em></td><td id="tc1981" align="right" colspan="1">library syntax</td></tr>
</tbody></table>

The &lt;test&gt; expressions are evaluated from left to right, and the value of the
first expression that evaluates to a true value (see
section <a href="r5rs-9.html#Booleans" class="inbound">Booleans</a>) is returned.  Any remaining expressions
are not evaluated.  If all expressions evaluate to false values, the
value of the last expression is returned.  If there are no
expressions then <tt id='tt1984'
>#f</tt> is returned.<br/><br/><center id='center1988'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog1986'
>(or (= 2 2) (&gt; 2 1))                   =&gt;  #t
(or (= 2 2) (&lt; 2 1))                   =&gt;  #t
(or #f #f #f)                          =&gt;  #f
(or (memq 'b '(a b c)) 
    (/ 3 0))                           =&gt;  (b c)
</pre>
</td></tr>
</tbody></table></center>
</td></tr>
</tbody></table><br/>
</div>
<!-- Binding constructs -->
<a name="Binding-constructs"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">4.2.2 Binding constructs</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Binding-constructs" class="mark"></a>
The three binding constructs <samp id='samp1991'
>let</samp>, <samp id='samp1992'
>let*</samp>, and <samp id='samp1993'
>letrec</samp>
give Scheme a block structure, like Algol 60.  The syntax of the three
constructs is identical, but they differ in the regions they establish
<a name="g1995" class="mark"></a>for their variable bindings.  In a <samp id='samp1997'
>let</samp> expression, the initial
values are computed before any of the variables become bound; in a
<samp id='samp1998'
>let*</samp> expression, the bindings and evaluations are performed
sequentially; while in a <samp id='samp1999'
>letrec</samp> expression, all the bindings are in
effect while their initial values are being computed, thus allowing
mutually recursive definitions.<br/><br/><table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g2002" class="mark"></a><a name="let" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc2006" align="left" colspan="1"><strong id='bold2004'
>let</strong><em id='it2005'
>  &lt;bindings&gt; &lt;body&gt;</em></td><td id="tc2007" align="right" colspan="1">library syntax</td></tr>
</tbody></table>

<em id='emph2010'
>Syntax:</em>
&lt;Bindings&gt; should have the form<br/><br/><center id='center2014'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog2012'
>((&lt;variable1&gt; &lt;init1&gt;) ...,),
</pre>
</td></tr>
</tbody></table></center>

where each &lt;init&gt; is an expression, and &lt;body&gt; should be a
sequence of one or more expressions.  It is
an error for a &lt;variable&gt; to appear more than once in the list of variables
being bound.<br/><br/><em id='emph2016'
>Semantics:</em>
The &lt;init&gt;s are evaluated in the current environment (in some
unspecified order), the &lt;variable&gt;s are bound to fresh locations
holding the results, the &lt;body&gt; is evaluated in the extended
environment, and the value(s) of the last expression of &lt;body&gt;
is(are) returned.  Each binding of a &lt;variable&gt; has &lt;body&gt; as its
region.
<a name="g2018" class="mark"></a>
<center id='center2026'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2024'
>(<strong id='bold7731'
>let</strong> ((x 2) (y 3))
  (* x y))                             =&gt;  6<br/><br/>(<strong id='bold7732'
>let</strong> ((x 2) (y 3))
  (<strong id='bold7733'
>let</strong> ((x 7)
        (z (+ x y)))
    (* z x)))                          =&gt;  35
</pre>
</td></tr>
</tbody></table></center>
See also named <samp id='samp2027'
>let</samp>, section <a href="r5rs-7.html#Iteration" class="inbound">Iteration</a>.
</td></tr>
</tbody></table><br/>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g2031" class="mark"></a><a name="let*" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc2035" align="left" colspan="1"><strong id='bold2033'
>let*</strong><em id='it2034'
>  &lt;bindings&gt; &lt;body&gt;</em></td><td id="tc2036" align="right" colspan="1">library syntax</td></tr>
</tbody></table>
<em id='emph2039'
>Syntax:</em>
&lt;Bindings&gt; should have the form<br/><br/><center id='center2043'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog2041'
>((&lt;variable1&gt; &lt;init1&gt;) ...,),
</pre>
</td></tr>
</tbody></table></center>

and &lt;body&gt; should be a sequence of
one or more expressions.<br/><br/><em id='emph2045'
>Semantics:</em>
<samp id='samp2046'
>Let*</samp> is similar to <samp id='samp2047'
>let</samp>, but the bindings are performed
sequentially from left to right, and the region of a binding indicated
<a name="g2049" class="mark"></a>by <samp id='samp2051'
>(&lt;variable&gt; &lt;init&gt;)</samp> is that part of the <samp id='samp2052'
>let*</samp>
expression to the right of the binding.  Thus the second binding is done
in an environment in which the first binding is visible, and so on.<br/><br/><center id='center2058'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2056'
>(<strong id='bold7734'
>let</strong> ((x 2) (y 3))
  (<strong id='bold7735'
>let*</strong> ((x 7)
         (z (+ x y)))
    (* z x)))                          =&gt;  70
</pre>
</td></tr>
</tbody></table></center>
</td></tr>
</tbody></table><br/>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g2062" class="mark"></a><a name="letrec" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc2066" align="left" colspan="1"><strong id='bold2064'
>letrec</strong><em id='it2065'
>  &lt;bindings&gt; &lt;body&gt;</em></td><td id="tc2067" align="right" colspan="1">library syntax</td></tr>
</tbody></table>

<em id='emph2070'
>Syntax:</em>
&lt;Bindings&gt; should have the form<br/><br/><center id='center2074'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog2072'
>((&lt;variable1&gt; &lt;init1&gt;) ...,),
</pre>
</td></tr>
</tbody></table></center>

and &lt;body&gt; should be a sequence of
one or more expressions. It is an error for a &lt;variable&gt; to appear more
than once in the list of variables being bound.<br/><br/><em id='emph2076'
>Semantics:</em>
The &lt;variable&gt;s are bound to fresh locations holding undefined
values, the &lt;init&gt;s are evaluated in the resulting environment (in
some unspecified order), each &lt;variable&gt; is assigned to the result
of the corresponding &lt;init&gt;, the &lt;body&gt; is evaluated in the
resulting environment, and the value(s) of the last expression in
&lt;body&gt; is(are) returned.  Each binding of a &lt;variable&gt; has the
entire <samp id='samp2077'
>letrec</samp> expression as its region, making it possible to
<a name="g2079" class="mark"></a>define mutually recursive procedures.<br/><br/><center id='center2089'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2087'
>(<strong id='bold7736'
>letrec</strong> ((even?
          (<strong id='bold7737'
>lambda</strong> (n)
            (<strong id='bold7738'
>if</strong> (zero? n)
                #t
                (odd? (- n 1)))))
         (odd?
          (<strong id='bold7739'
>lambda</strong> (n)
            (<strong id='bold7740'
>if</strong> (zero? n)
                #f
                (even? (- n 1))))))
  (even? 88))   
                                       =&gt;  #t
</pre>
</td></tr>
</tbody></table></center>

One restriction on <samp id='samp2090'
>letrec</samp> is very important: it must be possible
to evaluate each &lt;init&gt; without assigning or referring to the value of any
&lt;variable&gt;.  If this restriction is violated, then it is an error.  The
restriction is necessary because Scheme passes arguments by value rather than by
name.  In the most common uses of <samp id='samp2091'
>letrec</samp>, all the &lt;init&gt;s are
lambda expressions and the restriction is satisfied automatically.<br/><br/>
</td></tr>
</tbody></table><br/>
</div>
<!-- Sequencing -->
<a name="Sequencing"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">4.2.3 Sequencing</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Sequencing" class="mark"></a>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g2096" class="mark"></a><a name="begin" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc2100" align="left" colspan="1"><strong id='bold2098'
>begin</strong><em id='it2099'
>  &lt;expression1&gt; &lt;expression2&gt; ...,</em></td><td id="tc2101" align="right" colspan="1">library syntax</td></tr>
</tbody></table>

The &lt;expression&gt;s are evaluated sequentially from left to right,
and the value(s) of the last &lt;expression&gt; is(are) returned.  This
expression type is used to sequence side effects such as input and
output.<br/><br/><center id='center2116'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2114'
>(<font color="#6959cf"><strong id='bold7741'
>define</strong></font> <font color="#6959cf"><strong id='bold7743'
>x</strong></font> 0)<br/><br/>(<strong id='bold7745'
>begin</strong> (<strong id='bold7746'
>set!</strong> x 5)
       (+ x 1))                        =&gt;  6<br/><br/>(<strong id='bold7747'
>begin</strong> (display <font color="red">&quot;4 plus 1 equals &quot;</font>)
       (display (+ 4 1)))              =&gt;  <em id='emph2107'
>unspecified</em>
          <em id='emph2108'
>and prints</em>  4 plus 1 equals 5
</pre>
</td></tr>
</tbody></table></center>
</td></tr>
</tbody></table><br/>
</div>
<!-- Iteration -->
<a name="Iteration"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">4.2.4 Iteration</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Iteration" class="mark"></a>


<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g2120" class="mark"></a><a name="do" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc2125" align="left" colspan="1"><strong id='bold2122'
>do</strong><em id='it2124'
> ((&lt;variable1&gt; &lt;init1&gt; &lt;step1&gt;) <tt id='tt2123'
> ...,) (&lt;test&gt; &lt;expression&gt; ...,) &lt;command&gt; ...,)</tt></em></td><td id="tc2126" align="right" colspan="1">library syntax</td></tr>
</tbody></table>
<a name="g2130" class="mark"></a>
<samp id='samp2132'
>Do</samp> is an iteration construct.  It specifies a set of variables to
be bound, how they are to be initialized at the start, and how they are
to be updated on each iteration.  When a termination condition is met,
the loop exits after evaluating the &lt;expression&gt;s.<br/><br/><samp id='samp2134'
>Do</samp> expressions are evaluated as follows:
The &lt;init&gt; expressions are evaluated (in some unspecified order),
the &lt;variable&gt;s are bound to fresh locations, the results of the
&lt;init&gt; expressions are stored in the bindings of the
&lt;variable&gt;s, and then the iteration phase begins.<br/><br/>Each iteration begins by evaluating &lt;test&gt;; if the result is
false (see section <a href="r5rs-9.html#Booleans" class="inbound">Booleans</a>), then the &lt;command&gt;
expressions are evaluated in order for effect, the &lt;step&gt;
expressions are evaluated in some unspecified order, the
&lt;variable&gt;s are bound to fresh locations, the results of the
&lt;step&gt;s are stored in the bindings of the
&lt;variable&gt;s, and the next iteration begins.<br/><br/>If &lt;test&gt; evaluates to a true value, then the
&lt;expression&gt;s are evaluated from left to right and the value(s) of
the last &lt;expression&gt; is(are) returned.  If no &lt;expression&gt;s
are present, then the value of the <samp id='samp2137'
>do</samp> expression is unspecified.<br/><br/>The region of the binding of a &lt;variable&gt;
<a name="g2140" class="mark"></a>consists of the entire <samp id='samp2142'
>do</samp> expression except for the &lt;init&gt;s.
It is an error for a &lt;variable&gt; to appear more than once in the
list of <samp id='samp2143'
>do</samp> variables.<br/><br/>A &lt;step&gt; may be omitted, in which case the effect is the
same as if <samp id='samp2145'
>(&lt;variable&gt; &lt;init&gt; &lt;variable&gt;)</samp> had
been written instead of <samp id='samp2146'
>(&lt;variable&gt; &lt;init&gt;)</samp>.<br/><br/><center id='center2152'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2150'
>(do ((vec (make-vector 5))
     (i 0 (+ i 1)))
    ((= i 5) vec)
  (vector-set! vec i i))               =&gt;  #(0 1 2 3 4)<br/><br/>(<strong id='bold7749'
>let</strong> ((x '(1 3 5 7 9)))
  (do ((x x (cdr x))
       (sum 0 (+ sum (car x))))
      ((null? x) sum)))                =&gt;  25
</pre>
</td></tr>
</tbody></table></center>

<table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc2155" align="left" colspan="1"><strong id='bold2153'
>let</strong><em id='it2154'
>  &lt;variable&gt; &lt;bindings&gt; &lt;body&gt;</em></td><td id="tc2156" align="right" colspan="1">library syntax</td></tr>
</tbody></table>

``Named <samp id='samp2159'
>let</samp>'' is a variant on the syntax of <code id='code2160'
>let</code> which provides
<a name="g2162" class="mark"></a>a more general looping construct than <samp id='samp2164'
>do</samp> and may also be used to express
recursions.
It has the same syntax and semantics as ordinary <samp id='samp2165'
>let</samp>
except that &lt;variable&gt; is bound within &lt;body&gt; to a procedure
whose formal arguments are the bound variables and whose body is
&lt;body&gt;.  Thus the execution of &lt;body&gt; may be repeated by
invoking the procedure named by &lt;variable&gt;.<br/><br/><center id='center2171'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2169'
>(<strong id='bold7750'
>let</strong> loop ((numbers '(3 -2 1 6 -5))
           (nonneg '())
           (neg '()))
  (<strong id='bold7751'
>cond</strong> ((null? numbers) (list nonneg neg))
        ((&gt;= (car numbers) 0)
         (loop (cdr numbers)
               (cons (car numbers) nonneg)
               neg))
        ((&lt; (car numbers) 0)
         (loop (cdr numbers)
               nonneg
               (cons (car numbers) neg)))))   
          =&gt;  ((6 1 3) (-5 -2))
</pre>
</td></tr>
</tbody></table></center>
</td></tr>
</tbody></table><br/><br/><br/></div>
<!-- Delayed evaluation -->
<a name="Delayed-evaluation"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">4.2.5 Delayed evaluation</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Delayed-evaluation" class="mark"></a>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g2176" class="mark"></a><a name="delay" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc2180" align="left" colspan="1"><strong id='bold2178'
>delay</strong><em id='it2179'
>  &lt;expression&gt;</em></td><td id="tc2181" align="right" colspan="1">library syntax</td></tr>
</tbody></table>


The <samp id='samp2184'
>delay</samp> construct is used together with the procedure <code id='code2185'
>force</code> to
<a name="g2187" class="mark"></a>implement &quot;lazy evaluation&quot; or &quot;call by need&quot;.
<a name="g2190" class="mark"></a><a name="g2193" class="mark"></a><tt id='tt2195'
>(delay &lt;expression&gt;)</tt> returns an object called a
&quot;promise&quot; which at some point in the future may be asked (by
<a name="g2197" class="mark"></a>the <samp id='samp2199'
>force</samp> procedure) 
 to evaluate
&lt;expression&gt;, and deliver the resulting value.
The effect of &lt;expression&gt; returning multiple values
is unspecified.<br/><br/>See the description of <samp id='samp2201'
>force</samp> (section <a href="r5rs-9.html#Control-features" class="inbound">Control features</a>) for a
more complete description of <samp id='samp2202'
>delay</samp>.<br/><br/></td></tr>
</tbody></table><br/>
</div>
<!-- Quasiquotation -->
<a name="Quasiquotation"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">4.2.6 Quasiquotation</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Quasiquotation" class="mark"></a>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g2207" class="mark"></a><a name="quasiquote" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc2211" align="left" colspan="1"><strong id='bold2209'
>quasiquote</strong><em id='it2210'
>  &lt;qq template&gt; </em></td><td id="tc2212" align="right" colspan="1">syntax</td></tr>
</tbody></table>
<table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc2217" align="left" colspan="1"><em id='it2216'
><tt id='tt2215'
>`</tt>&lt;qq template&gt;</em></td><td id="tc2218" align="right" colspan="1">syntax</td></tr>
</tbody></table>

``Backquote'' or ``quasiquote'' expressions are useful
<a name="g2222" class="mark"></a>for constructing a list or vector structure when most but not all of the
desired structure is known in advance.  If no
commas appear within the &lt;qq template&gt;, the result of
<a name="g2225" class="mark"></a>evaluating
<tt id='tt2227'
>`</tt>&lt;qq template&gt; is equivalent to the result of evaluating
<tt id='tt2228'
>'</tt>&lt;qq template&gt;.  If a comma appears within the
<a name="g2230" class="mark"></a>&lt;qq template&gt;, however, the expression following the comma is
evaluated (``unquoted'') and its result is inserted into the structure
instead of the comma and the expression.  If a comma appears followed
immediately by an at-sign (&#x40;), then the following
<a name="g2233" class="mark"></a>expression must evaluate to a list; the opening and closing parentheses
of the list are then ``stripped away'' and the elements of the list are
inserted in place of the comma at-sign expression sequence.  A comma
at-sign should only appear within a list or vector &lt;qq template&gt;.<br/><br/>
<center id='center2241'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2239'
>`(list ,(+ 1 2) 4)                     =&gt;  (list 3 4)
(<strong id='bold7752'
>let</strong> ((name 'a)) `(list ,name ',name))           
          =&gt;  (list a (<strong id='bold7753'
>quote</strong> a))
`(a ,(+ 1 2) ,&#x40;(map abs '(4 -5 6)) b)           
          =&gt;  (a 3 4 5 6 b)
`((<samp id='samp2236'
>foo</samp> ,(- 10 3)) ,&#x40;(cdr '(c)) . ,(car '(cons)))           
          =&gt;  ((foo 7) . cons)
`#(10 5 ,(sqrt 4) ,&#x40;(map sqrt '(16 9)) 8)           
          =&gt;  #(10 5 2 4 3 8)
</pre>
</td></tr>
</tbody></table></center>

Quasiquote forms may be nested.  Substitutions are made only for
unquoted components appearing at the same nesting level
as the outermost backquote.  The nesting level increases by one inside
each successive quasiquotation, and decreases by one inside each
unquotation.<br/><br/><center id='center2246'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2244'
>`(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f)           
          =&gt;  (a `(b ,(+ 1 2) ,(foo 4 d) e) f)
(<strong id='bold7754'
>let</strong> ((name1 'x)
      (name2 'y))
  `(a `(b ,,name1 ,',name2 d) e))           
          =&gt;  (a `(b ,x ,'y d) e)
</pre>
</td></tr>
</tbody></table></center>

The two notations
 <tt id='tt2247'
>`</tt>&lt;qq template&gt; and <tt id='tt2248'
>(quasiquote &lt;qq template&gt;)</tt>
 are identical in all respects.
 <samp id='samp2249'
>,&lt;expression&gt;</samp> is identical to <samp id='samp2250'
>(unquote &lt;expression&gt;)</samp>,
 and
 <samp id='samp2251'
>,&#x40;&lt;expression&gt;</samp> is identical to <samp id='samp2252'
>(unquote-splicing &lt;expression&gt;)</samp>.
The external syntax generated by <code id='code2253'
>write</code> for two-element lists whose
<a name="g2255" class="mark"></a>car is one of these symbols may vary between implementations.<br/><br/><a name="g2259" class="mark"></a>
<center id='center2263'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2261'
>(quasiquote (list (unquote (+ 1 2)) 4))           
          =&gt;  (list 3 4)
'(quasiquote (list (unquote (+ 1 2)) 4))           
          =&gt;  `(list ,(+ 1 2) 4)
     i.e., (quasiquote (list (unquote (+ 1 2)) 4))
</pre>
</td></tr>
</tbody></table></center>

Unpredictable behavior can result if any of the symbols
<code id='code2264'
>quasiquote</code>, <code id='code2265'
>unquote</code>, or <code id='code2266'
>unquote-splicing</code> appear in
<a name="g2268" class="mark"></a><a name="g2271" class="mark"></a><a name="g2274" class="mark"></a>positions within a &lt;qq template&gt; otherwise than as described above.
</td></tr>
</tbody></table><br/>
</div>
</div><br>
<!-- Macros -->
<a name="Macros"></a>
<div class="section-atitle"><table width="100%"><tr><td bgcolor="#dedeff"><h3><font color="black">4.3 Macros</font>
</h3></td></tr></table>
</div><div class="section">
<a name="Macros" class="mark"></a>

Scheme programs can define and use new derived expression types,
 called <em id='emph2278'
>macros</em>.
<a name="g2280" class="mark"></a>Program-defined expression types have the syntax<br/><br/><center id='center2285'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog2283'
>(&lt;keyword&gt; &lt;datum&gt; ...)
</pre>
</td></tr>
</tbody></table></center>

where &lt;keyword&gt; is an identifier that uniquely determines the
expression type.  This identifier is called the <em id='emph2286'
>syntactic
keyword</em>, or simply <em id='emph2287'
>keyword</em>, of the macro.  The
<a name="g2289" class="mark"></a><a name="g2292" class="mark"></a><a name="g2295" class="mark"></a>number of the &lt;datum&gt;s, and their syntax, depends on the
expression type.<br/><br/>Each instance of a macro is called a <em id='emph2298'
>use</em>
<a name="g2300" class="mark"></a>of the macro.
The set of rules that specifies
how a use of a macro is transcribed into a more primitive expression
is called the <em id='emph2302'
>transformer</em>
<a name="g2304" class="mark"></a>of the macro.<br/><br/>The macro definition facility consists of two parts:<br/><br/><ul class="itemize" id='itemize2312'
><li>A set of expressions used to establish that certain identifiers
are macro keywords, associate them with macro transformers, and control
the scope within which a macro is defined, and<br/><br/></li>
<li>a pattern language for specifying macro transformers.<br/><br/></li>
</ul>
The syntactic keyword of a macro may shadow variable bindings, and local
variable bindings may shadow keyword bindings.    All macros
<a name="g2314" class="mark"></a>defined using the pattern language  are ``hygienic'' and ``referentially
transparent'' and thus preserve Scheme's lexical scoping [Kohlbecker86], [
hygienic], [Bawden88], [macrosthatwork], [syntacticabstraction]:<br/><br/><a name="g2318" class="mark"></a>
<a name="g2321" class="mark"></a>
<ul class="itemize" id='itemize2328'
><li>If a macro transformer inserts a binding for an identifier
(variable or keyword), the identifier will in effect be renamed
throughout its scope to avoid conflicts with other identifiers.
Note that a <code id='code2323'
>define</code> at top level may or may not introduce a binding;
see section <a href="r5rs-8.html#Definitions" class="inbound">Definitions</a>.<br/><br/></li>
<li>If a macro transformer inserts a free reference to an
identifier, the reference refers to the binding that was visible
where the transformer was specified, regardless of any local
bindings that may surround the use of the macro.<br/><br/></li>
</ul>
<a name="g2330" class="mark"></a>


<!-- Binding constructs for syntactic keywords -->
<a name="Binding-constructs-for-syntactic-keywords"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">4.3.1 Binding constructs for syntactic keywords</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Binding-constructs-for-syntactic-keywords" class="mark"></a>
<samp id='samp2332'
>Let-syntax</samp> and <samp id='samp2333'
>letrec-syntax</samp> are
analogous to <samp id='samp2334'
>let</samp> and <samp id='samp2335'
>letrec</samp>, but they bind
syntactic keywords to macro transformers instead of binding variables
to locations that contain values.  Syntactic keywords may also be
bound at top level; see section <a href="r5rs-8.html#Syntax-definitions" class="inbound">Syntax definitions</a>.<br/><br/><table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g2338" class="mark"></a><a name="let-syntax" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc2342" align="left" colspan="1"><strong id='bold2340'
>let-syntax</strong><em id='it2341'
>  &lt;bindings&gt; &lt;body&gt;</em></td><td id="tc2343" align="right" colspan="1">syntax</td></tr>
</tbody></table>
<em id='emph2346'
>Syntax:</em>
&lt;Bindings&gt; should have the form<br/><br/><center id='center2350'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog2348'
>((&lt;keyword&gt; &lt;transformer spec&gt;) ...,)
</pre>
</td></tr>
</tbody></table></center>

Each &lt;keyword&gt; is an identifier,
each &lt;transformer spec&gt; is an instance of <samp id='samp2351'
>syntax-rules</samp>, and
&lt;body&gt; should be a sequence of one or more expressions.  It is an error
for a &lt;keyword&gt; to appear more than once in the list of keywords
being bound.<br/><br/><em id='emph2353'
>Semantics:</em>
The &lt;body&gt; is expanded in the syntactic environment
obtained by extending the syntactic environment of the
<samp id='samp2354'
>let-syntax</samp> expression with macros whose keywords are
the &lt;keyword&gt;s, bound to the specified transformers.
Each binding of a &lt;keyword&gt; has &lt;body&gt; as its region.<br/><br/><center id='center2368'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2366'
>(let-syntax ((when (<strong id='bold7755'
>syntax-rules</strong> ()
                     ((when test stmt1 stmt2 ...)
                      (<strong id='bold7756'
>if</strong> test
                          (<strong id='bold7757'
>begin</strong> stmt1
                                 stmt2 ...))))))
  (<strong id='bold7758'
>let</strong> ((<strong id='bold7759'
>if</strong> #t))
    (when if (<strong id='bold7760'
>set!</strong> if 'now))
    if))                               =&gt;  now<br/><br/>(<strong id='bold7761'
>let</strong> ((x 'outer))
  (let-syntax ((m (<strong id='bold7762'
>syntax-rules</strong> () ((m) x))))
    (<strong id='bold7763'
>let</strong> ((x 'inner))
      (m))))                           =&gt;  outer
</pre>
</td></tr>
</tbody></table></center>
</td></tr>
</tbody></table><br/>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g2372" class="mark"></a><a name="letrec-syntax" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc2376" align="left" colspan="1"><strong id='bold2374'
>letrec-syntax</strong><em id='it2375'
>  &lt;bindings&gt; &lt;body&gt;</em></td><td id="tc2377" align="right" colspan="1">syntax</td></tr>
</tbody></table>

<em id='emph2380'
>Syntax:</em>
Same as for <samp id='samp2381'
>let-syntax</samp>.<br/><br/><em id='emph2383'
>Semantics:</em>
 The &lt;body&gt; is expanded in the syntactic environment obtained by
extending the syntactic environment of the <samp id='samp2384'
>letrec-syntax</samp>
expression with macros whose keywords are the
&lt;keyword&gt;s, bound to the specified transformers.
Each binding of a &lt;keyword&gt; has the &lt;bindings&gt;
as well as the &lt;body&gt; within its region,
so the transformers can
transcribe expressions into uses of the macros
introduced by the <samp id='samp2385'
>letrec-syntax</samp> expression.<br/><br/><center id='center2397'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2395'
>(letrec-syntax
  ((my-or (<strong id='bold7764'
>syntax-rules</strong> ()
            ((my-or) #f)
            ((my-or e) e)
            ((my-or e1 e2 ...)
             (<strong id='bold7765'
>let</strong> ((temp e1))
               (<strong id='bold7766'
>if</strong> temp
                   temp
                   (my-or e2 ...)))))))
  (<strong id='bold7767'
>let</strong> ((x #f)
        (y 7)
        (temp 8)
        (<strong id='bold7768'
>let</strong> odd?)
        (<strong id='bold7769'
>if</strong> even?))
    (my-or x
           (<strong id='bold7770'
>let</strong> temp)
           (<strong id='bold7771'
>if</strong> y)
           y)))                        =&gt;  7
</pre>
</td></tr>
</tbody></table></center>
</td></tr>
</tbody></table><br/>
</div>
<!-- Pattern language -->
<a name="Pattern-language"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">4.3.2 Pattern language</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Pattern-language" class="mark"></a>
A &lt;transformer spec&gt; has the following form:<br/><br/><table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g2402" class="mark"></a><a name="syntax-rules" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc2406" align="left" colspan="1"><strong id='bold2404'
>syntax-rules</strong><em id='it2405'
>  &lt;literals&gt; &lt;syntax rule&gt; ...,</em></td><td id="tc2407" align="right" colspan="1"></td></tr>
</tbody></table>

<em id='emph2410'
>Syntax:</em>
&lt;Literals&gt; is a list of identifiers and each &lt;syntax rule&gt;
should be of the form<br/><br/><center id='center2414'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog2412'
>(&lt;pattern&gt; &lt;template&gt;)
</pre>
</td></tr>
</tbody></table></center>

The &lt;pattern&gt; in a &lt;syntax rule&gt; is a list &lt;pattern&gt;
that begins with the keyword for the macro.<br/><br/>A &lt;pattern&gt; is either an identifier, a constant, or one of the
following<br/><br/><center id='center2419'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog2417'
>(&lt;pattern&gt; ...)
(&lt;pattern&gt; &lt;pattern&gt; ... . &lt;pattern&gt;)
(&lt;pattern&gt; ... &lt;pattern&gt; &lt;ellipsis&gt;)
#(&lt;pattern&gt; ...)
#(&lt;pattern&gt; ... &lt;pattern&gt; &lt;ellipsis&gt;)
</pre>
</td></tr>
</tbody></table></center>

and a template is either an identifier, a constant, or one of the following<br/><br/><center id='center2423'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog2421'
>(&lt;element&gt; ...)
(&lt;element&gt; &lt;element&gt; ... . &lt;template&gt;)
#(&lt;element&gt; ...)
</pre>
</td></tr>
</tbody></table></center>

where an &lt;element&gt; is a &lt;template&gt; optionally
followed by an &lt;ellipsis&gt; and
an &lt;ellipsis&gt; is the identifier ``<samp id='samp2424'
>...</samp>'' (which cannot be used as
an identifier in either a template or a pattern).
<a name="g2425" class="mark"></a>
<br/><em id='emph2428'
>Semantics:</em> An instance of <samp id='samp2429'
>syntax-rules</samp> produces 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
<samp id='samp2430'
>syntax-rules</samp> is matched against the patterns contained in the
&lt;syntax rule&gt;s, beginning with the leftmost &lt;syntax rule&gt;.
When a match is found, the macro use is transcribed hygienically
according to the template.<br/><br/>An identifier that appears in the pattern of a &lt;syntax rule&gt; is
a <em id='emph2432'
>pattern variable</em>, unless it is the keyword that begins the pattern,
is listed in &lt;literals&gt;, or is the identifier ``<samp id='samp2433'
>...</samp>''.
Pattern variables match arbitrary input elements and
are used to refer to elements of the input in the template.  It is an
error for the same pattern variable to appear more than once in a
&lt;pattern&gt;.<br/><br/>The keyword at the beginning of the pattern in a
&lt;syntax rule&gt; is not involved in the matching and
is not considered a pattern variable or literal identifier.<br/><br/><br/><em id='emph2437'
>Rationale:</em><br/><br/>The scope of the keyword is determined by the expression or syntax
definition that binds it to the associated macro transformer.
If the keyword were a pattern variable or literal
identifier, then
the template that follows the pattern would be within its scope
regardless of whether the keyword were bound by <samp id='samp2439'
>let-syntax</samp>
or by <samp id='samp2440'
>letrec-syntax</samp>.<br/><br/>Identifiers that appear in &lt;literals&gt; are interpreted as literal
identifiers to be matched against corresponding subforms of the input.
A subform
in the input matches a literal identifier if and only if it is an
identifier
and either both its occurrence in the macro expression and its
occurrence in the macro definition have the same lexical binding, or
the two identifiers are equal and both have no lexical binding.<br/><br/>
A subpattern followed by <samp id='samp2443'
>...</samp> can match zero or more elements of the
input.  It is an error for <samp id='samp2444'
>...</samp> to appear in &lt;literals&gt;.
Within a pattern the identifier <samp id='samp2445'
>...</samp> must follow the last element of
a nonempty sequence of subpatterns.<br/><br/>More formally, an input form F matches a pattern P if and only if:<br/><br/><ul class="itemize" id='itemize2472'
><li>P is a non-literal identifier; or<br/><br/></li>
<li>P is a literal identifier and F is an identifier with the same
binding; or<br/><br/></li>
<li>P is a list <samp id='samp2452'
>(P_1 ... P_n)</samp> and F is a
list of n
forms that match P_1 through P_n, respectively; or<br/><br/></li>
<li>P is an improper list
<samp id='samp2455'
>(P_1 P_2 ... P_n . P_n+1)</samp>
and F is a list or
improper list of n or more forms that match P_1 through P_n,
respectively, and whose nth ``cdr'' matches P_n+1; or<br/><br/></li>
<li>P is of the form
<samp id='samp2458'
>(P_1 ... P_n P_n+1 &lt;ellipsis&gt;)</samp>
where &lt;ellipsis&gt; is the identifier <samp id='samp2459'
>...</samp>
and F is
a proper list of at least n forms, the first n of which match
P_1 through P_n, respectively, and each remaining element of F
matches P_n+1; or<br/><br/></li>
<li>P is a vector of the form <samp id='samp2462'
>#(P_1 ... P_n)</samp>
and F is a vector
of n forms that match P_1 through P_n; or<br/><br/></li>
<li>P is of the form
<samp id='samp2465'
>#(P_1 ... P_n P_n+1 &lt;ellipsis&gt;)</samp>
where &lt;ellipsis&gt; is the identifier <samp id='samp2466'
>...</samp>
and F is a vector of n
or more forms the first n of which match
P_1 through P_n, respectively, and each remaining element of F
matches P_n+1; or<br/><br/></li>
<li>P is a datum and F is equal to P in the sense of
the <samp id='samp2469'
>equal?</samp> procedure.<br/><br/></li>
</ul>
It is an error to use a macro keyword, within the scope of its
binding, in an expression that does not match any of the patterns.<br/><br/>When a macro use is transcribed according to the template of the
matching &lt;syntax rule&gt;, pattern variables that occur in the
template are replaced by the subforms they match in the input.
Pattern variables that occur in subpatterns followed by one or more
instances of the identifier
<samp id='samp2474'
>...</samp> are allowed only in subtemplates that are
followed by as many instances of <samp id='samp2475'
>...</samp>.
They are replaced in the
output by all of the subforms they match in the input, distributed as
indicated.  It is an error if the output cannot be built up as
specified.<br/><br/>
Identifiers that appear in the template but are not pattern variables
or the identifier
<samp id='samp2477'
>...</samp> are inserted into the output as literal identifiers.  If a
literal identifier is inserted as a free identifier then it refers to the
binding of that identifier within whose scope the instance of
<samp id='samp2478'
>syntax-rules</samp> appears.
If a literal identifier is inserted as a bound identifier then it is
in effect renamed to prevent inadvertent captures of free identifiers.<br/><br/>As an example, if <code id='code2480'
>let</code> and <code id='code2481'
>cond</code> are defined as in
<a name="g2483" class="mark"></a><a name="g2486" class="mark"></a>section <a href="r5rs-7.html#Derived-expression-types" class="inbound">Derived expression types</a> then they are hygienic (as required) and
the following is not an error.<br/><br/><center id='center2493'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2491'
>(<strong id='bold7772'
>let</strong> ((=&gt; #f))
  (<strong id='bold7773'
>cond</strong> (#t =&gt; 'ok)))                  =&gt; ok
</pre>
</td></tr>
</tbody></table></center>

The macro transformer for <samp id='samp2494'
>cond</samp> recognizes <samp id='samp2495'
>=&gt;</samp>
as a local variable, and hence an expression, and not as the
top-level identifier <samp id='samp2496'
>=&gt;</samp>, which the macro transformer treats
as a syntactic keyword.  Thus the example expands into<br/><br/><center id='center2503'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2501'
>(<strong id='bold7774'
>let</strong> ((=&gt; #f))
  (<strong id='bold7775'
>if</strong> #t (<strong id='bold7776'
>begin</strong> =&gt; 'ok)))
</pre>
</td></tr>
</tbody></table></center>

instead of<br/><br/><center id='center2510'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2508'
>(<strong id='bold7777'
>let</strong> ((=&gt; #f))
  (<strong id='bold7778'
>let</strong> ((temp #t))
    (<strong id='bold7779'
>if</strong> temp ('ok temp))))
</pre>
</td></tr>
</tbody></table></center>

which would result in an invalid procedure call.<br/><br/></td></tr>
</tbody></table><br/>         

</div>
</div><br>
</div></td>
</tr></table><div class="skribe-ending">
<hr> 
<p class="ending" id='paragraph7785'
><font size="-1">
This <span class="sc">Html</span> page has been produced by 
<a href="http://www.inria.fr/mimosa/fp/Skribe" class="http">Skribe</a>.
<br/>
Last update <em id='it7783'
>Tue Jun  2 11:43:24 2009</em>.</font></p></div>
</body>
</html>