《SICP》读书笔记--Chapter 1 Building Abstraction with Procedures

来源:互联网 发布:迪杰斯特拉算法 编辑:程序博客网 时间:2024/05/01 08:03

Forward

Focus on: the human mind, collections of computer programs, and the computer. 

Every computer program is a model, hatched in the mind, of a real or mental process.


Chapter 1 Building Abstractions with Procedures

The acts of mind, wherein it exerts its power over simple ideas, are chiefly these three:

1. Combining several simple ideas into one compound once, and thus all complex ideas are made

2. bringing two ideas, whether simple or complex, together, and setting them by one another so as to take a view of them at once, without uniting them into one(做对比,modularity?)

3. separating them from all other ideas that accompany them in their real existence --abstraction


注:这三个方向,是作为library的提供者,framework的提供者需要思考的问题,让使用者通过什么样的方式compound, abstraction


1.1 The elements of programming

A powerful programming language is more than just a means for instructing a computer to perform task.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 themeans that the language provide for combining simple ideas to form more complex ideas:

1. primitive expression

2. means of combination

3. means of abstraction


1.1.1 Expressions

1.1.2 Naming and the Environment

(define size 2): simplest means of abstractions[ABSTRACTION]

1.1.3 Evaluating Combinations

goal: think procedurally

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

1.1.4 Compound Procedures

(define (square x) (* x x)) [COMPOUND]

1.1.5 The Substitution Model for Procedure Application

Applicative vs Normal Order

1.1.6 Conditional Expressions and Predicates

1.1.7 Example: Square Roots by Newton's Method

Important difference between mathematical functions and computer procedures: Procedures must be effective.

function: describing properties of things / declarative knowledge

procedure: how to do things / imperative knowledge

1.1.8 Procedures as Black-Box Abstractions

when we define good-enough? procedure in terms of square, we are able to regard thesquare procedure as a 'black box'. We are not at the moment concerned with how the procedure computes its result, only with the fact that it computes square. 'square' is aprocedural abstraction


1.2 Procedures and the Process They Generate

1.2.1 Linear Recursion and Iteration

n! = n * (n-1)!

recursive process: a chain of deferred operations. n!length of the chain is grows linearly with n.

iterative process: whose state can be summarized by a fixed number ofstate variables, together with a fixed rule that describes how the state variables should be updated as the process moves from one state to another. n! the number ofsteps required is growing linearly with n


NOTE: NOT CONFUSE recursive process with recursive procedure

recursive procedure: procedure definition refers to itself.

recursive process: how the process involved, not the syntax of procedure is written


1.2.2 Tree Recursion

fib(n) = fib(n-1) + fib(n-2)

1.2.3 Orders of Growth

1.2.4 Exponentiation

1.2.5 Greatest Common Divisor


1.3 Formulating Abstractions with Higher-Order Procedures


1.3.1 Procedures as Arguments [ABSTRACTION]

ProceduresOne of things we should demand from a powerful programming language is the ability to buildabstractions by assigning names to common patterns and then to work in terms of the abstraction directly.


Procedure Abstraction:separate the way the procedure would be used from the details of how the procedure would be implemented in terms of more primitive procedures.


High-Order Procedures: procedures that manipulate procedures. power abstraction mechanism. limited our ability to create abstractions if we are restricted to procedures whose parameters must be numbers. [IMPORTANT]


(define (sum-integers a b)

   (if (> a b)

      0

      (+ a (sum-integers (+ a 1) b))))


(define (sum-cubes a b)

   (if (> a b)

      0

      (+ (cube a) (sum-cubes (+ a 1) b))))


1/(1*3) + 1/(5*7) + ...

(define (pi-sum a b)

   (if (> a b)

      0

      (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))


General Procedure Abstraction:

(define (sum term a next b)

   (if (> a b)

      0

      (+ (term a)

         (sum term (next a) next b))))


Implement Cube Sum by General Procedure

(define (sum-cubes a b)

   (sum cube a inc b))


1.3.2 Constructing Procedures Using Lambda

lambda is used to create procedures in the same way as define, except that no name is specified for the procedure


1.3.3 Procedures as General Methods

1.3.4 Procedures as Returned Values

0 0