547 lines
		
	
	
		
			24 KiB
		
	
	
	
		
			HTML
		
	
	
	
			
		
		
	
	
			547 lines
		
	
	
		
			24 KiB
		
	
	
	
		
			HTML
		
	
	
	
| <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
 | |
| <html>
 | |
| <!-- Generated from TeX source by tex2page, v 4o4, 
 | |
|      (c) Dorai Sitaram, http://www.cs.rice.edu/~dorai/tex2page -->
 | |
| <head>
 | |
| <title>
 | |
| Revised^5 Report on the Algorithmic Language Scheme
 | |
| </title>
 | |
| <link rel="stylesheet" type="text/css" href="r5rs-Z-C.css" title=default>
 | |
| <meta name=robots content="noindex,follow">
 | |
| </head>
 | |
| <body>
 | |
| 
 | |
| <p><div class=navigation>[Go to <span><a href="r5rs.html">first</a>, <a href="r5rs-Z-H-9.html">previous</a></span><span>, <a href="r5rs-Z-H-11.html">next</a></span> page<span>;   </span><span><a href="r5rs-Z-H-2.html#%_toc_start">contents</a></span><span><span>;   </span><a href="r5rs-Z-H-15.html#%_index_start">index</a></span>]</div><p>
 | |
| 
 | |
| <a name="%_chap_7"></a>
 | |
| <h1 class=chapter>
 | |
| <div class=chapterheading><a href="r5rs-Z-H-2.html#%_toc_%_chap_7">Chapter 7</a></div><p>
 | |
| <a href="r5rs-Z-H-2.html#%_toc_%_chap_7">Formal syntax and semantics</a></h1><p>
 | |
| 
 | |
| <p>
 | |
| 
 | |
| This chapter provides formal descriptions of what has already been
 | |
| described informally in previous chapters of this report.<p>
 | |
| 
 | |
| <p>
 | |
| 
 | |
| <a name="%_sec_7.1"></a>
 | |
| <h2><a href="r5rs-Z-H-2.html#%_toc_%_sec_7.1">7.1  Formal syntax</a></h2><p>
 | |
| 
 | |
| <p>
 | |
| 
 | |
| This section provides a formal syntax for Scheme written in an extended
 | |
| BNF.<p>
 | |
| 
 | |
| All spaces in the grammar are for legibility.  Case is insignificant;
 | |
| for example, <tt>#x1A</tt> and <tt>#X1a</tt> are equivalent.  <empty>
 | |
| stands for the empty string.<p>
 | |
| 
 | |
| The following extensions to BNF are used to make the description more
 | |
| concise:  <thing>* means zero or more occurrences of
 | |
| <thing>; and <thing><sup>+</sup> means at least one
 | |
| <thing>.<p>
 | |
| 
 | |
| <a name="%_sec_7.1.1"></a>
 | |
| <h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_7.1.1">7.1.1  Lexical structure</a></h3><p>
 | |
| 
 | |
| This section describes how individual tokens<a name="%_idx_636"></a> (identifiers,
 | |
| numbers, etc.) are formed from sequences of characters.  The following
 | |
| sections describe how expressions and programs are formed from sequences
 | |
| of tokens.<p>
 | |
| 
 | |
| <Intertoken space> may occur on either side of any token, but not
 | |
| within a token.<p>
 | |
| 
 | |
| Tokens which require implicit termination (identifiers, numbers,
 | |
| characters, and dot) may be terminated by any <delimiter>, but not
 | |
| necessarily by anything else.<p>
 | |
| 
 | |
| The following five characters are reserved for future extensions to the
 | |
| language: <tt><code class=verbatim>[</code> <code class=verbatim>]</code> <code class=verbatim>{</code> <code class=verbatim>}</code> <code class=verbatim>|</code></tt><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-2.gif" border="0"></div><p><p>
 | |
| 
 | |
| 
 | |
| <p>
 | |
| 
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-3.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-4.gif" border="0"></div><p><p>
 | |
| 
 | |
| The following rules for <num <em>R</em>>, <complex <em>R</em>>, <real
 | |
| <em>R</em>>, <ureal <em>R</em>>, <uinteger <em>R</em>>, and <prefix <em>R</em>>
 | |
| should be replicated for <em>R</em>  =  2, 8, 10,
 | |
| 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.
 | |
| <p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-5.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-6.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p>
 | |
| 
 | |
| <a name="%_sec_7.1.2"></a>
 | |
| <h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_7.1.2">7.1.2  External representations</a></h3><p>
 | |
| 
 | |
| <p>
 | |
| 
 | |
| <Datum> is what the <tt>read</tt> procedure (section <a href="r5rs-Z-H-9.html#%_sec_6.6.2">6.6.2</a>)
 | |
| successfully parses.  Note that any string that parses as an
 | |
| <expression> will also parse as a <datum>.  <p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-7.gif" border="0"></div><p><p>
 | |
| 
 | |
| <a name="%_sec_7.1.3"></a>
 | |
| <h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_7.1.3">7.1.3  Expressions</a></h3><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-8.gif" border="0"></div><p><p>
 | |
| 
 | |
| <a name="%_sec_7.1.4"></a>
 | |
| <h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_7.1.4">7.1.4  Quasiquotations</a></h3><p>
 | |
| 
 | |
| The following grammar for quasiquote expressions is not context-free.
 | |
| It is presented as a recipe for generating an infinite number of
 | |
| production rules.  Imagine a copy of the following rules for <em>D</em>  =  1, 2,
 | |
| 3, <tt>...</tt>.  <em>D</em> keeps track of the nesting depth.<p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-9.gif" border="0"></div><p><p>
 | |
| 
 | |
| In <quasiquotation>s, a <list qq template <em>D</em>> can sometimes
 | |
| be confused with either an <unquotation <em>D</em>> or a <splicing
 | |
| unquotation <em>D</em>>.  The interpretation as an
 | |
| <unquotation> or <splicing
 | |
| unquotation <em>D</em>> takes precedence.<p>
 | |
| 
 | |
| <a name="%_sec_7.1.5"></a>
 | |
| <h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_7.1.5">7.1.5  Transformers</a></h3><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-10.gif" border="0"></div><p><p>
 | |
| 
 | |
| <a name="%_sec_7.1.6"></a>
 | |
| <h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_7.1.6">7.1.6  Programs and definitions</a></h3><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-11.gif" border="0"></div><p>
 | |
|         <p><a name="%_sec_7.2"></a>
 | |
| <h2><a href="r5rs-Z-H-2.html#%_toc_%_sec_7.2">7.2  Formal semantics</a></h2><p>
 | |
| 
 | |
| <p>
 | |
| 
 | |
| <p>
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| <p>
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 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 [<a href="r5rs-Z-H-14.html#%_sec_7.3">29</a>]; the notation is
 | |
| summarized below:<p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-12.gif" border="0"></div><p><p>
 | |
| 
 | |
| The reason that expression continuations take sequences of values instead
 | |
| of single values is to simplify the formal treatment of procedure calls
 | |
| and multiple return values.<p>
 | |
| 
 | |
| The boolean flag associated with pairs, vectors, and strings will be true
 | |
| for mutable objects and false for immutable objects.<p>
 | |
| 
 | |
| The order of evaluation within a call is unspecified.  We mimic that
 | |
| here by applying arbitrary permutations <i>permute</i> and <i>unpermute</i>, which must be inverses, to the arguments in a call before
 | |
| and after they are evaluated.  This is not quite right since it suggests,
 | |
| incorrectly, that the order of evaluation is constant throughout a program (for
 | |
| any given number of arguments), but it is a closer approximation to the intended
 | |
| semantics than a left-to-right evaluation would be.<p>
 | |
| 
 | |
| The storage allocator <i>new</i> is implementation-dependent, but it must
 | |
| obey the following axiom:  if <i>new</i> <img src="r5rs-Z-G-D-2.gif" border="0"> <img src="r5rs-Z-G-D-4.gif" border="0"> <tt><em>L</em></tt>, then
 | |
| <img src="r5rs-Z-G-D-2.gif" border="0"> (<i>new</i> <img src="r5rs-Z-G-D-2.gif" border="0"> | <tt><em>L</em></tt>)<img src="r5rs-Z-G-D-7.gif" border="0"> 2  =  <i><em>f</em><em>a</em><em>l</em><em>s</em><em>e</em></i>.<p>
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| <p>
 | |
| 
 | |
| The definition of <img src="r5rs-Z-G-D-5.gif" border="0"> is omitted because an accurate definition of
 | |
| <img src="r5rs-Z-G-D-5.gif" border="0"> would complicate the semantics without being very interesting.<p>
 | |
