Building Abstractions With Procedures

     今天准备开始研究下SICP,学习下函数式编程的基本思想,也拓展下自己的视野。这本书是MIT的本科生教材,一共就五章,希望能坚持读完。

第一章:Building Abstractions with Procedures

       Lisp is an acronym for LISt Processing, was designed to provide symbol-manipulation capabilities for attacking programming problems such as the symbolic differentiation and integration of algebraic expressions.The dialect of Lisp used in this book is called Scheme.

      If Lisp is not a mainstream language, why are we using it as the framework for our discussion of programming? Because the language possesses unique features that make it an excellent medium for studying important programming constructs and data structures and for relating them to the linguistic features that support them. The most significant of these features is the fact that Lisp descriptions of processes, called procedures, can themselves be represented and manipulated as Lisp data.The importance of this is that there are powerful program-design techniques that rely on the ability to blur the traditional distinction between ``passive'' data and ``active'' processes.

1.1 The Elements of Programming

      A powerful programming language is more than just a means for instructing a computer to perform tasks . The language also serves as a framework within which we organize our ideas about processes. Thus, when we describe a language, we should pay particular attention to the means that  the language provides for combining simple ideas to form more complex ideas. Every powerful language has three mechanisms for accomplishing this:

 primitive expressions, which represent the simplest entities the language is concerned with,

 means of combination, by which compound elements are built from simpler ones, and

  means of abstraction, by which compound elements can be named and manipulated as units.

1.1.1 Expressions

486  (+ 12 34)  (* 21 23) (* 3 5 7 12)  

(+ (* 3 5) (/ 6 2))  

1.1.2 Naming and  the Environment

      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.

eg:

(define pi 3.14159)

(define radius 10)

(* pi (* radius radius))

314.159

(define circumference (* 2 pi radius))circumference

62.8318

     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).

1.1.3 Evaluating Combinations

     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.1.4 Compound Procedures

(define (square x) (* x x))  

(define (sum-of-squares x y)  (+ (square x) (square y)))

1.1.5 The Substitution Model for Procedure Application

       This alternative ``fully expand and then reduce'' evaluation method is known as normal-order evaluation, in contrast to the ``evaluate the arguments and then apply'' method that the interpreter actually uses, which is called applicative-order evaluation.

1.1.6 Conditional Expressions and Predicates

eg1:

(define (abs x)        

  (cond ((> x 0) x)

             ((= x 0) 0)

             ((< x 0) (- x))))

eg2:

(define (abs x)

  (cond ((< x 0) (- x))

              (else x)))

eg3:

(define (abs x)

  (if (< x 0)

      (- x)

      x))

eg4:

(and <e1> ... <en>)  (or <e1> ... <en>)  (not <e>)

local names:

       A formal parameter of a procedure has a very special role in the procedure definition, in that it doesn't matter what name the formal parameter has. Such a name is called a bound variable, and we s;≠r‘®´‘ººęƒ¬ay that the procedure definition binds its formal parameters. If a variable is not †® boº und, we say that it is free.

Internal definitions and block structures

(define (sqrt x)

  (define (good-enough? guess x)  (< (abs (- (square guess) x)) 0.001))

  (define (improve guess x) (average guess (/ x guess)))

  (define (sqrt-iter guess x)  (if (good-enough? guess x)  guess (sqrt-iter (improve guess x) x)))

  (sqrt-iter 1.0 x))

1.2 Procedures and Processes They Generate

Linear Recursion and Iteration

The contrast between the two processes can be seen in another way. In the iterative case, the program variables provide a complete description of the state of the process at any point. If we stopped the computation between steps, all we would need to do to resume the computation is to supply the interpreter with the values of the three program variables. Not so with the recursive process.

(define (factorial n)

  (if (= n 1)  1 (* n (factorial (- n 1)))))

(define (factorial n) (fact-iter 1 1 n))

(define (fact-iter product counter max-count)

  (if (> counter max-count) product (fact-iter (* counter product) (+ counter 1) max-count)))

Tree Recursion

Thus, the process uses a number of steps that grows exponentially with the input. On the other hand, the space required grows only linearly with the input, because we need keep track only of which nodes are above us in the tree at any point in the computation. In general, the number of steps required by a tree-recursive process will be proportional to the number of nodes in the tree, while the space required will be proportional to the maximum depth of the tree.

1.3 Formulating Abstractions with Higher-Order Procedures

     Yet even in numerical processing we will be severely limited in our ability to create abstractions if we are restricted to procedures whose parameters must be numbers. Often the same programming pattern will be used with a number of different procedures. To express such patterns as concepts, we will need to construct procedures that can accept procedures as arguments or return procedures as values. Procedures that manipulate procedures are called higher-order procedures.

Constructing procedures using Lambda

(lambda (x) (+ x 4))   (lambda (x) (/ 1.0 (* x (+ x 2))))

(define (plus4 x) (+ x 4)) is equivalent to (define plus4 (lambda (x) (+ x 4)))


Finding roots of equations by the half-interval method


Procedures As Return Values


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,142评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,298评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,068评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,081评论 1 291
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,099评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,071评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,990评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,832评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,274评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,488评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,649评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,378评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,979评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,625评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,643评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,545评论 2 352