欢迎使用CSDN-markdown编辑器

Scheme笔记——Building Abstractions with Procedures (1)

主要是培养抽象思维,顺带学英语

一、表达式

1.第一种表达式:数(以10为基数,10进制)

One kind of primitive expression you might type is a number
486
你可以键入的一种基本表达式就是数

2.第二中表达式:组合式

Expressions such as these, formed by delimiting a list of expressions within parentheses in order to denote procedure application, are called combinations
(+ 137 349)
486
像上面这样的表达式称为组合式,其构成方式就是用括号括起一些表达式,形成一个表,用于表示一个过程应用

2.1运算符operator && 运算对象operands

The leftmost element in the list is called the operator, and the other elements are called operands.
表里最左边的元素称为运算符,其他元素都称为运算对象。

2.2实际参数

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.
要得到这种组合式的值,采用的方式就是将由运算符所刻画的过程应用于有关的实际参数,而所谓的实际参数也就是那些运算对象的值。

2.3前缀表示prefix nonation

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.
将运算符放在所有运算对象左边,这种表示称为前缀表示。刚开始看到这种表示时会感到不习惯,因为它与常规数学表示差别很大。

2.4输入的打印格式 pretty-printing

We can help ourselves by writing such an expression in the form following a formatting convention known as pretty-printing, in which each long combination is written so that the operands are aligned vertically.
(+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))
(+ (* 3
         (+ (* 2 4)
              (+ 3 5)))
     (+ (- 10 7)
          6))
美观打印规则,令各个运算对象垂直对齐。

二、命名和规则 Naming and the Environment

1.变量variable

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.
程序设计语言中一个必不可少的方面,就是它需要提供一种通过名字去使用计算对象的方式。我们将名字标识符称为变量,它的值也就是它所对应的那个对象。

2.最简单的抽象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.
(define pi 3.14159)
(define radius 10)
(* pi (* radius radius))
314.159
(define circumference (* 2 pi radius))
circumference
62.8318
define是我们所用的语言里最简单的抽象,它允许我们用一个简单的名字去引用一个组合运算的结果,例如上面算出的circumference.

3.环境 environment

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).
应该可以看到,我们可以把值与符号关联起来,而后又能提取这些值,意味着解释器必须维护某种存储能力,以便保持有关的名字-值对偶的轨迹。这种存储被称为环境(更精确地说是全局环境,因为我们以后将看到,在一个计算过程中完全可能涉及若干不同环境)。

三、组合式的求值 Evaluating Combinations

1.求值组合式的过程

To evaluate a combination, do the following:
1. Evaluate the subexpressions of the combination.
2. Apply the procedure that is the value of the leftmost subexpression (the operator) to the arguments that are the values of the other subexpressions (the operands).
要求值一个组合式,做下面的事情:
1.求值该组合式的各个子表达式
2.将作为最左子表达式(操作符)的值的过程应用于相应的实际参数,而所谓的实际参数也就是其他子表达式(运算对象)的值。

2.递归recursive

Thus, the evaluation rule is recursive in nature; that is, it includes, as one of its steps, the need to invoke the rule itself.
因此,在性质上,这一求值过程是递归的,也就是说,它在自己的工作步骤中,包含着调用这个规则本身的需要。

3.树形积累

Viewing evaluation in terms of the tree, we can imagine that the values of the operands percolate upward, starting from the terminal nodes and then combining at higher and higher levels. In general, we shall see that recursion is a very powerful technique for dealing with hierarchical, treelike objects. In fact, the “percolate values upward” form of the evaluation rule is an example of a general kind of process known as tree accumulation.
以树的观点看这种求值过程,可以设想那些运算对象的值向上穿行,从终端结点开始,而后在越來越高的层次中组合起来。一般而言,我们应该把递归看作是一种处理层次结构(像树这样的对象)强有力的技术。事实上,“值向上穿行”形式的求值形式是一类更一般形式的求值过程的一个例子,这种计算过程称为树形积累。

4.处理基础情况的规定

Next, observe that the repeated application of the first step brings us to the point where we need to evaluate, not combinations, but primitive expressions such as numerals, built-in operators, or other names. We take care of the primitive cases by stipulating that
the values of numerals are the numbers that they name,
● the values of built-in operators are the machine instruction sequences that carry out the corresponding operations, and
● the values of other names are the objects associated with those names in the environment.
We may regard the second rule as a special case of the third one by stipulating that symbols such as +and * are also included in the global environment, and are associated with the sequences of machine instructions that are their “values.” The key point to notice is the role of the environment in determining the meaning of the symbols in expressions.
进一步的观察告诉我们,对第一个步骤的反复应用,总可以把我们带到求值中的某一点,在这里遇到的不是组合式,而是基本表达式,例如数,内部运算符或者其他名字。处理这些基础情况的方式如下规定:
●数的值就是它们所表示的数值
●内部运算符的值就是能完成相应操作的机器指令序列
●其他名字的值就是在环境中关联于这一名字的那个对象
我们可以将第二种规定看作是第三种规定的特殊情况,为此只需要将像+和*一类的运算符也包含在全局环境里,并将相应的指令序列作为与之关联的“值”。需要指出的关键一点是,环境所扮演的角色就是用于确定表达式中各个符号的意义。

5.特殊形式special forms

Notice that the evaluation rule given above does not handle definitions.
For instance, evaluating (define x 3) does not apply define to two arguments, Since the purpose of the define is precisely to associate x with a value. (That is, (define x 3) is not a combination.)Such exceptions to the general evaluation rule are called special forms. Define is the only example of a special form that we have seen so far, but we will meet others shortly.That is, the evaluation rule for expressions can be described by a simple general rule together with specialized rules for a small number of special forms.
请注意,上面给出的求值规则里并没有处理定义。例如,对(define x 3)的求值并不是将define应用于它的两个实际参数。这是因为define的作用就是为x关联1个值(也就是说,(define x 3)并不是一个组合式)。一般性求值规则的这种例外称为特殊形式,define是至今我们已经看到的唯一的一种特殊形式,下面还将看到另外的一些特殊形式。也就是说,对各种表达式的求值规则可以描述为一个简单的通用规则和一组针对不多的特殊形式的专门规则。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值