📄 r5rs-z-h-7.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-6.html">previous</a></span><span>, <a href="r5rs-Z-H-8.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_4"></a><h1 class=chapter><div class=chapterheading><a href="r5rs-Z-H-2.html#%_toc_%_chap_4">Chapter 4</a></div><p><a href="r5rs-Z-H-2.html#%_toc_%_chap_4">Expressions</a></h1><p><p><p>Expression types are categorized as <em>primitive</em> or <em>derived</em>.Primitive expression types include variables and procedure calls.Derived expression types are not semantically primitive, but can insteadbe defined as macros.With the exception of <tt>quasiquote</tt>, whose macro definition is complex,the derived expressions are classified as library features.Suitable definitions are given in section <a href="r5rs-Z-H-10.html#%_sec_7.3">7.3</a>.<p><a name="%_sec_4.1"></a><h2><a href="r5rs-Z-H-2.html#%_toc_%_sec_4.1">4.1 Primitive expression types</a></h2><p><p><a name="%_sec_4.1.1"></a><h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_4.1.1">4.1.1 Variable references</a></h3><p><p><p><p><p><div align=left><u>syntax:</u> <tt><variable></tt> </div><p>An expression consisting of a variable<a name="%_idx_82"></a>(section <a href="r5rs-Z-H-6.html#%_sec_3.1">3.1</a>) is a variable reference. The value ofthe variable reference is the value stored in the location to which thevariable is bound. It is an error to reference anunbound<a name="%_idx_84"></a> variable.<p><tt><p>(define x 28)<br>x ===> 28<p></tt><p><p><a name="%_sec_4.1.2"></a><h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_4.1.2">4.1.2 Literal expressions</a></h3><p><p><p><p><p><div align=left><u>syntax:</u> <tt>(<a name="%_idx_86"></a>quote<i> <datum></i>)</tt> </div><div align=left><u>syntax:</u> <tt><tt>'</tt><datum></tt> </div><div align=left><u>syntax:</u> <tt><constant></tt> </div><p><tt>(quote <datum>)</tt> evaluates to <datum>.<a name="%_idx_88"></a><Datum>may be any external representation of a Scheme object (seesection <a href="r5rs-Z-H-6.html#%_sec_3.3">3.3</a>). This notation is used to include literalconstants in Scheme code.<p><tt><p>(quote a) ===> a<br>(quote <tt>#</tt>(a b c)) ===> #(a b c)<br>(quote (+ 1 2)) ===> (+ 1 2)<p></tt><p><tt>(quote <datum>)</tt> may be abbreviated as<tt>'</tt><datum>. The two notations are equivalent in allrespects.<p><tt><p>'a ===> a<br>'#(a b c) ===> #(a b c)<br>'() ===> ()<br>'(+ 1 2) ===> (+ 1 2)<br>'(quote a) ===> (quote a)<br>''a ===> (quote a)<p></tt><p>Numerical constants, string constants, character constants, and booleanconstants evaluate ``to themselves''; they need not be quoted.<p><tt><p>'"abc" ===> "abc"<br>"abc" ===> "abc"<br>'145932 ===> 145932<br>145932 ===> 145932<br>'<tt>#t</tt> ===> <tt>#t</tt><br><tt>#t</tt> ===> <tt>#t</tt><p></tt><p>As noted in section <a href="r5rs-Z-H-6.html#%_sec_3.4">3.4</a>, it is an error to alter a constant(i.e. the value of a literal expression) using a mutation procedure like<tt>set-car!</tt> or <tt>string-set!</tt>.<p><p><p><a name="%_sec_4.1.3"></a><h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_4.1.3">4.1.3 Procedure calls</a></h3><p><p><p><p><p><div align=left><u>syntax:</u> <tt>(<operator> <operand<sub>1</sub>> <tt>...</tt>)</tt> </div><p>A procedure call is written by simply enclosing in parenthesesexpressions for the procedure to be called and the arguments to bepassed to it. The operator and operand expressions are evaluated (in anunspecified order) and the resulting procedure is passed the resultingarguments.<a name="%_idx_90"></a><a name="%_idx_92"></a><tt><p>(+ 3 4) ===> 7<br>((if <tt>#f</tt> + *) 3 4) ===> 12<p></tt><p>A number of procedures are available as the values of variables in theinitial environment; for example, the addition and multiplicationprocedures in the above examples are the values of the variables <tt>+</tt>and <tt>*</tt>. New procedures are created by evaluating <tt>lambda</tt> expressions(see section <a href="#%_sec_4.1.4">4.1.4</a>).Procedure calls may return any number of values (see <tt>values</tt> insection <a href="r5rs-Z-H-9.html#%_sec_6.4">6.4</a>). With the exception of <tt>values</tt>the procedures available in the initial environment return onevalue or, for procedures such as <tt>apply</tt>, pass on the values returnedby a call to one of their arguments.<p>Procedure calls are also called <em>combinations</em>.<a name="%_idx_94"></a><p><blockquote><em>Note: </em> In contrast to other dialects of Lisp, the order ofevaluation is unspecified, and the operator expression and the operandexpressions are always evaluated with the same evaluation rules.</blockquote><p><blockquote><em>Note: </em>Although the order of evaluation is otherwise unspecified, the effect ofany concurrent evaluation of the operator and operand expressions isconstrained to be consistent with some sequential order of evaluation.The order of evaluation may be chosen differently for each procedure call.</blockquote><p><blockquote><em>Note: </em> In many dialects of Lisp, the empty combination, <tt>()</tt>, is a legitimate expression. In Scheme, combinations must have atleast one subexpression, so <tt>()</tt> is not a syntactically validexpression. </blockquote><p><p><p><p><a name="%_sec_4.1.4"></a><h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_4.1.4">4.1.4 Procedures</a></h3><p><p><p><p><p><div align=left><u>syntax:</u> <tt>(<a name="%_idx_96"></a>lambda<i> <formals> <body></i>)</tt> </div><p><em>Syntax: </em><Formals> should be a formal arguments list as described below,and <body> should be a sequence of one or more expressions.<p><em>Semantics: </em>A <tt>lambda</tt> expression evaluates to a procedure. The environment ineffect when the <tt>lambda</tt> expression was evaluated is remembered as part of theprocedure. When the procedure is later called with some actualarguments, the environment in which the <tt>lambda</tt> expression was evaluated willbe extended by binding the variables in the formal argument list tofresh locations, the corresponding actual argument values will be storedin those locations, and the expressions in the body of the <tt>lambda</tt> expressionwill be evaluated sequentially in the extended environment.The result(s) of the last expression in the body will be returned asthe result(s) of the procedure call.<p><tt><p>(lambda (x) (+ x x)) ===> <em>a procedure</em><br>((lambda (x) (+ x x)) 4) ===> 8<br><br>(define reverse-subtract<br> (lambda (x y) (- y x)))<br>(reverse-subtract 7 10) ===> 3<br><br>(define add4<br> (let ((x 4))<br> (lambda (y) (+ x y))))<br>(add4 6) ===> 10<p></tt><p><Formals> should have one of the following forms:<p><p><ul><li><tt>(<variable<sub>1</sub>> <tt>...</tt>)</tt>:The procedure takes a fixed number of arguments; when the procedure iscalled, the arguments will be stored in the bindings of thecorresponding variables.<p><li><variable>:The procedure takes any number of arguments; when the procedure iscalled, the sequence of actual arguments is converted into a newlyallocated list, and the list is stored in the binding of the<variable>.<p><li><tt>(<variable<sub>1</sub>> <tt>...</tt> <variable<sub><em>n</em></sub>> <strong>.</strong><variable<sub><em>n</em>+1</sub>>)</tt>:If a space-delimited period precedes the last variable, thenthe procedure takes <em>n</em> or more arguments, where <em>n</em> is thenumber of formal arguments before the period (there mustbe at least one).The value stored in the binding of the last variable will be anewly allocatedlist of the actual arguments left over after all the other actualarguments have been matched up against the other formal arguments.</ul><p><p>It is an error for a <variable> to appear more than once in<formals>.<p><tt><p>((lambda x x) 3 4 5 6) ===> (3 4 5 6)<br>((lambda (x y . z) z)<br> 3 4 5 6) ===> (5 6)<p></tt><p>Each procedure created as the result of evaluating a <tt>lambda</tt> expression is(conceptually) taggedwith a storage location, in order to make <tt>eqv?</tt> and<tt>eq?</tt> work on procedures (see section <a href="r5rs-Z-H-9.html#%_sec_6.1">6.1</a>).<p><p><p><a name="%_sec_4.1.5"></a><h3><a href="r5rs-Z-H-2.html#%_toc_%_sec_4.1.5">4.1.5 Conditionals</a></h3><p><p><p><p><p><div align=left><u>syntax:</u> <tt>(<a name="%_idx_98"></a>if<i> <test> <consequent> <alternate></i>)</tt> </div><div align=left><u>syntax:</u> <tt>(if<i> <test> <consequent></i>)</tt> </div> <em>Syntax: </em><Test>, <consequent>, and <alternate> may be arbitraryexpressions.<p><em>Semantics: </em>An <tt>if</tt> expression is evaluated as follows: first,<test> is evaluated. If it yields a true value<a name="%_idx_100"></a> (seesection <a href="r5rs-Z-H-9.html#%_sec_6.3.1">6.3.1</a>), then <consequent> is evaluated andits value(s) is(are) returned. Otherwise <alternate> is evaluated and itsvalue(s) is(are) returned. If <test> yields a false value and no<alternate> is specified, then the result of the expression isunspecified.<p><tt><p>(if (> 3 2) 'yes 'no) ===> yes<br>(if (> 2 3) 'yes 'no) ===> no<br>(if (> 3 2)<br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -