📄 r5rs-z-h-10.html
字号:
<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 interms 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 possibledefinition 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 ina location makes it an error to try to obtain the value stored in thelocation (no such expression is defined in Scheme).A trick is used to generate the temporary names needed to avoidspecifying 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 ofthe ability to write more than one expression in the body of a lambdaexpression. In any case, note that these rules apply only if the bodyof 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 definitionof <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 unspecificvalue.<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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -