SICP:1.1.3 Evaluating Combinations
SICP:1.1.3 Evaluating Combinations
本文逐段翻译 SICP 第二版 §1.1.3《Evaluating Combinations》,保留 evaluation rules、Scheme code、Figure 1.1 和 special forms 的原文位置与结构。
TL;DR
- 本章的目标之一,是把关于 procedural thinking 的问题隔离出来;在 evaluation combinations 时,interpreter 自身也在遵循一个 procedure。
- Combination 的 evaluation rule 是递归的:先 evaluation 所有 subexpressions,再把 operator 的 value 作为 procedure 应用到 operands 的 values 上。
- Environment 决定 symbols 的 meaning;
define不遵循普通 combination rule,而是一个 special form,拥有自己的 evaluation rule。
目录
| 章节 | 说明 |
|---|---|
| 1.1.3 Evaluating Combinations | combination 的递归 evaluation rule |
| Tree accumulation | 从 terminal nodes 向上累积 values |
| Primitive expressions | numerals、built-in operators 与 names |
| Special forms | define 与独立 evaluation rules |
1.1.3 Evaluating Combinations
本章的目标之一,是把关于 procedural thinking 的问题隔离出来。作为一个例子,考虑 combination 的 evaluation:此时 interpreter 自己也在遵循一个 procedure。
要 evaluation 一个 combination,请执行以下步骤:
- Evaluation combination 的 subexpressions。
- 把左侧 subexpression(即 operator)的 value 作为 procedure,应用到其他 subexpressions(即 operands)的 values,也就是 arguments 上。
即使是这个 simple rule,也展示了关于 processes 的一些 important points。首先注意,第一步规定:为了完成一个 combination 的 evaluation process,我们必须先对 combination 的每个 element 执行 evaluation process。因此,evaluation rule 在本质上是 recursive 的;也就是说,它的步骤之一就是再次 invoke 该 rule1。
Tree accumulation
递归可以非常 succinct 地表达一种 process;如果没有递归,一个 deeply nested combination 的 evaluation 可能会被看作相当 complicated。例如,evaluation:
(* (+ 2 (* 4 6))
(+ 3 5 7))
要求对四个不同的 combinations 应用 evaluation rule。我们可以把这个 process 表示为一个 tree,如 Figure 1.1 所示。每个 combination 表示为一个 node;从该 node 生出的 branches 对应 combination 的 operator 和 operands。
Terminal nodes(即没有 branches 从其中生出的 nodes)表示 operators 或 numbers。用 tree 的角度观察 evaluation,我们可以想象:operands 的 values 从 terminal nodes 开始向上 percolate,在越来越高的 levels 上不断组合。一般而言,我们会看到,recursion 是处理 hierarchical、tree-like objects 的一种 very powerful technique。事实上,“percolate values upward”形式的 evaluation rule,就是一种称为 tree accumulation 的 general process 的例子。

Primitive expressions
接着注意,对第一步的 repeated application 会把我们带到这样一个阶段:我们需要 evaluation 的不再是 combinations,而是 primitive expressions,例如 numerals、built-in operators 或其他 names。我们通过规定以下规则来处理 primitive cases:
- Numerals 的 values,就是它们所命名的 numbers;
- Built-in operators 的 values,就是执行相应 operations 的 machine-instruction sequences;
- Other names 的 values,就是 environment 中与这些 names 关联的 objects。
我们可以把第二条 rule 看作第三条 rule 的 special case:规定 + 和 * 这样的 symbols 也包含在 global environment 中,并且与作为它们 “values” 的 machine-instruction sequences 关联。
这里需要注意的关键点,是 environment 在决定 expressions 中 symbols 的 meaning 时所扮演的 role。在 Lisp 这种 interactive language 中,如果不指定能够为 symbol x(甚至 symbol +)提供 meaning 的 environment information,那么谈论 expression (+ x 1) 的 value 是没有意义的。正如 Chapter 3 将要展示的,environment 作为 evaluation 发生时的 context 的 general notion,对理解 program execution 会起到 important role。
Special forms
注意,上面给出的 evaluation rule 不处理 definitions。例如,evaluation (define x 3) 并不会把 define 应用到两个 arguments 上:其中一个是 symbol x 的 value,另一个是 3。这是因为 define 的 purpose 正是把 x 与一个 value 关联起来。(也就是说,(define x 3) 不是一个 combination。)
这种偏离 general evaluation rule 的 exceptions 称为 special forms。到目前为止,define 是我们看到的唯一 special form,但很快还会遇到其他 special forms。每一种 special form 都有自己的 evaluation rule。
不同 kinds 的 expressions(每种 expression 都带有相应的 evaluation rule)构成 programming language 的 syntax。与大多数其他 programming languages 相比,Lisp 的 syntax 非常 simple;也就是说,expressions 的 evaluation rule 可以由一个 simple general rule,加上少数 special forms 的 specialized rules 来描述2。
图与格式复核
- Figure 1.1:保留原文 tree representation,使用公开 HTML 中的原始 GIF,尺寸为
292 × 247,嵌入在 tree accumulation 段落之后。 - Scheme combination、primitive expressions、
define和(+ x 1)均保留原文 code/inline-code 形式。 - 原文的两个 evaluation steps、terminal nodes、environment role 和 special forms 顺序保持不变。
术语对照
| English | 译法/处理 |
|---|---|
| evaluate / evaluation | 保留 evaluation;必要处译作求值 |
| combination | 保留 combination;必要处译作组合式 |
| subexpression | 保留 subexpression;必要处译作子表达式 |
| operator / operand / argument | 保留 programming-language terms |
| recursive / recursion | 保留 recursive/recursion;必要处译作递归 |
| tree accumulation | 保留 tree accumulation;必要处译作树累积 |
| primitive expression | 保留 primitive expression |
| special form | 保留 special form;必要处译作特殊形式 |
| syntax | 保留 syntax;必要处译作语法 |
| environment | 保留 environment;必要处译作环境 |
参考资料
- Harold Abelson、Gerald Jay Sussman、Julie Sussman:SICP 第二版 MIT Press 页面
- SICP HTML full text
- MIT OCW 6.001 Readings
评论 (0)