灯下 登录
计算机科学 / SICP / subsection 中英对照

1.1.1 Expressions

One easy way to get started at programming is to examine some typical

interactions with an interpreter for the Scheme dialect of Lisp. Imagine that

you are sitting at a computer terminal. You type an

expression, and

the interpreter responds by displaying the result of its

evaluating

that expression.

开始学习程序设计的一个简便方式,是观察与 Lisp 的 Scheme 方言解释器的一些典型交互过程。想象你正坐在计算机终端前:你输入一个表达式,解释器随即显示对该表达式求值的结果。

One kind of primitive expression you might type is a number. (More precisely,

the expression that you type consists of the numerals that represent the number

in base 10.) If you present Lisp with a number

你可能输入的一类基本表达式是数。(更准确地说,你输入的表达式由代表该数的十进制数字字符组成。)如果你向 Lisp 输入一个数,

the interpreter will respond by printing

解释器将回应并打印出

Expressions representing numbers may be combined with an expression

representing a primitive procedure (such as + or *) to form a

compound expression that represents the application of the procedure to those

numbers. For example:

代表数的表达式可以和代表基本过程的表达式(如 + 或 *)组合在一起,形成代表将该过程应用于这些数的复合表达式。例如:

Expressions such as these, formed by delimiting a list of expressions within

parentheses in order to denote procedure application, are called

像这样,用括号括起一列表达式以表示过程应用的形式,被称为

combinations. The leftmost element in the list is called the

组合式 (combinations)。表中最左边的元素称为

operator, and the other elements are called

operands. The

value of a combination is obtained by applying the procedure specified by the

operator to the

arguments that are the values of the operands.

运算符 (operator),其他元素称为运算对象 (operands)。组合式的值,由将运算符所指定的过程作用于各运算对象的值——即各参数——而得到。

The convention of placing the operator to the left of the operands is known as

将运算符置于运算对象左边的约定被称为

prefix notation, and it may be somewhat confusing at first because it

departs significantly from the customary mathematical convention. Prefix

notation has several advantages, however. One of them is that it can

accommodate procedures that may take an arbitrary number of arguments, as in

the following examples:

前缀表示法 (prefix notation)。初看之下它可能令人困惑,因为它与惯用的数学约定相去甚远。然而前缀表示法有若干优点:其一,它可以容纳接受任意个参数的过程,如下例所示:

No ambiguity can arise, because the operator is always the leftmost element and

the entire combination is delimited by the parentheses.

由于运算符始终是最左边的元素,且整个组合式由括号定界,不会产生任何歧义。

A second advantage of prefix notation is that it extends in a straightforward

way to allow combinations to be nested, that is, to have combinations whose

elements are themselves combinations:

前缀表示法的第二个优点是,它以一种直接的方式延伸,允许组合式嵌套——即组合式的元素本身也是组合式:

There is no limit (in principle) to the depth of such nesting and to the

overall complexity of the expressions that the Lisp interpreter can evaluate.

It is we humans who get confused by still relatively simple expressions such as

这种嵌套的深度以及 Lisp 解释器所能求值的表达式的整体复杂程度,在原则上没有任何限制。真正感到困惑的是我们人类自己,面对如下这样相对简单的表达式也会不知所措:

which the interpreter would readily evaluate to be 57. We can help ourselves

by writing such an expression in the form

而解释器会轻而易举地求出结果为 57。我们可以将这样的表达式写成如下形式来帮助自己理解:

following a formatting convention known as

pretty-printing, in which

each long combination is written so that the operands are aligned vertically.

The resulting indentations display clearly the structure of the

expression.

这遵循一种称为美观打印 (pretty-printing) 的格式约定:每个较长的组合式都这样书写,使各运算对象在垂直方向对齐。由此产生的缩进结构清晰地展示了表达式的层次。

Even with complex expressions, the interpreter always operates in the same

basic cycle: It reads an expression from the terminal, evaluates the

expression, and prints the result. This mode of operation is often expressed

by saying that the interpreter runs in a

read-eval-print loop.

Observe in particular that it is not necessary to explicitly instruct the

interpreter to print the value of the expression.

即使面对复杂的表达式,解释器也始终在相同的基本循环中运行:从终端读入一个表达式,对该表达式求值,然后打印结果。这种运行方式常被表述为:解释器在读入-求值-打印循环(read-eval-print loop)中运行。请特别注意,这里无需显式指示解释器打印表达式的值。

Racket #lang sicp
486
Racket #lang sicp
486
Racket #lang sicp
(+ 137 349)
486

(- 1000 334)
666

(* 5 99)
495

(/ 10 5)
2

(+ 2.7 10)
12.7
Racket #lang sicp
(+ 21 35 12 7)
75

(* 25 4 12)
1200
Racket #lang sicp
(+ (* 3 5) (- 10 6))
19
Racket #lang sicp
(+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))
Racket #lang sicp
(+ (* 3
 (+ (* 2 4)
 (+ 3 5)))
 (+ (- 10 7)
 6))