目录
正在加载目录…
正在阅读专栏 SICP:Structure and Interpretation of Computer Programs 第 6 篇 · 共 80 篇 · 8%

SICP:1.1.6 Conditional Expressions and Predicates

发布于 2026-09-18 08:12 · 最后编辑于 2026-09-18 08:19 · 版本 v2 · 字数 4,766 · 预计阅读 10 分钟 👁 15 次阅读

SICP:1.1.6 Conditional Expressions and Predicates

本文逐段翻译 SICP 第二版 §1.1.6《Conditional Expressions and Predicates》,保留 cond、if、logical composition、Scheme code、图片以及 Exercises 1.1–1.5 的原文顺序。

TL;DR

  • 目前能够定义的 procedures 缺少 test 与分支能力;cond special form 用于表达 case analysis。
  • if 是只有两个 cases 时使用的 restricted conditional;and 与 or 采用 short-circuit evaluation,因此是 special forms,而 not 是 ordinary procedure。
  • Predicates 可以由 primitive predicates 组合而成;本节最后的 Exercises 1.1–1.5 用来练习这些 evaluation rules。

目录

章节说明
1.1.6 Conditional Expressions and Predicatesconditional expressions 与 predicates 的引入
cond 与 case analysis多分支 conditional
if expression二分支 conditional
Logical compositionand、or 与 not
Exercises 1.1–1.5本节练习

1.1.6 Conditional Expressions and Predicates

cond 与 case analysis

在此时我们能够定义的 procedures 这一 class,其 expressive power 还非常 limited,因为我们没有办法执行 tests,也没有办法根据 test 的 result 执行不同的 operations。例如,我们无法通过测试一个 number 是 positive、negative 还是 zero,并按照不同 cases 采取不同 actions 的 rule,来定义一个计算 number 的 absolute value 的 procedure:

SICP absolute value rule

这个 construct 称为 case analysis,Lisp 提供了一个 special form 来表示这样的 case analysis。它称为 cond(表示“conditional”),使用方式如下:

scheme
(define (abs x)
  (cond ((> x 0) x)
        ((= x 0) 0)
        ((< x 0) (- x))))

Conditional expression 的 general form 是:

scheme
(cond (<p1> <e1>)
      (<p2> <e2>)
      ...
      (<pn> <en>))

它由 symbol cond 以及一组用 parentheses 包围的 expression pairs (<p> <e>) 组成;这些 pairs 称为 clauses。每个 pair 中的第一个 expression 是一个 predicate——也就是说,它是一个 value 会被解释为 true 或 false 的 expression1。

Conditional expressions 的 evaluation 规则如下。首先 evaluation <p1>。如果它的 value 是 false,就 evaluation <p2>。如果 <p2> 的 value 也为 false,就 evaluation <p3>。这个 process 会持续进行,直到找到一个 value 为 true 的 predicate;此时,interpreter 返回该 clause 中对应的 consequent expression <e> 的 value,作为 conditional expression 的 value。如果没有任何一个 <p> 被发现为 true,那么 cond 的 value 就是 undefined。

Predicate 这个词既用于返回 true 或 false 的 procedures,也用于 evaluation 后得到 true 或 false 的 expressions。Absolute-value procedure abs 使用了 primitive predicates >、< 和 =2。这些 predicates 接受两个 numbers 作为 arguments,分别测试第一个 number 是否大于、小于或等于第二个 number,并相应地返回 true 或 false。

另一种写 absolute-value procedure 的方式是:

scheme
(define (abs x)
  (cond ((< x 0) (- x))
        (else x)))

用 English 表达,就是“如果 x 小于 zero,就返回 - x;否则返回 x”。else 是一个 special symbol,可以在 cond 的 final clause 中代替 <p> 使用。当所有 previous clauses 都被 bypass 时,它会使 cond 返回对应 <e> 的 value。实际上,这里的 <p> 可以使用任何总是 evaluation 为 true value 的 expression。

if expression

还可以用另一种方式写 absolute-value procedure:

scheme
(define (abs x)
  (if (< x 0)
      (- x)
      x))

这里使用了 special form if;当 case analysis 中恰好有两个 cases 时,可以使用这种 restricted type 的 conditional。if expression 的 general form 是:

scheme
(if <predicate> <consequent> <alternative>)

要 evaluation 一个 if expression,interpreter 首先 evaluation expression 的 <predicate> 部分。如果 <predicate> evaluation 为 true value,interpreter 接着 evaluation <consequent>,并返回它的 value。否则,interpreter evaluation <alternative>,并返回它的 value3。

Logical composition

除了 <、= 和 > 这样的 primitive predicates 外,还有 logical composition operations,使我们能够构造 compound predicates。最常用的三个如下。

and

scheme
(and <e1> ... <en>)

Interpreter 按照从左到右的顺序,一次 evaluation 一个 expressions。如果任何一个 <e> evaluation 为 false,and expression 的 value 就是 false,其余的 <e> 不再 evaluation。如果所有 <e> 都 evaluation 为 true values,and expression 的 value 就是最后一个 <e> 的 value。

