Javascript SICP 1.1 - The Elements of Programming

任何强大的语言,都必须有三种机制:

基本表达式:语言最基本、最简单的元!
组合的手段:如何把基本元素组合起来
抽象的手段:让组合元素命名且可操作
Programing中,我们处理两个元素:function 及 data,data是我们可以操作的东西,function是让我们描述如何操作data的规则

Evaluating Operator Combinations

对 operand (运算元) 的组合进行求值
执行一个function,function中表示operator 操作 argument, argument 就是 operand 的值
由1.可以看出,为了实现一个Combinations的求值过程,还必须先对Combinations里的每个元素进行相同的求值,这本身就是一个 “递迴”的行为
例如:
(2 + 4 * 6) * (3 + 12);

我们可以採用树的形式,每一个Combinations用一个有分支的节点表示,而operator与argument就是叶子的部分,求值过程可以想像,这些运算的值从叶子开始一直往上计算求值,然后在越来越高的层次中组合起来,可以看出 “递迴”对于处理这种层次结构的 “树”是非常有用的,称为 “tree accumulation”。

functions

数字与运算是基本的数据与function(函数)

Nesting of combinations提供了组合运算的手段

Constant declarations 把名字跟值关联起来,且提供一个有限制的抽象手段

Javascript的function declaration:

http://img2.58codes.com/2024/20117516cXB7zPDupR.png

The Substitution Model for Function Application

对一个combination求值:
http://img2.58codes.com/2024/20117516YWJwRFBZmD.png
对这个运算组合式求值,就像上述function所提,对组合式的各个元素求值,而后对arguments( the values of argument-expressions)执行一个function(value of the name)

EX: http://img2.58codes.com/2024/20117516IMWdzhurBW.png

substitution model 对 f(5)求值

applicative-order evaluation
sum_of_squares(a + 1, a * 2); -> sum_of_squares(5 + 1, 5 * 2);square(6) + square(10); -> (6 * 6) + (10 * 10);36 + 100; -> 136;
normal-order evaluation
另一种求值法 — 等到需要计算值时才计算,把function完全展开之后才求值
http://img2.58codes.com/2024/20117516R0uhIcHcdd.png

下面有一个超级酷的观察求值的function,请收看的人一定要试试,可以先猜猜function会输出什么值(或是出现什么动作)
EX 1.5
What behavior will Ben observe with an interpreter that uses applicative-order evaluation? What behavior will he observe with an interpreter that uses normal-order evaluation? Explain your answer.

http://img2.58codes.com/2024/20117516tz0sxt9wOL.png


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章