<!-- 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>7. R5Rs<br/>Scheme Revised(5) Report on the Algorithmic Language Scheme -- Formal syntax and semantics</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='center8154' ><table width="97%" border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse;" frame="box" rules="none"><tbody> <tr bgcolor="#8381de"><th id="tc8144" align="center" colspan="1"><font color="#ffffff"><strong id='bold8142' >main page</strong></font></th></tr> <tr bgcolor="#ffffff"><td id="tc8151" align="center" colspan="1"><table width="100%" border="0" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc8147" align="left" valign="top" colspan="1"><strong id='bold8146' >top:</strong></td><td id="tc8148" 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='center8164' ><table width="97%" border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse;" frame="box" rules="none"><tbody> <tr bgcolor="#8381de"><th id="tc8158" align="center" colspan="1"><font color="#ffffff"><strong id='bold8156' >Formal syntax and semantics</strong></font></th></tr> <tr bgcolor="#ffffff"><td id="tc8161" align="center" colspan="1"><table cellspacing="1" cellpadding="1" width="100%" class="toc"> <tbody> <tr><td valign="top" align="left">7.1</td><td colspan="4" width="100%"><a href="r5rs-10.html#Formal-syntax">Formal syntax</a></td></tr> <tr><td></td><td valign="top" align="left">7.1.1</td><td colspan="3" width="100%"><a href="r5rs-10.html#Lexical-structure">Lexical structure</a></td></tr> <tr><td></td><td valign="top" align="left">7.1.2</td><td colspan="3" width="100%"><a href="r5rs-10.html#External-representations">External representations</a></td></tr> <tr><td></td><td valign="top" align="left">7.1.3</td><td colspan="3" width="100%"><a href="r5rs-10.html#Expressions">Expressions</a></td></tr> <tr><td></td><td valign="top" align="left">7.1.4</td><td colspan="3" width="100%"><a href="r5rs-10.html#Quasiquotations">Quasiquotations</a></td></tr> <tr><td></td><td valign="top" align="left">7.1.5</td><td colspan="3" width="100%"><a href="r5rs-10.html#Transformers">Transformers</a></td></tr> <tr><td></td><td valign="top" align="left">7.1.6</td><td colspan="3" width="100%"><a href="r5rs-10.html#Programs-and-definitions">Programs and definitions</a></td></tr> <tr><td valign="top" align="left">7.2</td><td colspan="4" width="100%"><a href="r5rs-10.html#Formal-semantics">Formal semantics</a></td></tr> <tr><td valign="top" align="left">7.3</td><td colspan="4" width="100%"><a href="r5rs-10.html#Derived-expression-types">Derived expression types</a></td></tr> </tbody> </table> </td></tr> </tbody></table> </center> <br/><br/><center id='center8174' ><table width="97%" border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse;" frame="box" rules="none"><tbody> <tr bgcolor="#8381de"><th id="tc8168" align="center" colspan="1"><font color="#ffffff"><strong id='bold8166' >Chapters</strong></font></th></tr> <tr bgcolor="#ffffff"><td id="tc8171" 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="Formal-syntax-and-semantics" class="mark"></a> This chapter provides formal descriptions of what has already been described informally in previous chapters of this report.<br/><br/> <!-- Formal syntax --> <a name="Formal-syntax"></a> <div class="section-atitle"><table width="100%"><tr><td bgcolor="#dedeff"><h3><font color="black">7.1 Formal syntax</font> </h3></td></tr></table> </div><div class="section"> <a name="Formal-syntax" class="mark"></a> This section provides a formal syntax for Scheme written in an extended BNF.<br/><br/>All spaces in the grammar are for legibility. Case is insignificant; for example, <samp id='samp6767' >#x1A</samp> and <samp id='samp6768' >#X1a</samp> are equivalent. <empty> stands for the empty string.<br/><br/>The following extensions to BNF are used to make the description more concise: <thing>* means zero or more occurrences of <thing>; and <thing>+ means at least one <thing>.<br/><br/><!-- Lexical structure --> <a name="Lexical-structure"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">7.1.1 Lexical structure</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Lexical-structure" class="mark"></a> This section describes how individual tokens (identifiers, <a name="g6772" class="mark"></a>numbers, etc.) are formed from sequences of characters. The following sections describe how expressions and programs are formed from sequences of tokens.<br/><br/><Intertoken space> may occur on either side of any token, but not within a token.<br/><br/>Tokens which require implicit termination (identifiers, numbers, characters, and dot) may be terminated by any <delimiter>, but not necessarily by anything else.<br/><br/>The following five characters are reserved for future extensions to the language: <tt id='tt6777' >[ ] { } |</tt><br/><br/><br/><center id='center6792' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ccccff"><pre class="prog" id='prog6790' ><tt id='tt6788' ><token> --> <identifier> | <boolean> | <number> <a name="g6780" class="mark"></a> | <character> | <string> | ( | ) | #( | <tt id='tt6782' >'</tt> | <tt id='tt6783' >`</tt> | , | ,@ | <strong id='bold6784' >.</strong> <delimiter> --> <whitespace> | ( | ) | " | ; <whitespace> --> <space or newline> <comment> --> ; <all subsequent characters up to a line break> <a name="g6786" class="mark"></a><atmosphere> --> <whitespace> | <comment> <intertoken space> --> <atmosphere>*</tt><br/><br/> </pre> </td></tr> </tbody></table></center> <center id='center6811' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ccccff"><pre class="prog" id='prog6809' ><tt id='tt6808' ><identifier> --> <initial> <subsequent>* | <peculiar identifier> <initial> --> <letter> | <special initial> <letter> --> a | b | c | ... | z<br/><br/><special initial> --> ! | $ | % | & | * | / | : | < | = | > | ? | ^ | _ | ~ <subsequent> --> <initial> | <digit> | <special subsequent> <digit> --> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 <special subsequent> --> + | - | .: | @ <peculiar identifier> --> + | - | ... <syntactic keyword> --> <expression keyword> <a name="g6795" class="mark"></a><a name="g6798" class="mark"></a> | else | => | define | unquote | unquote-splicing <expression keyword> --> quote | lambda | if | set! | begin | cond | and | or | case | let | let* | letrec | do | delay | quasiquote<br/><br/><strong id='bold6802' ><samp id='samp6801' ><variable> => <</samp></strong>any <identifier> that isn't <a name="g6804" class="mark"></a> <strong id='bold6806' > also a <syntactic keyword>></strong><br/><br/><boolean> --> #t | #f <character> --> #\ <any character> | #\ <character name> <character name> --> space | newline <string> --> " <string element>* " <string element> --> <any character other than " or \> | \" | \\ </tt> </pre> </td></tr> </tbody></table></center> <center id='center6815' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ccccff"><pre class="prog" id='prog6813' ><tt id='tt6812' ><number> --> <num 2>| <num 8> | <num 10>| <num 16> </tt> </pre> </td></tr> </tbody></table></center> The following rules for <num R>, <complex R>, <real R>, <ureal R>, <uinteger R>, and <prefix R> should be replicated for <strong id='bold6816' >R = 2, 8, 10,</strong> and 16. There are no rules for <decimal 2>, <decimal 8>, and <decimal 16>, which means that numbers containing decimal points or exponents must be in decimal radix. <center id='center6820' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ccccff"><pre class="prog" id='prog6818' ><tt id='tt6817' ><num R> --> <prefix R> <complex R> <complex R> --> <real R> | <real R> @ <real R> | <real R> + <ureal R> i | <real R> - <ureal R> i | <real R> + i | <real R> - i | + <ureal R> i | - <ureal R> i | + i | - i <real R> --> <sign> <ureal R> <ureal R> --> <uinteger R> | <uinteger R> / <uinteger R> | <decimal R> <decimal 10> --> <uinteger 10> <suffix> | . <digit 10>+ #* <suffix> | <digit 10>+ . <digit 10>* #* <suffix> | <digit 10>+ #+ . #* <suffix> <uinteger R> --> <digit R>+ #* <prefix R> --> <radix R> <exactness> | <exactness> <radix R> </tt> </pre> </td></tr> </tbody></table></center> <center id='center6834' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ccccff"><pre class="prog" id='prog6832' ><tt id='tt6831' ><suffix> --> <empty> | <exponent marker> <sign> <digit 10>+ <exponent marker> --> e | s | f | d | l <sign> --> <empty> | + | - <exactness> --> <empty> | #i | #e <a name="g6821" class="mark"></a><a name="g6823" class="mark"></a><radix 2> --> #b <a name="g6825" class="mark"></a><radix 8> --> #o <a name="g6827" class="mark"></a><radix 10> --> <empty> | #d <radix 16> --> #x <a name="g6829" class="mark"></a><digit 2> --> 0 | 1 <digit 8> --> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 <digit 10> --> <digit> <digit 16> --> <digit 10> | a | b | c | d | e | f </tt> </pre> </td></tr> </tbody></table></center> </div> <!-- External representations --> <a name="External-representations"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">7.1.2 External representations</font> </h3></td></tr></table> </div><div class="subsection"> <a name="External-representation" class="mark"></a> <Datum> is what the <code id='code6835' >read</code> procedure (section <a href="r5rs-9.html#Input" class="inbound">Input</a>) <a name="g6837" class="mark"></a>successfully parses. Note that any string that parses as an <expression> will also parse as a <datum>. <br/><br/><center id='center6843' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ccccff"><pre class="prog" id='prog6841' ><tt id='tt6840' ><datum> --> <simple datum> | <compound datum> <simple datum> --> <boolean> | <number> | <character> | <string> | <symbol> <symbol> --> <identifier> <compound datum> --> <list> | <vector> <list> --> (<datum>*) | (<datum>+ .: <datum>) | <abbreviation> <abbreviation> --> <abbrev prefix> <datum> <abbrev prefix> --> ' | ` | , | ,@ <vector> --> #(<datum>*) </tt> </pre> </td></tr> </tbody></table></center> </div> <!-- Expressions --> <a name="Expressions"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">7.1.3 Expressions</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Expression" class="mark"></a> <center id='center6856' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ccccff"><pre class="prog" id='prog6854' ><tt id='tt6853' ><expression> --> <variable> | <literal> | <procedure call> | <lambda expression> | <conditional> | <assignment> | <derived expression> | <macro use> | <macro block><br/><br/><literal> --> <quotation> | <self-evaluating> <self-evaluating> --> <boolean> | <number> | <character> | <string> <quotation> --> '<datum> | (quote <datum>) <procedure call> --> (<operator> <operand>*) <operator> --> <expression> <operand> --> <expression><br/><br/><lambda expression> --> (lambda <formals> <body>) <formals> --> (<variable>*) | <variable> | (<variable>+ .: <variable>) <body> --> <definition>* <sequence> <sequence> --> <command>* <expression> <command> --> <expression><br/><br/><conditional> --> (if <test> <consequent> <alternate>) <test> --> <expression> <consequent> --> <expression> <alternate> --> <expression> | <empty><br/><br/><assignment> --> (set! <variable> <expression>)<br/><br/><derived expression> --> (cond <cond clause>+) | (cond <cond clause>* (else <sequence>)) | (case <expression> <case clause>+) | (case <expression> <case clause>* (else <sequence>)) | (and <test>*) | (or <test>*) | (let (<binding spec>*) <body>) | (let <variable> (<binding spec>*) <body>) | (let* (<binding spec>*) <body>) | (letrec (<binding spec>*) <body>) | (begin <sequence>) | (do (<iteration spec>*) (<test> <do result>) <command>*) | (delay <expression>) | <quasiquotation><br/><br/><cond clause> --> (<test> <sequence>) | (<test>) | (<test> => <recipient>) <recipient> --> <expression> <case clause> --> ((<datum>*) <sequence>) <binding spec> --> (<variable> <expression>) <iteration spec> --> (<variable> <init> <step>) | (<variable> <init>) <init> --> <expression> <step> --> <expression> <do result> --> <sequence> | <empty><br/><br/><macro use> --> (<keyword> <datum>*) <keyword> --> <identifier><br/><br/><macro block> --> (let-syntax (<syntax spec>*) <body>) | (letrec-syntax (<syntax spec>*) <body>) <syntax spec> --> (<keyword> <transformer spec>)<br/><br/></tt> </pre> </td></tr> </tbody></table></center> </div> <!-- Quasiquotations --> <a name="Quasiquotations"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">7.1.4 Quasiquotations</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Quasiquotations" class="mark"></a> 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 D = 1, 2,3, .... D keeps track of the nesting depth.<br/><br/><center id='center6861' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ccccff"><pre class="prog" id='prog6859' ><tt id='tt6858' ><quasiquotation> --> <quasiquotation 1> <qq template 0> --> <expression> <quasiquotation D> --> `<qq template D> | (quasiquote <qq template D>) <qq template D> --> <simple datum> | <list qq template D> | <vector qq template D> | <unquotation D> <list qq template D> --> (<qq template or splice D>*) | (<qq template or splice D>+ .: <qq template D>) | '<qq template D> | <quasiquotation D+1> <vector qq template D> --> #(<qq template or splice D>*) <unquotation D> --> ,<qq template D-1> | (unquote <qq template D-1>) <qq template or splice D> --> <qq template D> | <splicing unquotation D> <splicing unquotation D> --> ,@<qq template D-1> | (unquote-splicing <qq template D-1>) </tt> </pre> </td></tr> </tbody></table></center> In <quasiquotation>s, a <list qq template D> can sometimes be confused with either an <unquotation D> or a <splicing unquotation D>. The interpretation as an <unquotation> or <splicing unquotation D> takes precedence.<br/><br/></div> <!-- Transformers --> <a name="Transformers"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">7.1.5 Transformers</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Transformers" class="mark"></a> <center id='center6868' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ccccff"><pre class="prog" id='prog6866' ><tt id='tt6865' ><transformer spec> --> (syntax-rules (<identifier>*) <syntax rule>*) <syntax rule> --> (<pattern> <template>) <pattern> --> <pattern identifier> | (<pattern>*) | (<pattern>+ . <pattern>) | (<pattern>* <pattern> <ellipsis>) | #(<pattern>*) | #(<pattern>* <pattern> <ellipsis>) | <pattern datum> <pattern datum> --> <string> | <character> | <boolean> | <number> <template> --> <pattern identifier> | (<template element>*) | (<template element>+ . <template>) | #(<template element>*) | <template datum> <template element> --> <template> | <template> <ellipsis> <template datum> --> <pattern datum> <pattern identifier> --> <any identifier except <samp id='samp6863' >...</samp>> <ellipsis> --> <the identifier <samp id='samp6864' >...</samp>> </tt> </pre> </td></tr> </tbody></table></center> </div> <!-- Programs and definitions --> <a name="Programs-and-definitions"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">7.1.6 Programs and definitions</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Programs-and-definitions" class="mark"></a> <center id='center6872' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ccccff"><pre class="prog" id='prog6870' ><tt id='tt6869' ><program> --> <command or definition>* <command or definition> --> <command> | <definition> | <syntax definition> | (begin <command or definition>+) <definition> --> (define <variable> <expression>) | (define (<variable> <def formals>) <body>) | (begin <definition>*) <def formals> --> <variable>* | <variable>* .: <variable> <syntax definition> --> (define-syntax <keyword> <transformer spec>) </tt> </pre> </td></tr> </tbody></table></center> </div> </div><br> <!-- Formal semantics --> <a name="Formal-semantics"></a> <div class="section-atitle"><table width="100%"><tr><td bgcolor="#dedeff"><h3><font color="black">7.2 Formal semantics</font> </h3></td></tr></table> </div><div class="section"> <a name="Formal-semantics" class="mark"></a> This section provides a formal denotational semantics for the primitive expressions of Scheme and selected built-in procedures. The concepts and notation used here are described in <span class="sc">[Stoy77]</span>.<br/><br/><font size="-1"><em id='emph6875' >Note:</em> The formal semantics section was written in LaTeX which is incompatible with TeXinfo. See the Formal semantics section of the original document from which this was derived. </font> </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">7.3 Derived expression types</font> </h3></td></tr></table> </div><div class="section"> <a name="Derived-expression-type" class="mark"></a> This section gives macro definitions for the derived expression types in terms of the primitive expression types (literal, variable, call, <samp id='samp6877' >lambda</samp>, <samp id='samp6878' >if</samp>, <samp id='samp6879' >set!</samp>). See section <a href="r5rs-9.html#Control-features" class="inbound">Control features</a> for a possible definition of <samp id='samp6880' >delay</samp>.<br/><br/><center id='center6907' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog6905' >(<font color="#6959cf"><strong id='bold8175' >define-syntax</strong></font> <font color="#6959cf"><strong id='bold8177' >cond</strong></font> (<strong id='bold8179' >syntax-rules</strong> (else =>) ((<strong id='bold8180' >cond</strong> (else result1 result2 ...)) (<strong id='bold8181' >begin</strong> result1 result2 ...)) ((<strong id='bold8182' >cond</strong> (test => result)) (<strong id='bold8183' >let</strong> ((temp test)) (<strong id='bold8184' >if</strong> temp (result temp)))) ((<strong id='bold8185' >cond</strong> (test => result) clause1 clause2 ...) (<strong id='bold8186' >let</strong> ((temp test)) (<strong id='bold8187' >if</strong> temp (result temp) (<strong id='bold8188' >cond</strong> clause1 clause2 ...)))) ((<strong id='bold8189' >cond</strong> (test)) test) ((<strong id='bold8190' >cond</strong> (test) clause1 clause2 ...) (<strong id='bold8191' >let</strong> ((temp test)) (<strong id='bold8192' >if</strong> temp temp (<strong id='bold8193' >cond</strong> clause1 clause2 ...)))) ((<strong id='bold8194' >cond</strong> (test result1 result2 ...)) (<strong id='bold8195' >if</strong> test (<strong id='bold8196' >begin</strong> result1 result2 ...))) ((<strong id='bold8197' >cond</strong> (test result1 result2 ...) clause1 clause2 ...) (<strong id='bold8198' >if</strong> test (<strong id='bold8199' >begin</strong> result1 result2 ...) (<strong id='bold8200' >cond</strong> clause1 clause2 ...))))) </pre> </td></tr> </tbody></table></center> <center id='center6924' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog6922' >(<font color="#6959cf"><strong id='bold8201' >define-syntax</strong></font> <font color="#6959cf"><strong id='bold8203' >case</strong></font> (<strong id='bold8205' >syntax-rules</strong> (else) ((<strong id='bold8206' >case</strong> (key ...) clauses ...) (<strong id='bold8207' >let</strong> ((atom-key (key ...))) (<strong id='bold8208' >case</strong> atom-key clauses ...))) ((<strong id='bold8209' >case</strong> key (else result1 result2 ...)) (<strong id='bold8210' >begin</strong> result1 result2 ...)) ((<strong id='bold8211' >case</strong> key ((atoms ...) result1 result2 ...)) (<strong id='bold8212' >if</strong> (memv key '(atoms ...)) (<strong id='bold8213' >begin</strong> result1 result2 ...))) ((<strong id='bold8214' >case</strong> key ((atoms ...) result1 result2 ...) clause clauses ...) (<strong id='bold8215' >if</strong> (memv key '(atoms ...)) (<strong id='bold8216' >begin</strong> result1 result2 ...) (<strong id='bold8217' >case</strong> key clause clauses ...))))) </pre> </td></tr> </tbody></table></center> <center id='center6930' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog6928' >(<font color="#6959cf"><strong id='bold8218' >define-syntax</strong></font> <font color="#6959cf"><strong id='bold8220' >and</strong></font> (<strong id='bold8222' >syntax-rules</strong> () ((and) #t) ((and test) test) ((and test1 test2 ...) (<strong id='bold8223' >if</strong> test1 (and test2 ...) #f)))) </pre> </td></tr> </tbody></table></center> <center id='center6937' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog6935' >(<font color="#6959cf"><strong id='bold8224' >define-syntax</strong></font> <font color="#6959cf"><strong id='bold8226' >or</strong></font> (<strong id='bold8228' >syntax-rules</strong> () ((or) #f) ((or test) test) ((or test1 test2 ...) (<strong id='bold8229' >let</strong> ((x test1)) (<strong id='bold8230' >if</strong> x x (or test2 ...)))))) </pre> </td></tr> </tbody></table></center> <center id='center6947' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog6945' >(<font color="#6959cf"><strong id='bold8231' >define-syntax</strong></font> <font color="#6959cf"><strong id='bold8233' >let</strong></font> (<strong id='bold8235' >syntax-rules</strong> () ((<strong id='bold8236' >let</strong> ((name val) ...) body1 body2 ...) ((<strong id='bold8237' >lambda</strong> (name ...) body1 body2 ...) val ...)) ((<strong id='bold8238' >let</strong> tag ((name val) ...) body1 body2 ...) ((<strong id='bold8239' >letrec</strong> ((tag (<strong id='bold8240' >lambda</strong> (name ...) body1 body2 ...))) tag) val ...)))) </pre> </td></tr> </tbody></table></center> <center id='center6957' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog6955' >(<font color="#6959cf"><strong id='bold8241' >define-syntax</strong></font> <font color="#6959cf"><strong id='bold8243' >let*</strong></font> (<strong id='bold8245' >syntax-rules</strong> () ((<strong id='bold8246' >let*</strong> () body1 body2 ...) (<strong id='bold8247' >let</strong> () body1 body2 ...)) ((<strong id='bold8248' >let*</strong> ((name1 val1) (name2 val2) ...) body1 body2 ...) (<strong id='bold8249' >let</strong> ((name1 val1)) (<strong id='bold8250' >let*</strong> ((name2 val2) ...) body1 body2 ...))))) </pre> </td></tr> </tbody></table></center> The following <samp id='samp6958' >letrec</samp> macro uses the symbol <samp id='samp6959' ><undefined></samp> in place of an expression which returns something that when stored in a location makes it an error to try to obtain the value stored in the location (no such expression is defined in Scheme). A trick is used to generate the temporary names needed to avoid specifying the order in which the values are evaluated. This could also be accomplished by using an auxiliary macro.<br/><br/><center id='center6977' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog6975' >(<font color="#6959cf"><strong id='bold8251' >define-syntax</strong></font> <font color="#6959cf"><strong id='bold8253' >letrec</strong></font> (<strong id='bold8255' >syntax-rules</strong> () ((<strong id='bold8256' >letrec</strong> ((var1 init1) ...) body ...) (<strong id='bold8257' >letrec</strong> <font color="red">"generate temp names"</font> (var1 ...) () ((var1 init1) ...) body ...)) ((<strong id='bold8259' >letrec</strong> <font color="red">"generate temp names"</font> () (temp1 ...) ((var1 init1) ...) body ...) (<strong id='bold8261' >let</strong> ((var1 <undefined>) ...) (<strong id='bold8262' >let</strong> ((temp1 init1) ...) (<strong id='bold8263' >set!</strong> var1 temp1) ... body ...))) ((<strong id='bold8264' >letrec</strong> <font color="red">"generate temp names"</font> (x y ...) (temp ...) ((var1 init1) ...) body ...) (<strong id='bold8266' >letrec</strong> <font color="red">"generate temp names"</font> (y ...) (newtemp temp ...) ((var1 init1) ...) body ...)))) </pre> </td></tr> </tbody></table></center> <center id='center6984' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog6982' >(<font color="#6959cf"><strong id='bold8268' >define-syntax</strong></font> <font color="#6959cf"><strong id='bold8270' >begin</strong></font> (<strong id='bold8272' >syntax-rules</strong> () ((<strong id='bold8273' >begin</strong> exp ...) ((<strong id='bold8274' >lambda</strong> () exp ...))))) </pre> </td></tr> </tbody></table></center> The following alternative expansion for <samp id='samp6985' >begin</samp> does not make use of the ability to write more than one expression in the body of a lambda expression. In any case, note that these rules apply only if the body of the <samp id='samp6986' >begin</samp> contains no definitions.<br/><br/><center id='center6996' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog6994' >(<font color="#6959cf"><strong id='bold8275' >define-syntax</strong></font> <font color="#6959cf"><strong id='bold8277' >begin</strong></font> (<strong id='bold8279' >syntax-rules</strong> () ((<strong id='bold8280' >begin</strong> exp) exp) ((<strong id='bold8281' >begin</strong> exp1 exp2 ...) (<strong id='bold8282' >let</strong> ((x exp1)) (<strong id='bold8283' >begin</strong> exp2 ...))))) </pre> </td></tr> </tbody></table></center> The following definition of <samp id='samp6997' >do</samp> uses a trick to expand the variable clauses. As with <samp id='samp6998' >letrec</samp> above, an auxiliary macro would also work. The expression <samp id='samp6999' >(if #f #f)</samp> is used to obtain an unspecific value.<br/><br/><center id='center7014' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog7012' >(<font color="#6959cf"><strong id='bold8284' >define-syntax</strong></font> <font color="#6959cf"><strong id='bold8286' >do</strong></font> (<strong id='bold8288' >syntax-rules</strong> () ((do ((var init step ...) ...) (test expr ...) command ...) (<strong id='bold8289' >letrec</strong> ((loop (<strong id='bold8290' >lambda</strong> (var ...) (<strong id='bold8291' >if</strong> test (<strong id='bold8292' >begin</strong> (<strong id='bold8293' >if</strong> #f #f) expr ...) (<strong id='bold8294' >begin</strong> command ... (loop (do <font color="red">"step"</font> var step ...) ...)))))) (loop init ...))) ((do <font color="red">"step"</font> x) x) ((do <font color="red">"step"</font> x y) y))) </pre> </td></tr> </tbody></table></center> </div><br> </div></td> </tr></table><div class="skribe-ending"> <hr> <p class="ending" id='paragraph8303' ><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='it8301' >Tue Jun 2 11:43:24 2009</em>.</font></p></div> </body> </html>