A critical aspect of a programming language is the means it provides for using
names to refer to computational objects. We say that the name identifies a
编程语言的一个关键方面,是它为使用名字来指代计算对象所提供的手段。我们说,名字标识了一个
variable whose
value is the object.
In the Scheme dialect of Lisp, we name things with define. Typing
变量 (variable),其值就是该对象。在 Lisp 的 Scheme 方言中,我们用 define 来命名事物。键入
causes the interpreter to associate the value 2 with the name
size. Once
the name size has been associated with the number 2, we can refer to the
value 2 by name:
会使解释器将值 2 与名字 size 关联起来。一旦名字 size 与数 2 关联之后,我们就可以通过名字来引用值 2:
Here are further examples of the use of define:
下面是使用 define 的更多示例:
Define is our language’s simplest means of abstraction, for it allows us
to use simple names to refer to the results of compound operations, such as the
circumference computed above. In general, computational objects may
have very complex structures, and it would be extremely inconvenient to have to
remember and repeat their details each time we want to use them. Indeed,
complex programs are constructed by building, step by step, computational
objects of increasing complexity. The interpreter makes this step-by-step
program construction particularly convenient because name-object associations
can be created incrementally in successive interactions. This feature
encourages the incremental development and testing of programs and is largely
responsible for the fact that a Lisp program usually consists of a large number
of relatively simple procedures.
define 是我们语言中最简单的抽象手段,因为它允许我们用简单的名字来指代复合操作的结果,例如上面计算出的 circumference。一般来说,计算对象可能具有极为复杂的结构,每次使用时都要记住并重复其细节将是极其不便的。事实上,复杂程序正是通过一步一步地构建复杂度不断提升的计算对象来完成的。解释器使这种逐步的程序构建过程尤为方便,因为名字与对象的关联可以在一次次交互中逐步建立。这一特性鼓励程序的增量式开发与测试,也在很大程度上造就了这样一个事实:一个 Lisp 程序通常由大量相对简单的过程组成。
It should be clear that the possibility of associating values with symbols and
later retrieving them means that the interpreter must maintain some sort of
memory that keeps track of the name-object pairs. This memory is called the
应当清楚,将值与符号关联并在之后检索这些值的可能性,意味着解释器必须维护某种记忆,以追踪名字与对象之间的对应关系。这种记忆称为
environment (more precisely the
global environment, since
we will see later that a computation may involve a number of different
environments).
环境 (environment)(更准确地说是全局环境 (global environment),因为我们后面将会看到,一次计算可能涉及若干个不同的环境)。
(define size 2) size
2
(* 5 size)
10 (define pi 3.14159)
(define radius 10)
(* pi (* radius radius))
314.159
(define circumference (* 2 pi radius))
circumference
62.8318