מבוא מורחב 1 review: scheme language things that make up scheme programs: self-evaluating...
TRANSCRIPT
מבוא מורחב 1
Review: scheme language
• things that make up scheme programs:• self-evaluating 23, "hello", #t• names +, pi• combinations (+ 2 3) (* pi 4)• special forms (define pi 3.14)
• syntax• combination: (oper-expression other-expressions …)• special form: special keyword as first subexpression
• semantics• combinations: evaluate subexpressions in any
orderapply operator to operands
• special forms: each one special
מבוא מורחב 2
Read-Eval-Print-Loop: two worlds
• visible world
• executionworld
23 23
23
eval
self-rule
prin
t
expression
value
printed representation of value
pi 3.14
3.14eval
name-rule
prin
t
value
name-rule: look up value of name in current environment
מבוא מורחב 3
Review: define special form
• define-rule:• evaluate 2nd operand only• name in 1st operand position is bound to that value• overall value of the define expression is undefined
(define pi 3.14)
eval
define-rule undefinedpr
int
scheme versions differ• visible world
• executionworld
name value
pi 3.14
"pi --> 3.14"
מבוא מורחב 4
Mathematical operators are just names
1. (+ 3 5) ==> 8
2. (define fred +) ==> undef
3. (fred 4 6) ==> 10
• How to explain this?
• Explanation:
• + is just a name
• + is bound to a value which is a procedure
• line 2 binds the name fred to that same value
מבוא מורחב 5
Primitive procedures are just values
• visible world
• executionworld
* #[compiled-procedure 8 #x583363]
eval
name-rule
prin
tA primitive procthat multiplies its
arguments
expression
value
printed representation of value
מבוא מורחב 6
Lambda: making new procedures
(lambda (x) (* x x))
expression
eval
lambda-rule
A compound procthat squares its
argument
value
#[compound-procedure 9]
prin
t
printed representation of value
7
Interaction of define and lambda
1. (lambda (x) (* x x))==> #[compound-procedure 9]
2. (define square (lambda (x) (* x x))) ==> undef
3. (square 4) ==> 16
4. ((lambda (x) (* x x)) 4) ==> 16
Syntactic Sugar:
5. (define (square x) (* x x)) ==> undef
• a convenient shorthand for 2 above
• this is a use of lambda!
מבוא מורחב 8
Lambda special form
• lambda syntax (lambda (x y) (/ (+ x y) 2))
• 1st operand position: the parameter list (x y)• a list of names (perhaps empty)• determines the number of operands required
• 2nd operand position: the body (/ (+ x y) 2)• may be any expression• not evaluated when the lambda is evaluated• evaluated when the procedure is applied
• semantics of lambda:
מבוא מורחב 9
THE VALUE OF
A LAMBDA EXPRESSION
IS
A PROCEDURE
מבוא מורחב 10
Your turn: fill in each box
•(define twice )
•(twice 2) ==> 4
(twice 3) ==> 6
•(define constant2 (lambda () 2))
• ==> 2
•(define second )
•(second 2 15 3) ==> 15
(second 34 -5 16) ==> -5
(lambda (x) (* 2 x))
(constant2)
(lambda (x y z) y)
מבוא מורחב 11
Substitution model
• a way to figure out what happens during evaluation• not really what happens in the computer
• to apply a compound procedure:
evaluate the body of the procedure, with eachparameter replaced by the corresponding operand
• to apply a primitive procedure: just do it
(define square (lambda (x) (* x x)))
1. (square 4)
2. (* 4 4)
3. 16
מבוא מורחב 12
Substitution model details
(define square (lambda (x) (* x x)))(define average (lambda (x y) (/ (+ x y) 2)))
(average 5 (square 3))(average 5 (* 3 3))(average 5 9) first evaluate operands,
then substitute (applicative order)
(/ (+ 5 9) 2)(/ 14 2) if operator is a primitive procedure, 7 replace by result of operation
13
A less trivial procedure: sum of squares
• S(n) = 02 + 12 + 22 ………. …… + n2
• Notice that S(n) = S(n-1) + n2
• Also notice that S(0) = 0
(define sum-sq (lambda (n) (if (= n 0)
0 (+ (sum-sq (- n 1)) (square n))))
• predicate = tests numerical equality (= 4 4) ==> #t (true) (= 4 5) ==> #f (false)
• if special form(if (= 4 4) 2 3) ==> 2(if (= 4 5) 2 3) ==> 3
predicate consequent alternative
מבוא מורחב 14
IF special form
(if <predicate> <consequent> <alternative>)
• When the value of <predicate> is #f, the value of the expression is the value of <alternative>. Otherwise it is the value of <consequent>
1
#t
(if (< 2 3) 1 (/ 1 0)) ==>
(if (< 3 2) 0 (> 9 8 7)) ==>
מבוא מורחב 15
(sum-sq 3)(if (= 3 0) 0 (+ (sum-sq (- 3 1)) (square 3)))(if #f 0 (+ (sum-sq (- 3 1)) (square 3)))(+ (sum-sq (- 3 1)) (square 3))(+ (sum-sq (- 3 1)) (* 3 3))(+ (sum-sq (- 3 1)) 9)(+ (sum-sq 2) 9)(+ (if (= 2 0) 0 (+ (sum-sq (- 2 1)) (square 2))) 9)..(+ (+ (sum-sq 1) 4) 9)..(+ (+ (+ (sum-sq 0) 1) 4) 9)(+ (+ (+ (if (= 0 0) 0(+ (sum-sq (- 0 1)) (square 0))) 1) 4) 9)
..(+ (+ (+ 0 1) 4) 9)..14
(define (sum-sq n) (if (= n 0) 0 (+ (sum-sq (- n 1)) (square n))))
מבוא מורחב 16
How to design recursive algorithms
• follow the general pattern:
1. wishful thinking
2. decompose the problem
3. identify non-decomposable (smallest) problems
1. Wishful thinking• Assume the desired procedure exists.
• want to implement sum-sq ? OK, assume it exists.• BUT, only solves a smaller version of the problem.
מבוא מורחב 17
2. Decompose the problem
• Solve a problem by
1. solve a smaller instance (using wishful thinking)
2. convert that solution to the desired solution
• Step 2 requires creativity! • Must design the strategy before coding.
• S(n) = S(n-1) + n2
• solve the smaller instance, add n2 to get the solution
(define sum-sq
(lambda (n) (+ (sum-sq(- n 1)) (square n))))
מבוא מורחב 18
3. Identify non-decomposable problems
• Decomposing not enough by itself• Must identify the "smallest" problems and solve directly
• S(0)=0
(define sum-sq (lambda (n)
(if (= n 0) 0
(+ (sum-sq (- n 1)) (square n)))))
מבוא מורחב 19
General form of recursive algorithms
• test, base case, recursive case
(define sum-sq (lambda (n)
(if (= n 0) ; test for base case
0 ; base case
(+ (sum-sq (- n 1)) (square n))
; recursive case
)))
• base case: smallest (non-decomposable) problem• recursive case: larger (decomposable) problem
מבוא מורחב 20
Another example
• odd?
(define odd? (lambda (n)
(not (odd? (- n 1))) ; recursive case
)))
(if (= n 0) ; test for base case #f ; base case
• See another way to write it in the notes
מבוא מורחב 21
Short summary
• Design a recursive algorithm by
1. wishful thinking
2. decompose the problem
3. identify non-decomposable (smallest) problems
• Recursive algorithms have
1. test
2. recursive case
3. base case
מבוא מורחב 22
Procedural abstraction
• Abstractions hide complexity
• Several fundamental kinds of abstraction this term
• Procedural abstraction is one:• (sum-sq n) computes S(n)• user doesn't have to think about the internals
מבוא מורחב 23
Procedural abstraction (sqrt)
To find an approximation of square root of x:• Make a guess G• Improve the guess by averaging G and x/G• Keep improving the guess until it is good enough
G = 1X = 2
X/G = 2 G = ½ (1+ 2) = 1.5
X/G = 4/3 G = ½ (3/2 + 4/3) = 17/12 = 1.416666
X/G = 24/17 G = ½ (17/12 + 24/17) = 577/408 = 1.4142156
.2for :Example xx
מבוא מורחב 24
(define (sqrt x) (sqrt-iter 1.0 x))
(define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x)))
(define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001))
(define (improve guess x) (average guess (/ x guess)))
מבוא מורחב 25
Block Structure
(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))
מבוא מורחב 26
Lexical Scoping
(define (sqrt x)
(define (good-enough? guess) (< (abs (- (square guess) x)) 0.001))
(define (improve guess) (average guess (/ x guess)))
(define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess))))
(sqrt-iter 1.0))
מבוא מורחב 27
=> (sqrt 4)
(define (good-enough? guess) (< (abs (- (square guess) 4)) 0.001)) (define (improve guess) (average guess (/ 4 guess))) (define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (sqrt-iter 1.0)
(if (good-enough? 1.0) 1.0 (sqrt-iter (improve 1.0)))
מבוא מורחב 28
(define (h x) (define (f y) (+ x y)) (define (g x) (f x)) (g (* 2 x)))
=> (h 1)
(define (f y) (+ 1 y))(define (g x) (f x))(g (* 2 1))
(f 2)(+ 1 2) 3