or

scheme
(or <e1> ... <en>)

Interpreter 按照从左到右的顺序,一次 evaluation 一个 expressions。如果任何一个 <e> evaluation 为 true value,就返回该 value 作为 or expression 的 value,其余的 <e> 不再 evaluation。如果所有 <e> 都 evaluation 为 false,or expression 的 value 就是 false。

not

scheme
(not <e>)

当 expression <e> evaluation 为 false 时,not expression 的 value 是 true;否则 value 是 false。

注意,and 和 or 是 special forms,而不是 procedures,因为它们的 subexpressions 不一定会全部被 evaluation。not 则是一个 ordinary procedure。

作为这些 operations 如何使用的例子,可以把 number x 位于范围 5 < x < 10 内的 condition 表达为:

scheme
(and (> x 5) (< x 10))

另一个例子是,我们可以定义一个 predicate,测试一个 number 是否大于或等于另一个 number:

scheme
(define (>= x y)
  (or (> x y) (= x y)))

或者也可以写成:

scheme
(define (>= x y)
  (not (< x y)))

Exercises 1.1–1.5

Exercise 1.1

下面是一组 expressions。对于每个 expression,interpreter 会打印出什么 result?假设这组 expressions 按照给出的顺序依次 evaluation。

scheme
10
(+ 5 3 4)
(- 9 1)
(/ 6 2)
(+ (* 2 4) (- 4 6))
(define a 3)
(define b (+ a 1))
(+ a b (* a b))
(= a b)
(if (and (> b a) (< b (* a b)))
    b
    a)
(cond ((= a 4) 6)
      ((= b 4) (+ 6 7 a))
      (else 25))
(+ 2 (if (> b a) b a))
(* (cond ((> a b) a)
         ((< a b) b)
         (else -1))
   (+ a 1))

Exercise 1.2

将下面的 expression 翻译为 prefix form:

SICP Exercise 1.2

Exercise 1.3

定义一个 procedure,它接受三个 numbers 作为 arguments,并返回其中两个较大 numbers 的 squares 之和。

Exercise 1.4

注意,我们的 evaluation model 允许 combination 的 operators 是 compound expressions。利用这一观察,描述下面 procedure 的 behavior:

scheme
(define (a-plus-abs-b a b)
  ((if (> b 0) + -) a b))

Exercise 1.5

Ben Bitdiddle 发明了一个 test,用来判断他面对的 interpreter 使用的是 applicative-order evaluation 还是 normal-order evaluation。他定义了以下两个 procedures:

scheme
(define (p) (p))

(define (test x y)
  (if (= x 0)
      0
      y))

然后他 evaluation expression:

scheme
(test 0 (p))

如果 interpreter 使用 applicative-order evaluation,Ben 会观察到什么 behavior?如果 interpreter 使用 normal-order evaluation,他会观察到什么 behavior?解释你的 answer。

假设无论 interpreter 使用 normal-order 还是 applicative-order,special form if 的 evaluation rule 都相同:先 evaluation predicate expression,再根据 result 决定 evaluation consequent 还是 alternative expression。

图与格式复核

  • 绝对值规则图:保留原文 GIF,尺寸为 123 × 54,嵌入在 case analysis 引入段落之后。
  • Exercise 1.2 图:保留原文 GIF,尺寸为 165 × 36,嵌入在 Exercise 1.2 题干之后。
  • cond、if、and、or、not 的 code blocks、evaluation order 与 Exercises 1.1–1.5 的顺序保持不变。

术语对照

English译法/处理
conditional expression保留 conditional expression;必要处译作条件表达式
predicate保留 predicate;必要处译作谓词
case analysis保留 case analysis;必要处译作分情况分析
clause保留 clause;必要处译作子句
consequent expression保留 consequent expression;必要处译作后件表达式
special form保留 special form;必要处译作特殊形式
logical composition保留 logical composition;必要处译作逻辑组合
short-circuit evaluation保留 short-circuit evaluation;必要处译作短路求值
primitive predicate保留 primitive predicate;必要处译作 primitive 谓词
alternative保留 alternative;必要处译作备选分支

参考资料

  1. 原文脚注说明,在 Scheme 中,只有 #f 被视为 false;其他 values 都被视为 true。本篇保留原文脚注位置。 ↩

  2. 原文脚注说明,predicate 一词也可用于返回 true 或 false 的 procedure;primitive predicates 的具体表示依赖于 language。 ↩

  3. 原文脚注说明,if expression 的 predicate 必须得到 true 或 false 的判断结果;在 Scheme 中,除 #f 外的 values 都按 true 处理。 ↩

← 返回列表
← 上一篇 SICP:1.1.5 Substitution Model 下一篇 → SICP:1.1.7 Example: Square Roots by Newton's Method

评论 (0)

暂无评论,来留下第一条吧。
登录 或 注册 后才能发表评论