MIT 6.001 SICP Notes
L1
Computer science
Scienceengineering or artComputerprocess- cs & geometry
Controlling complexity <- ==Key Idea in 6.001==
- Black-box abstraction
- Primitive objects
- Primitive procedures
- Primitive data
- Means of combination
- Procedure composition
- Construction of compound data
- Means of abstraction // using procedure as primitive
- Procedure definition
- Simple data abstraction
- Capturing common patterns // find sqrt -> find fixed point
- High-order procedures
- Data as procedures
- Primitive objects
- Conventional interfaces
- OOP
- Manifest typing // List <- Lisp
- Stream // Text & file <- Unix
- Meta-linguistic abstraction
- Making new languages
Scheme
Black-box abstraction
- primitives
+,1,2.5,#f,#t,"string"...
- means of combination
()
if...
- means of abstraction - names
(define (sq x) (* x x))
(define sq2 (λ(x) (* x x)))
(define pi 3.14)
(define plus +)
- primitives
Substitution Model
- eval the operator to get procedure
- eval the operands to get args
- apply the procedure to the args:
copy the procedure, substituting the args - eval the result //goto 1
Kinds of expressions
- Numbers
- Symbols
- Lambda expressions
- Definitions
- Conditionals
- Combinations
Peano Arithmetic
参考:皮亚诺公理
|
|
recursive fibonacci
time: O(~1.618^n) space: O(n) // longest path of tree
feeling the procedure to processes
general rule break to many instances of local rule
recursive hanoi
|
|
L2
High-order procedures
SICP 1.3
$\sum_\limits{i=a}^bi$
|
|
$\sum_\limits{i=a}^bi^2$
|
|
$\sum$ not depending upon $i$
… making complicated system and understand them, it’s crucial to divide the things up into as many pieces as I can, each of which I understand separately … having debugged it once and unserstood it once and been able to share that …
Leibnitz’s formula, pi-sum, different ways of sum
$\sum_{i=a, by~4}^b\frac1{i(i+2)}$
General pattern
|
|
Iterative sum
|
|
Heron’s square root algorithm
$y \to \frac{y+\frac{x}y}{2}$
$f(\sqrt{x})=\sqrt{x}$
Look for a Fixed-Point of $f$
|
|
|
|
how converge
$y \to \frac{x}y$
damp out oscillations
|
|
// formal parameter(parameter), actual parameter(argument)
Newton’s method
to find a $y$ such that
$f(y)=0$
start with a guess, $y0$
$y{n+1}=y_n-\frac{f(yn)}{\frac{df}{dy}\big|{y=y_n}}$
Wishful thinking, essential to good engineering…
|
|
// derivative, quotient
The rights and privileges of first-class citizens
- To be named by variables
- To be passed as arguments to procedures
- To be returned as values of procedures
- To be incorporated into data structures
Compound Data
SICP 2.1
build system in layers
rational number math
$\frac{n_1}{d_1}\frac{n_2}{d_2}=\frac{n_1n_2}{d_1*d_2}$
|
|
*rat(n1, d1, n2, d2)
// numerator, denominator
List structure
Pairs: box and pointer notation
|
|
+rat *rat -rat
make-rat / numer / denom <- abstraction layer
pairs
|
|
|
|
Data Abstration -> seperate the use and representation
If you have the name of the spirit, you get control over it
Deferring decisions
To reatin flexibility, never make up your mind about anything until you’re forced to do it. Use abstractions.
Layered system:
L1. segments # is pairs of vectors
constructors/selectors
L2. vectors # is pairs of numbers
constructors/selectors
L3. pairs
calc the length of segment
- calc by vectors
- calc by pairs <- hard to read, hard to change
What a rational number is really?
Axiom for rational number:
if x = (make-rat n d)
then (numer x)/(denom x) = n/d
What are Pairs really?
Axiom for pairs:
For any x and y
(car (cons x y)) is x
(cdr (cons x y)) is y
==spoiler alert==
|
|
- reinfore the idea of abstraction
- introduce an idea about blurring the line between procedure and data
L3
SICP 2.2
Closure
- elements is said to be closed under an operation if applying the operation to elements in the set produces an element that is again an element of the set
- an implementation technique for representing procedures with free variables not use this sense in this book
LIST
Conventional Interface(1,*)->(2,*)->(3,*)->(4,)
// conventional(list 1 2 3 4)
// syntactic sugar
General pattern to process list
map
12345(define (map p l)(if (null? l)nil(cons (p (car l)) # cons up(map p (cdr l))))) # cdring downfor-each
Escher - Square Limit
Tackling complex system: build a language
- primitives
- picture
- means of combination
- rotate, flip, beside, above
- mean of abstraction
- primitives
embedded languages
making full use of surrounding languageTwo kinds of design
Layers of language
Language of Schemes of Combination
Language of Geometric Positions
Language of Primitive Pictures- full range of linguistic power at each level
- more robust
- vocabularies fro talking about the degsign at different levels
Trees of task
- each node representing a specific task
The powerful of Lisp
the design process is not so much implementing programs as implementing languages
Symbolic Differentiation
SICP 2.3
- Robust system
Insensitive to small changes, the space of solutions ought to be a continuous in this space of problems
- How-to
Solve a problem at every level of
decomposition of the problem at the subproblems- Solve the class of problems which are a neightborhood of the particulare problem, by producing a language at that level of detail in which the solutions to that class of problems is representable in that language
make changes to the problems == change the solution constructed by that language
Rest of the lecture:
Translate derivative rules to lisp
L4
Pattern-maching: Rule-based Substitution
|
|
|
|
|
|
- Matcher
|
|
Pattern: (+ (* (? x) (? y)) (? y))
Expression: (+ (* 3 x) x)
traverse two tees simultaneously, result: matched, dict{x=3, y=’x}
- Instantiate
|
|
- Simplifier
// GIGO
|
|
- Dictionary
Generic Operators
SICP 2.4
- Horizontal barrier: between use(interface) and representation(implementation)
- Vertical barrier: between different representations
Complex number arithmetic
// real-part, imaginary-part, magnitude, angle
Two representations
- Polar // easy to multiply
- Rectangular // easy to add
Typed data
(define (attach-type type contents) (cons type contents))
Generic operator
- dispatch on type
- a manager check the type and call the right procedures
data-directed programming
- use a table to replace manager
(put key1 key2 value)
(get key1 key2 value)
~ | polar | rectangular
— | — | —
real-part | real-polar | real-rect
imag-part | ~ | ~
mag | mag-polar | mag-rect
angle | ~ | ~123456(define (operate op obj)(let((proc (get (type obj) op)))(if (not (null? proc))(proc (contents obj))(error "undefined op"))))- use a table to replace manager
- dispatch on type
Generic ADD/SUB/MUL/DIV
- operate rational, complex, ordinary, polynomial …
- chains of types
- (complex,)->(rect,)->(1,2)
- (poly,)->(poly,)->…->(complex,)->(rect,)->(2,3)
L5
SICP 3.1 & 3.2
- Variable
- Introduce time in procedure
- Functional ~ Imperative
|
|
- Substitution model not working
Environment model
- Bound variables & Free variables
(λ(x) (* x y))
y
is free,*
is free,x
is bound
- Scope
- bind by lambda
- Environment
- Sequence of linked frames
- Frame
- Map variables to values //on name conflict, child’s shadow parent’s
- Procedure
- Compound of procedure code and environment
- Bound variables & Free variables
Why assignment
- for objects with local state
- for decomposition
SICP 3.3
- Circuits simulator
- Agenda
- Mutable CONS