| 
 | |
| If  P is a program in which all variables are defined before being
 | |
| referenced or assigned, then the meaning of  P is
 | |
| <p><div align=left><img src="r5rs-Z-G-13.gif" border="0"></div><p>
 | |
| where  I* is the sequence of variables defined in  P,  P'
 | |
| is the sequence of expressions obtained by replacing every definition
 | |
| in  P by an assignment, <undefined> is an expression that evaluates
 | |
| to <i>undefined</i>, and
 | |
| <img src="r5rs-Z-G-D-6.gif" border="0"> is the semantic function that assigns meaning to expressions.<p>
 | |
| 
 | |
| <p>
 | |
| 
 | |
| <a name="%_sec_7.2.1"></a>
 | |
| <h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_7.2.1">7.2.1  Abstract syntax</a></h3><p>
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| <p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-14.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-15.gif" border="0"></div><p><p>
 | |
| 
 | |
| <a name="%_sec_7.2.2"></a>
 | |
| <h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_7.2.2">7.2.2  Domain equations</a></h3><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-16.gif" border="0"></div><p><p>
 | |
| 
 | |
| <a name="%_sec_7.2.3"></a>
 | |
| <h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_7.2.3">7.2.3  Semantic functions</a></h3><p>
 | |
| 
 | |
| 
 | |
| 
 | |
| <p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-17.gif" border="0"></div><p><p>
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| <p>
 | |
| 
 | |
| <p>
 | |
| 
 | |
| <p><p><p>
 | |
| 
 | |
| Definition of <img src="r5rs-Z-G-D-5.gif" border="0"> deliberately omitted.<p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-18.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-19.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-20.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-21.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-22.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-23.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-24.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-25.gif" border="0"></div><p><p>
 | |
| 
 | |
| Here and elsewhere, any expressed value other than <i>undefined</i> may
 | |
| be used in place of <i>unspecified</i>.<p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-26.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-27.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-28.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-29.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-30.gif" border="0"></div><p><p>
 | |
| 
 | |
| 
 | |
| <a name="%_sec_7.2.4"></a>
 | |
| <h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_7.2.4">7.2.4  Auxiliary functions</a></h3><p>
 | |
| 
 | |
| <p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-31.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-32.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-33.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-34.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-35.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-36.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-37.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-38.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-39.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-40.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-41.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-42.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-43.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-44.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-45.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-46.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-47.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-48.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-49.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-50.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-51.gif" border="0"></div><p><p>
 | |
| 
 | |
| <a name="%_idx_638"></a>
 | |
| <p><div align=left><img src="r5rs-Z-G-52.gif" border="0"></div><p><p>
 | |
| 
 | |
| <a name="%_idx_640"></a>
 | |
| <p><div align=left><img src="r5rs-Z-G-53.gif" border="0"></div><p><p>
 | |
| 
 | |
| <a name="%_idx_642"></a>
 | |
| <p><div align=left><img src="r5rs-Z-G-54.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-55.gif" border="0"></div><p><p>
 | |
| 
 | |
| <a name="%_idx_644"></a>
 | |
| <p><div align=left><img src="r5rs-Z-G-56.gif" border="0"></div><p><p>
 | |
| 
 | |
| <a name="%_idx_646"></a>
 | |
| <p><div align=left><img src="r5rs-Z-G-57.gif" border="0"></div><p><p>
 | |
| 
 | |
| <a name="%_idx_648"></a>
 | |
| <p><div align=left><img src="r5rs-Z-G-58.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-59.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-60.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-61.gif" border="0"></div><p><p>
 | |
| 
 | |
| <p><div align=left><img src="r5rs-Z-G-62.gif" border="0"></div><p><p>
 | |
| 
 | |
| 
 | |
|         <p><a name="%_sec_7.3"></a>
 | |
| <h2><a href="r5rs-Z-H-2.html#%_toc_%_sec_7.3">7.3  Derived expression types</a></h2><p>
 | |
| 
 | |
| <p>
 | |
| 
 | |
| This section gives macro definitions for the derived expression types in
 | |
| terms of the primitive expression types (literal, variable, call, <tt>lambda</tt>,
 | |
| <tt>if</tt>, <tt>set!</tt>).  See section <a href="r5rs-Z-H-9.html#%_sec_6.4">6.4</a> for a possible
 | |
| definition of <tt>delay</tt>.<p>
 | |
| 
 | |
| <tt><p>(define-syntax <tt>cond</tt><br>
 | |
|   (syntax-rules (else =>)<br>
 | |
|     ((cond (else result1 result2 ...))<br>
 | |
|      (begin result1 result2 ...))<br>
 | |
|     ((cond (test => result))<br>
 | |
|      (let ((temp test))<br>
 | |
|        (if temp (result temp))))<br>
 | |
|     ((cond (test => result) clause1 clause2 ...)<br>
 | |
|      (let ((temp test))<br>
 | |
|        (if temp<br>
 | |
|            (result temp)<br>
 | |
|            (cond clause1 clause2 ...))))<br>
 | |
|     ((cond (test)) test)<br>
 | |
|     ((cond (test) clause1 clause2 ...)<br>
 | |
|      (let ((temp test))<br>
 | |
|        (if temp<br>
 | |
|            temp<br>
 | |
|            (cond clause1 clause2 ...))))<br>
 | |
|     ((cond (test result1 result2 ...))<br>
 | |
|      (if test (begin result1 result2 ...)))<br>
 | |
|     ((cond (test result1 result2 ...)<br>
 | |
|            clause1 clause2 ...)<br>
 | |
|      (if test<br>
 | |
|          (begin result1 result2 ...)<br>
 | |
|          (cond clause1 clause2 ...)))))<br>
 | |
| <p></tt><p>
 | |
| 
 | |
| <tt><p>(define-syntax <tt>case</tt><br>
 | |
|   (syntax-rules (else)<br>
 | |
|     ((case (key ...)<br>
 | |
|        clauses ...)<br>
 | |
|      (let ((atom-key (key ...)))<br>
 | |
|        (case atom-key clauses ...)))<br>
 | |
|     ((case key<br>
 | |
|        (else result1 result2 ...))<br>
 | |
|      (begin result1 result2 ...))<br>
 | |
|     ((case key<br>
 | |
|        ((atoms ...) result1 result2 ...))<br>
 | |
|      (if (memv key '(atoms ...))<br>
 | |
|          (begin result1 result2 ...)))<br>
 | |
|     ((case key<br>
 | |
|        ((atoms ...) result1 result2 ...)<br>
 | |
|        clause clauses ...)<br>
 | |
|      (if (memv key '(atoms ...))<br>
 | |
|          (begin result1 result2 ...)<br>
 | |
|          (case key clause clauses ...)))))<br>
 | |
| <p></tt><p>
 | |
| 
 | |
| <tt><p>(define-syntax <tt>and</tt><br>
 | |
|   (syntax-rules ()<br>
 | |
|     ((and) <tt>#t</tt>)<br>
 | |
|     ((and test) test)<br>
 | |
|     ((and test1 test2 ...)<br>
 | |
|      (if test1 (and test2 ...) <tt>#f</tt>))))<br>
 | |
| <p></tt><p>
 | |
| 
 | |
| <tt><p>(define-syntax <tt>or</tt><br>
 | |
|   (syntax-rules ()<br>
 | |
|     ((or) <tt>#f</tt>)<br>
 | |
|     ((or test) test)<br>
 | |
|     ((or test1 test2 ...)<br>
 | |
|      (let ((x test1))<br>
 | |
|        (if x x (or test2 ...))))))<br>
 | |
| <p></tt><p>
 | |
| 
 | |
| <tt><p>(define-syntax <tt>let</tt><br>
 | |
|   (syntax-rules ()<br>
 | |
|     ((let ((name val) ...) body1 body2 ...)<br>
 | |
|      ((lambda (name ...) body1 body2 ...)<br>
 | |
|       val ...))<br>
 | |
|     ((let tag ((name val) ...) body1 body2 ...)<br>
 | |
|      ((letrec ((tag (lambda (name ...)<br>
 | |
|                       body1 body2 ...)))<br>
 | |
|         tag)<br>
 | |
|       val ...))))<br>
 | |
| <p></tt><p>
 | |
| 
 | |
| <tt><p>(define-syntax <tt>let*</tt><br>
 | |
|   (syntax-rules ()<br>
 | |
|     ((let* () body1 body2 ...)<br>
 | |
|      (let () body1 body2 ...))<br>
 | |
|     ((let* ((name1 val1) (name2 val2) ...)<br>
 | |
|        body1 body2 ...)<br>
 | |
|      (let ((name1 val1))<br>
 | |
|        (let* ((name2 val2) ...)<br>
 | |
|          body1 body2 ...)))))<br>
 | |
| <p></tt><p>
 | |
| 
 | |
| The following <tt>letrec</tt> macro uses the symbol <tt><undefined></tt>
 | |
| 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.<p>
 | |
| 
 | |
| <tt><p>(define-syntax <tt>letrec</tt><br>
 | |
|   (syntax-rules ()<br>
 | |
|     ((letrec ((var1 init1) ...) body ...)<br>
 | |
|      (letrec "generate_temp_names"<br>
 | |
|        (var1 ...)<br>
 | |
|        ()<br>
 | |
|        ((var1 init1) ...)<br>
 | |
|        body ...))<br>
 | |
|     ((letrec "generate_temp_names"<br>
 | |
|        ()<br>
 | |
|        (temp1 ...)<br>
 | |
|        ((var1 init1) ...)<br>
 | |
|        body ...)<br>
 | |
|      (let ((var1 <undefined>) ...)<br>
 | |
|        (let ((temp1 init1) ...)<br>
 | |
|          (set! var1 temp1)<br>
 | |
|          ...<br>
 | |
|          body ...)))<br>
 | |
|     ((letrec "generate_temp_names"<br>
 | |
|        (x y ...)<br>
 | |
|        (temp ...)<br>
 | |
|        ((var1 init1) ...)<br>
 | |
|        body ...)<br>
 | |
|      (letrec "generate_temp_names"<br>
 | |
|        (y ...)<br>
 | |
|        (newtemp temp ...)<br>
 | |
|        ((var1 init1) ...)<br>
 | |
|        body ...))))<br>
 | |
| <p></tt><p>
 | |
| 
 | |
| <tt><p>(define-syntax <tt>begin</tt><br>
 | |
|   (syntax-rules ()<br>
 | |
|     ((begin exp ...)<br>
 | |
|      ((lambda () exp ...)))))<br>
 | |
| <p></tt><p>
 | |
| 
 | |
| The following alternative expansion for <tt>begin</tt> 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 <tt>begin</tt> contains no definitions.<p>
 | |
| 
 | |
| <tt><p>(define-syntax begin<br>
 | |
|   (syntax-rules ()<br>
 | |
|     ((begin exp)<br>
 | |
|      exp)<br>
 | |
|     ((begin exp1 exp2 ...)<br>
 | |
|      (let ((x exp1))<br>
 | |
|        (begin exp2 ...)))))<br>
 | |
| <p></tt><p>
 | |
| 
 | |
| The following definition
 | |
| of <tt>do</tt> uses a trick to expand the variable clauses.
 | |
| As with <tt>letrec</tt> above, an auxiliary macro would also work.
 | |
| The expression <tt>(if #f #f)</tt> is used to obtain an unspecific
 | |
| value.<p>
 | |
| 
 | |
| <tt><p>(define-syntax <tt>do</tt><br>
 | |
|   (syntax-rules ()<br>
 | |
|     ((do ((var init step ...) ...)<br>
 | |
|          (test expr ...)<br>
 | |
|          command ...)<br>
 | |
|      (letrec<br>
 | |
|        ((loop<br>
 | |
|          (lambda (var ...)<br>
 | |
|            (if test<br>
 | |
|                (begin<br>
 | |
|                  (if #f #f)<br>
 | |
|                  expr ...)<br>
 | |
|                (begin<br>
 | |
|                  command<br>
 | |
|                  ...<br>
 | |
|                  (loop (do "step" var step ...)<br>
 | |
|                        ...))))))<br>
 | |
|        (loop init ...)))<br>
 | |
|     ((do "step" x)<br>
 | |
|      x)<br>
 | |
|     ((do "step" x y)<br>
 | |
|      y)))<br>
 | |
| <p></tt><p>
 | |
| 
 | |
|         <p>
 | |
|   
 | |
| <p><div class=navigation>[Go to <span><a href="r5rs.html">first</a>, <a href="r5rs-Z-H-9.html">previous</a></span><span>, <a href="r5rs-Z-H-11.html">next</a></span> page<span>;   </span><span><a href="r5rs-Z-H-2.html#%_toc_start">contents</a></span><span><span>;   </span><a href="r5rs-Z-H-15.html#%_index_start">index</a></span>]</div><p>
 | |
| 
 | |
| </body>
 | |
| </html>
 |