📄 r5rs-z-h-7.html
字号:
(- 3 2)<br> (+ 3 2)) ===> 1<p></tt><p><p><p><a name="%_sec_4.1.6"></a><h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_4.1.6">4.1.6 Assignments</a></h3><p><p><p><p><p><div align=left><u>syntax:</u> <tt>(<a name="%_idx_102"></a>set!<i> <variable> <expression></i>)</tt> </div><p><Expression> is evaluated, and the resulting value is stored inthe location to which <variable> is bound. <Variable> mustbe bound either in some region<a name="%_idx_104"></a> enclosing the <tt>set!</tt> expressionor at top level. The result of the <tt>set!</tt> expression isunspecified.<p><tt><p>(define x 2)<br>(+ x 1) ===> 3<br>(set! x 4) ===> <i>unspecified</i><br>(+ x 1) ===> 5<p></tt><p><p><p><a name="%_sec_4.2"></a><h2><a href="r5rs-Z-H-2.html#%_toc_%_sec_4.2">4.2 Derived expression types</a></h2><p><p>The constructs in this section are hygienic, as discussed insection <a href="#%_sec_4.3">4.3</a>.For reference purposes, section <a href="r5rs-Z-H-10.html#%_sec_7.3">7.3</a> gives macro definitionsthat will convert most of the constructs described in this section into the primitive constructs described in the previous section.<p><p><a name="%_sec_4.2.1"></a><h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_4.2.1">4.2.1 Conditionals</a></h3><p><p><p><p><p><div align=left><u>library syntax:</u> <tt>(<a name="%_idx_106"></a>cond<i> <clause<sub>1</sub>> <clause<sub>2</sub>> <tt>...</tt></i>)</tt> </div><p><em>Syntax: </em>Each <clause> should be of the form<tt><p>(<test> <expression<sub>1</sub>> <tt>...</tt>)<p></tt>where <test> is any expression. Alternatively, a <clause> may beof the form<tt><p>(<test> => <expression>)<p></tt>The last <clause> may bean ``else clause,'' which has the form<tt><p>(else <expression<sub>1</sub>> <expression<sub>2</sub>> <tt>...</tt>).<p></tt><a name="%_idx_108"></a><a name="%_idx_110"></a><p><em>Semantics: </em>A <tt>cond</tt> expression is evaluated by evaluating the <test>expressions of successive <clause>s in order until one of themevaluates to a true value<a name="%_idx_112"></a> (seesection <a href="r5rs-Z-H-9.html#%_sec_6.3.1">6.3.1</a>). When a <test> evaluates to a truevalue, then the remaining <expression>s in its <clause> areevaluated in order, and the result(s) of the last <expression> in the<clause> is(are) returned as the result(s) of the entire <tt>cond</tt>expression. If the selected <clause> contains only the<test> and no <expression>s, then the value of the<test> is returned as the result. If the selected <clause> uses the<tt>=></tt> alternate form, then the <expression> is evaluated.Its value must be a procedure that accepts one argument; this procedure is thencalled on the value of the <test> and the value(s) returned by thisprocedure is(are) returned by the <tt>cond</tt> expression.If all <test>s evaluateto false values, and there is no else clause, then the result ofthe conditional expression is unspecified; if there is an elseclause, then its <expression>s are evaluated, and the value(s) ofthe last one is(are) returned.<p><tt><p>(cond ((> 3 2) 'greater)<br> ((< 3 2) 'less)) ===> greater<br>(cond ((> 3 3) 'greater)<br> ((< 3 3) 'less)<br> (else 'equal)) ===> equal<br>(cond ((assv 'b '((a 1) (b 2))) => cadr)<br> (else <tt>#f</tt>)) ===> 2<p></tt><p><p><p><p><div align=left><u>library syntax:</u> <tt>(<a name="%_idx_114"></a>case<i> <key> <clause<sub>1</sub>> <clause<sub>2</sub>> <tt>...</tt></i>)</tt> </div><p><em>Syntax: </em><Key> may be any expression. Each <clause> should havethe form<tt><p>((<datum<sub>1</sub>> <tt>...</tt>) <expression<sub>1</sub>> <expression<sub>2</sub>> <tt>...</tt>),<p></tt>where each <datum> is an external representation of some object.All the <datum>s must be distinct.The last <clause> may be an ``else clause,'' which has the form<tt><p>(else <expression<sub>1</sub>> <expression<sub>2</sub>> <tt>...</tt>).<p></tt><a name="%_idx_116"></a><p><em>Semantics: </em>A <tt>case</tt> expression is evaluated as follows. <Key> isevaluated and its result is compared against each <datum>. If theresult of evaluating <key> is equivalent (in the sense of<tt>eqv?</tt>; see section <a href="r5rs-Z-H-9.html#%_sec_6.1">6.1</a>) to a <datum>, then theexpressions in the corresponding <clause> are evaluated from leftto right and the result(s) of the last expression in the <clause> is(are)returned as the result(s) of the <tt>case</tt> expression. If the result ofevaluating <key> is different from every <datum>, then ifthere is an else clause its expressions are evaluated and theresult(s) of the last is(are) the result(s) of the <tt>case</tt> expression;otherwise the result of the <tt>case</tt> expression is unspecified.<p><tt><p>(case (* 2 3)<br> ((2 3 5 7) 'prime)<br> ((1 4 6 8 9) 'composite)) ===> composite<br>(case (car '(c d))<br> ((a) 'a)<br> ((b) 'b)) ===> <i>unspecified</i><br>(case (car '(c d))<br> ((a e i o u) 'vowel)<br> ((w y) 'semivowel)<br> (else 'consonant)) ===> consonant<p></tt><p><p><p><p><div align=left><u>library syntax:</u> <tt>(<a name="%_idx_118"></a>and<i> <test<sub>1</sub>> <tt>...</tt></i>)</tt> </div><p>The <test> expressions are evaluated from left to right, and thevalue of the first expression that evaluates to a false value (seesection <a href="r5rs-Z-H-9.html#%_sec_6.3.1">6.3.1</a>) is returned. Any remaining expressionsare not evaluated. If all the expressions evaluate to true values, thevalue of the last expression is returned. If there are no expressionsthen <tt>#t</tt> is returned.<p><tt><p>(and (= 2 2) (> 2 1)) ===> <tt>#t</tt><br>(and (= 2 2) (< 2 1)) ===> <tt>#f</tt><br>(and 1 2 'c '(f g)) ===> (f g)<br>(and) ===> <tt>#t</tt><p></tt><p><p><p><p><div align=left><u>library syntax:</u> <tt>(<a name="%_idx_120"></a>or<i> <test<sub>1</sub>> <tt>...</tt></i>)</tt> </div><p>The <test> expressions are evaluated from left to right, and the value of thefirst expression that evaluates to a true value (seesection <a href="r5rs-Z-H-9.html#%_sec_6.3.1">6.3.1</a>) is returned. Any remaining expressionsare not evaluated. If all expressions evaluate to false values, thevalue of the last expression is returned. If there are noexpressions then <tt>#f</tt> is returned.<p><tt><p>(or (= 2 2) (> 2 1)) ===> <tt>#t</tt><br>(or (= 2 2) (< 2 1)) ===> <tt>#t</tt><br>(or <tt>#f</tt> <tt>#f</tt> <tt>#f</tt>) ===> <tt>#f</tt><br>(or (memq 'b '(a b c)) <br> (/ 3 0)) ===> (b c)<p></tt><p><p><p><a name="%_sec_4.2.2"></a><h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_4.2.2">4.2.2 Binding constructs</a></h3><p>The three binding constructs <tt>let</tt>, <tt>let*</tt>, and <tt>letrec</tt>give Scheme a block structure, like Algol 60. The syntax of the threeconstructs is identical, but they differ in the regions<a name="%_idx_122"></a> they establishfor their variable bindings. In a <tt>let</tt> expression, the initialvalues are computed before any of the variables become bound; in a<tt>let*</tt> expression, the bindings and evaluations are performedsequentially; while in a <tt>letrec</tt> expression, all the bindings are ineffect while their initial values are being computed, thus allowingmutually recursive definitions.<p><p><div align=left><u>library syntax:</u> <tt>(<a name="%_idx_124"></a>let<i> <bindings> <body></i>)</tt> </div><p><em>Syntax: </em><Bindings> should have the form<tt><p>((<variable<sub>1</sub>> <init<sub>1</sub>>) <tt>...</tt>),<p></tt>where each <init> is an expression, and <body> should be asequence of one or more expressions. It isan error for a <variable> to appear more than once in the list of variablesbeing bound.<p><em>Semantics: </em>The <init>s are evaluated in the current environment (in someunspecified order), the <variable>s are bound to fresh locationsholding the results, the <body> is evaluated in the extendedenvironment, and the value(s) of the last expression of <body>is(are) returned. Each binding of a <variable> has <body> as itsregion.<a name="%_idx_126"></a><p><tt><p>(let ((x 2) (y 3))<br> (* x y)) ===> 6<br><br>(let ((x 2) (y 3))<br> (let ((x 7)<br> (z (+ x y)))<br> (* z x))) ===> 35<p></tt><p>See also named <tt>let</tt>, section <a href="#%_sec_4.2.4">4.2.4</a>.<p><p><p><p><div align=left><u>library syntax:</u> <tt>(<a name="%_idx_128"></a>let*<i> <bindings> <body></i>)</tt> </div><p><em>Syntax: </em><Bindings> should have the form<tt><p>((<variable<sub>1</sub>> <init<sub>1</sub>>) <tt>...</tt>),<p></tt>and <body> should be a sequence ofone or more expressions.<p><em>Semantics: </em><tt>Let*</tt> is similar to <tt>let</tt>, but the bindings are performedsequentially from left to right, and the region<a name="%_idx_130"></a> of a binding indicatedby <tt>(<variable> <init>)</tt> is that part of the <tt>let*</tt>expression to the right of the binding. Thus the second binding is donein an environment in which the first binding is visible, and so on.<p><tt><p>(let ((x 2) (y 3))<br> (let* ((x 7)<br> (z (+ x y)))<br> (* z x))) ===> 70<p></tt><p><p><p><p><div align=left><u>library syntax:</u> <tt>(<a name="%_idx_132"></a>letrec<i> <bindings> <body></i>)</tt> </div><p><em>Syntax: </em><Bindings> should have the form<tt><p>((<variable<sub>1</sub>> <init<sub>1</sub>>) <tt>...</tt>),<p></tt>and <body> should be a sequence ofone or more expressions. It is an error for a <variable> to appear morethan once in the list of variables being bound.<p><em>Semantics: </em>The <variable>s are bound to fresh locations holding undefinedvalues, the <init>s are evaluated in the resulting environment (insome unspecified order), each <variable> is assigned to the resultof the corresponding <init>, the <body> is evaluated in theresulting environment, and the value(s) of the last expression in<body> is(are) returned. Each binding of a <variable> has theentire <tt>letrec</tt> expression as its region<a name="%_idx_134"></a>, making it possible todefine mutually recursive procedures.<p><tt><p>(letrec ((even?<br> (lambda (n)<br> (if (zero? n)<br> <tt>#t</tt><br> (odd? (- n 1)))))<br> (odd?<br> (lambda (n)<br> (if (zero? n)<br> <tt>#f</tt><br> (even? (- n 1))))))<br> (even? 88)) <br> ===> <tt>#t</tt><p></tt><p>One restriction on <tt>letrec</tt> is very important: it must be possibleto evaluate each <init> without assigning or referring to the value of any
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -