📄 r5rs-z-h-7.html
字号:
<variable>. If this restriction is violated, then it is an error. Therestriction is necessary because Scheme passes arguments by value rather than byname. In the most common uses of <tt>letrec</tt>, all the <init>s are<tt>lambda</tt> expressions and the restriction is satisfied automatically.<p><p><p><a name="%_sec_4.2.3"></a><h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_4.2.3">4.2.3 Sequencing</a></h3><p><p><p><p><p><div align=left><u>library syntax:</u> <tt>(<a name="%_idx_136"></a>begin<i> <expression<sub>1</sub>> <expression<sub>2</sub>> <tt>...</tt></i>)</tt> </div><p>The <expression>s are evaluated sequentially from left to right,and the value(s) of the last <expression> is(are) returned. Thisexpression type is used to sequence side effects such as input andoutput.<p><tt><p>(define x 0)<br><br>(begin (set! x 5)<br> (+ x 1)) ===> 6<br><br>(begin (display "4 plus 1 equals ")<br> (display (+ 4 1))) ===> <i>unspecified</i><br> <em> and prints</em> 4 plus 1 equals 5<p></tt><p><p><p><a name="%_sec_4.2.4"></a><h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_4.2.4">4.2.4 Iteration</a></h3><p><div align=left><u>library syntax:</u> <tt>(do ((<variable<sub>1</sub>> <init<sub>1</sub>> <step<sub>1</sub>>)</tt> </div><a name="%_idx_138"></a><tt> <tt>...</tt>)<br> (<test> <expression> <tt>...</tt>)<br> <command> <tt>...</tt>)</tt><p><tt>Do</tt> is an iteration construct. It specifies a set of variables tobe bound, how they are to be initialized at the start, and how they areto be updated on each iteration. When a termination condition is met,the loop exits after evaluating the <expression>s.<p><tt>Do</tt> expressions are evaluated as follows:The <init> expressions are evaluated (in some unspecified order),the <variable>s are bound to fresh locations, the results of the<init> expressions are stored in the bindings of the<variable>s, and then the iteration phase begins.<p>Each iteration begins by evaluating <test>; if the result isfalse (see section <a href="r5rs-Z-H-9.html#%_sec_6.3.1">6.3.1</a>), then the <command>expressions are evaluated in order for effect, the <step>expressions are evaluated in some unspecified order, the<variable>s are bound to fresh locations, the results of the<step>s are stored in the bindings of the<variable>s, and the next iteration begins.<p>If <test> evaluates to a true value, then the<expression>s are evaluated from left to right and the value(s) ofthe last <expression> is(are) returned. If no <expression>sare present, then the value of the <tt>do</tt> expression is unspecified.<p>The region<a name="%_idx_140"></a> of the binding of a <variable>consists of the entire <tt>do</tt> expression except for the <init>s.It is an error for a <variable> to appear more than once in thelist of <tt>do</tt> variables.<p>A <step> may be omitted, in which case the effect is thesame as if <tt>(<variable> <init> <variable>)</tt> hadbeen written instead of <tt>(<variable> <init>)</tt>.<p><tt><p>(do ((vec (make-vector 5))<br> (i 0 (+ i 1)))<br> ((= i 5) vec)<br> (vector-set! vec i i)) ===> #(0 1 2 3 4)<br><br>(let ((x '(1 3 5 7 9)))<br> (do ((x x (cdr x))<br> (sum 0 (+ sum (car x))))<br> ((null? x) sum))) ===> 25<p></tt><p><p><p><div align=left><u>library syntax:</u> <tt>(let<i> <variable> <bindings> <body></i>)</tt> </div><p>``Named <tt>let</tt>'' is a variant on the syntax of <tt>let</tt> which providesa more general looping construct than <tt>do</tt> and may also be used to expressrecursions.It has the same syntax and semantics as ordinary <tt>let</tt>except that <variable> is bound within <body> to a procedurewhose formal arguments are the bound variables and whose body is<body>. Thus the execution of <body> may be repeated byinvoking the procedure named by <variable>.<p><tt><p>(let loop ((numbers '(3 -2 1 6 -5))<br> (nonneg '())<br> (neg '()))<br> (cond ((null? numbers) (list nonneg neg))<br> ((>= (car numbers) 0)<br> (loop (cdr numbers)<br> (cons (car numbers) nonneg)<br> neg))<br> ((< (car numbers) 0)<br> (loop (cdr numbers)<br> nonneg<br> (cons (car numbers) neg))))) <br> ===> ((6 1 3) (-5 -2))<p></tt><p><p><p><a name="%_sec_4.2.5"></a><h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_4.2.5">4.2.5 Delayed evaluation</a></h3><p><p><p><p><p><div align=left><u>library syntax:</u> <tt>(<a name="%_idx_142"></a>delay<i> <expression></i>)</tt> </div><p><p>The <tt>delay</tt> construct is used together with the procedure <tt>force</tt> toimplement <a name="%_idx_144"></a><em>lazy evaluation</em> or <a name="%_idx_146"></a><em>call by need</em>.<tt>(delay <expression>)</tt> returns an object called a<a name="%_idx_148"></a><em>promise</em> which at some point in the future may be asked (bythe <tt>force</tt> procedure) to evaluate<expression>, and deliver the resulting value.The effect of <expression> returning multiple valuesis unspecified.<p>See the description of <tt>force</tt> (section <a href="r5rs-Z-H-9.html#%_sec_6.4">6.4</a>) for amore complete description of <tt>delay</tt>.<p><p><p><a name="%_sec_4.2.6"></a><h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_4.2.6">4.2.6 Quasiquotation</a></h3><p><p><p><p><p><div align=left><u>syntax:</u> <tt>(<a name="%_idx_150"></a>quasiquote<i> <qq template></i>)</tt> </div> <div align=left><u>syntax:</u> <tt><tt>`</tt><qq template></tt> </div><p>``Backquote'' or ``quasiquote''<a name="%_idx_152"></a> expressions are usefulfor constructing a list or vector structure when most but not all of thedesired structure is known in advance. If nocommas<a name="%_idx_154"></a> appear within the <qq template>, the result ofevaluating<tt>`</tt><qq template> is equivalent to the result of evaluating<tt>'</tt><qq template>. If a comma<a name="%_idx_156"></a> appears within the<qq template>, however, the expression following the comma isevaluated (``unquoted'') and its result is inserted into the structureinstead of the comma and the expression. If a comma appears followedimmediately by an at-sign (<tt>@</tt>),<a name="%_idx_158"></a> then the followingexpression must evaluate to a list; the opening and closing parenthesesof the list are then ``stripped away'' and the elements of the list areinserted in place of the comma at-sign expression sequence. A commaat-sign should only appear within a list or vector <qq template>.<p><tt><p>`(list ,(+ 1 2) 4) ===> (list 3 4)<br>(let ((name 'a)) `(list ,name ',name)) <br> ===> (list a (quote a))<br>`(a ,(+ 1 2) ,@(map abs '(4 -5 6)) b) <br> ===> (a 3 4 5 6 b)<br>`((<tt> foo</tt> ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons))) <br> ===> ((foo 7) . cons)<br>`#(10 5 ,(sqrt 4) ,@(map sqrt '(16 9)) 8) <br> ===> #(10 5 2 4 3 8)<p></tt><p>Quasiquote forms may be nested. Substitutions are made only forunquoted components appearing at the same nesting levelas the outermost backquote. The nesting level increases by one insideeach successive quasiquotation, and decreases by one inside eachunquotation.<p><tt><p>`(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f) <br> ===> (a `(b ,(+ 1 2) ,(foo 4 d) e) f)<br>(let ((name1 'x)<br> (name2 'y))<br> `(a `(b ,,name1 ,',name2 d) e)) <br> ===> (a `(b ,x ,'y d) e)<p></tt><p>The two notations<tt>`</tt><qq template> and <tt>(quasiquote <qq template>)</tt>are identical in all respects.<tt>,<expression></tt> is identical to <tt>(unquote <expression>)</tt>,and<tt>,@<expression></tt> is identical to <tt>(unquote-splicing <expression>)</tt>.The external syntax generated by <tt>write</tt> for two-element lists whosecar is one of these symbols may vary between implementations.<a name="%_idx_160"></a><p><tt><p>(quasiquote (list (unquote (+ 1 2)) 4)) <br> ===> (list 3 4)<br>'(quasiquote (list (unquote (+ 1 2)) 4)) <br> ===> `(list ,(+ 1 2) 4)<br> <em>i.e.,</em> (quasiquote (list (unquote (+ 1 2)) 4))<p></tt><p>Unpredictable behavior can result if any of the symbols<tt>quasiquote</tt>, <tt>unquote</tt>, or <tt>unquote-splicing</tt> appear inpositions within a <qq template> otherwise than as described above.<p><p><p><a name="%_sec_4.3"></a><h2><a href="r5rs-Z-H-2.html#%_toc_%_sec_4.3">4.3 Macros</a></h2><p><p>Scheme programs can define and use new derived expression types,called <em>macros</em>.<a name="%_idx_162"></a>Program-defined expression types have the syntax<tt><p>(<keyword> <datum> ...)<p></tt>where <keyword> is an identifier that uniquely determines theexpression type. This identifier is called the <em>syntactickeyword</em><a name="%_idx_164"></a>, or simply <em>keyword</em><a name="%_idx_166"></a>, of the macro<a name="%_idx_168"></a>. Thenumber of the <datum>s, and their syntax, depends on theexpression type.<p>Each instance of a macro is called a <em>use</em><a name="%_idx_170"></a>of the macro.The set of rules that specifieshow a use of a macro is transcribed into a more primitive expressionis called the <em>transformer</em><a name="%_idx_172"></a>of the macro.<p>The macro definition facility consists of two parts:<p><p><ul><li>A set of expressions used to establish that certain identifiersare macro keywords, associate them with macro transformers, and controlthe scope within which a macro is defined, and<p><li>a pattern language for specifying macro transformers.</ul><p><p>The syntactic keyword of a macro may shadow variable bindings, and localvariable bindings may shadow keyword bindings. <a name="%_idx_174"></a> All macrosdefined using the pattern language are ``hygienic'' and ``referentiallytransparent'' and thus preserve Scheme's lexical scoping [<a href="r5rs-Z-H-14.html#%_sec_7.3">14</a>, <a href="r5rs-Z-H-14.html#%_sec_7.3">15</a>, <a href="r5rs-Z-H-14.html#%_sec_7.3">2</a>, <a href="r5rs-Z-H-14.html#%_sec_7.3">7</a>, <a href="r5rs-Z-H-14.html#%_sec_7.3">9</a>]:<a name="%_idx_176"></a><a name="%_idx_178"></a><p><p><ul><p><li>If a macro transformer inserts a binding for an identifier(variable or keyword), the identifier will in effect be renamedthroughout its scope to avoid conflicts with other identifiers.Note that a <tt>define</tt> at top level may or may not introduce a binding;see section <a href="r5rs-Z-H-8.html#%_sec_5.2">5.2</a>.<p><li>If a macro transformer inserts a free reference to anidentifier, the reference refers to the binding that was visiblewhere the transformer was specified, regardless of any localbindings that may surround the use of the macro.<p></ul><p><p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -