⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 r5rs-z-h-7.html

📁 scheme 标准(r5rs)。Scheme是MIT发布的基于Lambda运算的语言
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<a name="%_sec_4.3.1"></a><h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_4.3.1">4.3.1&nbsp;&nbsp;Binding constructs for syntactic keywords</a></h3><p><p><tt>Let-syntax</tt> and <tt>letrec-syntax</tt> areanalogous to <tt>let</tt> and <tt>letrec</tt>, but they bindsyntactic keywords to macro transformers instead of binding variablesto locations that contain values.  Syntactic keywords may also bebound at top level; see section&nbsp;<a href="r5rs-Z-H-8.html#%_sec_5.3">5.3</a>.<p><p><div align=left><u>syntax:</u>&nbsp;&nbsp;<tt>(<a name="%_idx_180"></a>let-syntax<i> &lt;bindings&gt; &lt;body&gt;</i>)</tt>&nbsp;</div><p><em>Syntax: </em>&lt;Bindings&gt; should have the form<tt><p>((&lt;keyword&gt;&nbsp;&lt;transformer&nbsp;spec&gt;)&nbsp;<tt>...</tt>)<p></tt>Each &lt;keyword&gt; is an identifier,each &lt;transformer spec&gt; is an instance of <tt>syntax-rules</tt>, and&lt;body&gt; should be a sequence of one or more expressions.  It is an errorfor a &lt;keyword&gt; to appear more than once in the list of keywordsbeing bound.<p><em>Semantics: </em>The &lt;body&gt; is expanded in the syntactic environmentobtained by extending the syntactic environment of the<tt>let-syntax</tt> expression with macros whose keywords arethe &lt;keyword&gt;s, bound to the specified transformers.Each binding of a &lt;keyword&gt; has &lt;body&gt; as its region.<p><tt><p>(let-syntax&nbsp;((when&nbsp;(syntax-rules&nbsp;()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((when&nbsp;test&nbsp;stmt1&nbsp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let&nbsp;((x&nbsp;'inner))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(m))))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;===&gt;&nbsp;&nbsp;outer<p></tt><p><p><p><p><div align=left><u>syntax:</u>&nbsp;&nbsp;<tt>(<a name="%_idx_182"></a>letrec-syntax<i> &lt;bindings&gt; &lt;body&gt;</i>)</tt>&nbsp;</div><p><em>Syntax: </em>Same as for <tt>let-syntax</tt>.<p><em>Semantics: </em>The &lt;body&gt; is expanded in the syntactic environment obtained byextending the syntactic environment of the <tt>letrec-syntax</tt>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 cantranscribe expressions into uses of the macrosintroduced by the <tt>letrec-syntax</tt> expression.<p><tt><p>(letrec-syntax<br>&nbsp;&nbsp;((my-or&nbsp;(syntax-rules&nbsp;()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((my-or)&nbsp;<tt>#f</tt>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((my-or&nbsp;e)&nbsp;e)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((my-or&nbsp;e1&nbsp;e2&nbsp;...)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb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bsp;&nbsp;&nbsp;===&gt;&nbsp;&nbsp;7<p></tt><p><p><p><a name="%_sec_4.3.2"></a><h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_4.3.2">4.3.2&nbsp;&nbsp;Pattern language</a></h3><p><p>A &lt;transformer spec&gt; has the following form:<p><p><div align=left><u>:</u>&nbsp;&nbsp;<tt>(<a name="%_idx_184"></a>syntax-rules<i> &lt;literals&gt; &lt;syntax rule&gt; <tt>...</tt></i>)</tt>&nbsp;</div><p><em>Syntax: </em>&lt;Literals&gt; is a list of identifiers and each &lt;syntax rule&gt;should be of the form<tt><p>(&lt;pattern&gt;&nbsp;&lt;template&gt;)<p></tt>The &lt;pattern&gt; in a &lt;syntax rule&gt; is a list &lt;pattern&gt;that begins with the keyword for the macro.<p>A &lt;pattern&gt; is either an identifier, a constant, or one of thefollowing<tt><p>(&lt;pattern&gt;&nbsp;<tt>...</tt>)<br>(&lt;pattern&gt;&nbsp;&lt;pattern&gt;&nbsp;<tt>...</tt>&nbsp;.&nbsp;&lt;pattern&gt;)<br>(&lt;pattern&gt;&nbsp;<tt>...</tt>&nbsp;&lt;pattern&gt;&nbsp;&lt;ellipsis&gt;)<br>#(&lt;pattern&gt;&nbsp;<tt>...</tt>)<br>#(&lt;pattern&gt;&nbsp;<tt>...</tt>&nbsp;&lt;pattern&gt;&nbsp;&lt;ellipsis&gt;)<p></tt>and a template is either an identifier, a constant, or one of the following<tt><p>(&lt;element&gt;&nbsp;<tt>...</tt>)<br>(&lt;element&gt;&nbsp;&lt;element&gt;&nbsp;<tt>...</tt>&nbsp;.&nbsp;&lt;template&gt;)<br>#(&lt;element&gt;&nbsp;<tt>...</tt>)<p></tt>where an &lt;element&gt; is a &lt;template&gt; optionallyfollowed by an &lt;ellipsis&gt; andan &lt;ellipsis&gt; is the identifier ``<tt>...</tt>'' (which cannot be used asan identifier in either a template or a pattern).<a name="%_idx_186"></a><p><em>Semantics: </em>An instance of <tt>syntax-rules</tt> produces a new macrotransformer by specifying a sequence of hygienic rewrite rules.  A useof a macro whose keyword is associated with a transformer specified by<tt>syntax-rules</tt> 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 hygienicallyaccording to the template.<p>An identifier that appears in the pattern of a &lt;syntax rule&gt; isa <em>pattern variable</em>, unless it is the keyword that begins the pattern,is listed in &lt;literals&gt;, or is the identifier ``<tt>...</tt>''.Pattern variables match arbitrary input elements andare used to refer to elements of the input in the template.  It is anerror for the same pattern variable to appear more than once in a&lt;pattern&gt;.<p>The keyword at the beginning of the pattern in a&lt;syntax rule&gt; is not involved in the matching andis not considered a pattern variable or literal identifier.<p><blockquote><em>Rationale:&nbsp;&nbsp;</em>The scope of the keyword is determined by the expression or syntaxdefinition that binds it to the associated macro transformer.If the keyword were a pattern variable or literalidentifier, thenthe template that follows the pattern would be within its scoperegardless of whether the keyword were bound by <tt>let-syntax</tt>or by <tt>letrec-syntax</tt>.</blockquote><p>Identifiers that appear in &lt;literals&gt; are interpreted as literalidentifiers to be matched against corresponding subforms of the input.A subformin the input matches a literal identifier if and only if it is anidentifierand either both its occurrence in the macro expression and itsoccurrence in the macro definition have the same lexical binding, orthe two identifiers are equal and both have no lexical binding.<p>A subpattern followed by <tt>...</tt> can match zero or more elements of theinput.  It is an error for <tt>...</tt> to appear in &lt;literals&gt;.Within a pattern the identifier <tt>...</tt> must follow the last element ofa nonempty sequence of subpatterns.<p>More formally, an input form <em>F</em> matches a pattern <em>P</em> if and only if:<p><p><ul><li><em>P</em> is a non-literal identifier; or<p><li><em>P</em> is a literal identifier and <em>F</em> is an identifier with the samebinding; or<p><li><em>P</em> is a list <tt>(<em>P</em><sub>1</sub> <tt>...</tt> <em>P</em><sub><em>n</em></sub>)</tt> and <em>F</em> is alist of <em>n</em>forms that match <em>P</em><sub>1</sub> through <em>P</em><sub><em>n</em></sub>, respectively; or<p><li><em>P</em> is an improper list<tt>(<em>P</em><sub>1</sub> <em>P</em><sub>2</sub> <tt>...</tt> <em>P</em><sub><em>n</em></sub> . <em>P</em><sub><em>n</em>+1</sub>)</tt>and <em>F</em> is a list orimproper list of <em>n</em> or more forms that match <em>P</em><sub>1</sub> through <em>P</em><sub><em>n</em></sub>,respectively, and whose <em>n</em>th ``cdr'' matches <em>P</em><sub><em>n</em>+1</sub>; or<p><li><em>P</em> is of the form<tt>(<em>P</em><sub>1</sub> <tt>...</tt> <em>P</em><sub><em>n</em></sub> <em>P</em><sub><em>n</em>+1</sub> &lt;ellipsis&gt;)</tt>where &lt;ellipsis&gt; is the identifier <tt>...</tt>and <em>F</em> isa proper list of at least <em>n</em> forms, the first <em>n</em> of which match<em>P</em><sub>1</sub> through <em>P</em><sub><em>n</em></sub>, respectively, and each remaining element of <em>F</em>matches <em>P</em><sub><em>n</em>+1</sub>; or<p><li><em>P</em> is a vector of the form <tt>#(<em>P</em><sub>1</sub> <tt>...</tt> <em>P</em><sub><em>n</em></sub>)</tt>and <em>F</em> is a vectorof <em>n</em> forms that match <em>P</em><sub>1</sub> through <em>P</em><sub><em>n</em></sub>; or<p><li><em>P</em> is of the form<tt>#(<em>P</em><sub>1</sub> <tt>...</tt> <em>P</em><sub><em>n</em></sub> <em>P</em><sub><em>n</em>+1</sub> &lt;ellipsis&gt;)</tt>where &lt;ellipsis&gt; is the identifier <tt>...</tt>and <em>F</em> is a vector of <em>n</em>or more forms the first <em>n</em> of which match<em>P</em><sub>1</sub> through <em>P</em><sub><em>n</em></sub>, respectively, and each remaining element of <em>F</em>matches <em>P</em><sub><em>n</em>+1</sub>; or<p><li><em>P</em> is a datum and <em>F</em> is equal to <em>P</em> in the sense ofthe <tt>equal?</tt> procedure.</ul><p><p>It is an error to use a macro keyword, within the scope of itsbinding, in an expression that does not match any of the patterns.<p>When a macro use is transcribed according to the template of thematching &lt;syntax rule&gt;, pattern variables that occur in thetemplate are replaced by the subforms they match in the input.Pattern variables that occur in subpatterns followed by one or moreinstances of the identifier<tt>...</tt> are allowed only in subtemplates that arefollowed by as many instances of <tt>...</tt>.They are replaced in theoutput by all of the subforms they match in the input, distributed asindicated.  It is an error if the output cannot be built up asspecified.<p>Identifiers that appear in the template but are not pattern variablesor the identifier<tt>...</tt> are inserted into the output as literal identifiers.  If aliteral identifier is inserted as a free identifier then it refers to thebinding of that identifier within whose scope the instance of<tt>syntax-rules</tt> appears.If a literal identifier is inserted as a bound identifier then it isin effect renamed to prevent inadvertent captures of free identifiers.<p>As an example, if <tt>let</tt> and <tt>cond</tt> are defined as insection&nbsp;<a href="r5rs-Z-H-10.html#%_sec_7.3">7.3</a> then they are hygienic (as required) andthe following is not an error.<p><tt><p>(let&nbsp;((=&gt;&nbsp;<tt>#f</tt>))<br>&nbsp;&nbsp;(cond&nbsp;(<tt>#t</tt>&nbsp;=&gt;&nbsp;'ok)))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;===&gt;&nbsp;ok<p></tt><p>The macro transformer for <tt>cond</tt> recognizes <tt>=&gt;</tt>as a local variable, and hence an expression, and not as thetop-level identifier <tt>=&gt;</tt>, which the macro transformer treatsas a syntactic keyword.  Thus the example expands into<p><tt><p>(let&nbsp;((=&gt;&nbsp;<tt>#f</tt>))<br>&nbsp;&nbsp;(if&nbsp;<tt>#t</tt>&nbsp;(begin&nbsp;=&gt;&nbsp;'ok)))<p></tt><p>instead of<p><tt><p>(let&nbsp;((=&gt;&nbsp;<tt>#f</tt>))<br>&nbsp;&nbsp;(let&nbsp;((temp&nbsp;<tt>#t</tt>))<br>&nbsp;&nbsp;&nbsp;&nbsp;(if&nbsp;temp&nbsp;('ok&nbsp;temp))))<p></tt><p>which would result in an invalid procedure call.<p><p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<p><p><div class=navigation>[Go to <span><a href="r5rs.html">first</a>, <a href="r5rs-Z-H-6.html">previous</a></span><span>, <a href="r5rs-Z-H-8.html">next</a></span> page<span>; &nbsp;&nbsp;</span><span><a href="r5rs-Z-H-2.html#%_toc_start">contents</a></span><span><span>; &nbsp;&nbsp;</span><a href="r5rs-Z-H-15.html#%_index_start">index</a></span>]</div><p></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -