The expressive power of the class of procedures that we can define at this
point is very limited, because we have no way to make tests and to perform
different operations depending on the result of a test. For instance, we
cannot define a procedure that computes the absolute value of a number by
testing whether the number is positive, negative, or zero and taking different
actions in the different cases according to the rule
目前我们能定义的这类过程的表达能力十分有限,因为我们还没有办法进行测试,并根据测试结果执行不同的操作。例如,我们无法定义一个计算某个数绝对值的过程,其思路本应是:通过测试这个数是正数、负数还是零,再按照如下规则分情况采取不同的行动:
|
x
|
=
{
x
if
若
x
> -->
0
,
0
if
若
x
=
0
,
−
x
if
若
x
0.
This construct is called a
case analysis, and there is a special form
in Lisp for notating such a case analysis. It is called cond (which
stands for “conditional”), and it is used as follows:
这种构造称为情况分析 (case analysis),Lisp 中有一个专门的特殊形式用于表示这样的情况分析,它叫做 cond("conditional"的缩写),用法如下:
The general form of a conditional expression is
条件表达式的一般形式为:
consisting of the symbol cond followed by parenthesized pairs of
expressions
它由符号 cond 后跟若干括号括起的表达式对 (pairs of expressions) 组成:
called
clauses. The first expression in each pair is a
这些对称为子句 (clauses)。每对中的第一个表达式是一个
predicate—that is, an expression whose value is interpreted as
either true or false.
谓词 (predicate)——即其值被解释为真或假的表达式。
Conditional expressions are evaluated as follows. The predicate ⟨
条件表达式的求值方式如下。首先对谓词 ⟨
p
1
⟩ is
evaluated first. If its value is false, then ⟨
⟩ 求值。若其值为假,则对 ⟨
p
2
⟩ is evaluated. If
⟨
⟩ 求值。若 ⟨
p
2
⟩’s value is also false, then ⟨
⟩ 的值也为假,则对 ⟨
p
3
⟩ is evaluated. This process
continues until a predicate is found whose value is true, in which case the
interpreter returns the value of the corresponding
consequent expression
⟨
e
⟩ of the clause as the value of the conditional expression.
If none of the ⟨
p
⟩’s is found to be true, the value of the cond is
undefined.
⟩ 求值。如此继续,直至找到某个值为真的谓词为止;此时解释器返回该子句对应的结论表达式 ⟨
e
⟩ 的值,作为整个条件表达式的值。若所有 ⟨
p
⟩ 均不为真,则 cond 的值未定义。
The word
predicate is used for procedures that return true or false,
as well as for expressions that evaluate to true or false. The absolute-value
procedure abs makes use of the primitive predicates >, ,
and =. These take two numbers as arguments and test whether the first
number is, respectively, greater than, less than, or equal to the second
number, returning true or false accordingly.
Another way to write the absolute-value procedure is
"谓词"一词既用于指返回真或假的过程,也用于指求值结果为真或假的表达式。求绝对值的过程 abs 使用了基本谓词 >、< 和 =。这些谓词接受两个数作为参数,分别检验第一个数是否大于、小于或等于第二个数,并相应地返回真或假。另一种写法是:
which could be expressed in English as “If x is less than zero return
−
x; otherwise return x.” Else is a special symbol that can be
used in place of the ⟨
p
⟩ in the final clause of a cond. This
causes the cond to return as its value the value of the corresponding
⟨
e
⟩ whenever all previous clauses have been bypassed. In fact, any
expression that always evaluates to a true value could be used as the ⟨
p
⟩
here.
Here is yet another way to write the absolute-value procedure:
用英语表达即:"若 x 小于零,返回 −x;否则返回 x。"else 是一个特殊符号,可用于 cond 最后一个子句中的 ⟨
p
⟩ 位置。这使得 cond 在所有前面的子句均被跳过时,将对应 ⟨
e
⟩ 的值作为自身的值返回。事实上,任何求值结果始终为真的表达式都可以在此充当 ⟨
p
⟩。以下是求绝对值过程的又一种写法:
This uses the special form if, a restricted type of conditional that can
be used when there are precisely two cases in the case analysis. The general
form of an if expression is
这里使用了特殊形式 if——一种受限的条件结构,适用于情况分析恰好只有两个分支的场合。if 表达式的一般形式是:
To evaluate an if expression, the interpreter starts by evaluating the
⟨predicate⟩ part of the expression. If the ⟨predicate⟩ evaluates
to a true value, the interpreter then evaluates the ⟨consequent⟩ and
returns its value. Otherwise it evaluates the ⟨alternative⟩ and returns
its value.
对 if 表达式求值时,解释器首先对表达式的 ⟨谓词⟩ 部分求值。若 ⟨谓词⟩ 的值为真,解释器随后对 ⟨结论⟩ 求值并返回其值;否则对 ⟨替代⟩ 求值并返回其值。
In addition to primitive predicates such as , =, and >,
there are logical composition operations, which enable us to construct compound
predicates. The three most frequently used are these:
除了 <、= 和 > 这类基本谓词之外,还有逻辑组合运算,使我们能够构造复合谓词。最常用的三种如下:
(and ⟨e₁⟩ … ⟨eₙ⟩)
The interpreter evaluates the expressions ⟨e⟩ one at a time, in
left-to-right order. If any ⟨e⟩ evaluates to false, the value of the
and expression is false, and the rest of the ⟨e⟩’s are not
evaluated. If all ⟨e⟩’s evaluate to true values, the value of the
and expression is the value of the last one.
解释器从左到右逐一对各表达式 ⟨e⟩ 求值。一旦某个 ⟨e⟩ 的值为假,and 表达式的值即为假,其余 ⟨e⟩ 不再求值。若所有 ⟨e⟩ 的值均为真,则 and 表达式的值为最后一个 ⟨e⟩ 的值。
(or ⟨e₁⟩ … ⟨eₙ⟩)
The interpreter evaluates the expressions ⟨e⟩ one at a time, in
left-to-right order. If any ⟨e⟩ evaluates to a true value, that value is
returned as the value of the or expression, and the rest of the
⟨e⟩’s are not evaluated. If all ⟨e⟩’s evaluate to false, the value
of the or expression is false.
解释器从左到右逐一对各表达式 ⟨e⟩ 求值。一旦某个 ⟨e⟩ 的值为真,该值即作为 or 表达式的值返回,其余 ⟨e⟩ 不再求值。若所有 ⟨e⟩ 的值均为假,则 or 表达式的值为假。
(not ⟨e⟩)
The value of a not expression is true when the expression ⟨e⟩
evaluates to false, and false otherwise.
当表达式 ⟨e⟩ 的值为假时,not 表达式的值为真;否则为假。
Notice that and and or are special forms, not procedures, because
the subexpressions are not necessarily all evaluated. Not is an
ordinary procedure.
注意,and 和 or 是特殊形式而非过程,因为其子表达式不一定全部被求值。not 则是普通的过程。
As an example of how these are used, the condition that a number x be in
the range 5
举例说明这些运算的用法:数 x 满足 5
x
10 may be expressed as
10 的条件可以表示为:
As another example, we can define a predicate to test whether one number is
greater than or equal to another as
再举一例,我们可以将"一个数大于或等于另一个数"这一谓词定义为:
or alternatively as
或者等价地写为:
(define (abs x)
(cond ((> x 0) x)
((= x 0) 0)
(( x 0) (- x)))) (cond (⟨p₁⟩ ⟨e₁⟩)
(⟨p₂⟩ ⟨e₂⟩)
…
(⟨pₙ⟩ ⟨eₙ⟩)) (⟨p⟩ ⟨e⟩) (define (abs x)
(cond (( x 0) (- x))
(else x))) (define (abs x)
(if ( x 0)
(- x)
x)) (if ⟨predicate⟩ ⟨consequent⟩ ⟨alternative⟩) (and (> x 5) ( x 10)) (define (>= x y)
(or (> x y) (= x y))) (define (>= x y)
(not ( x y)))