340 likes | 477 Views
CS220 Programming Principles. 프로그래밍의 이해 2003 가을학기 Class 3 한 태숙. Higher Order Procedure. Building Abstraction : assign a name to common pattern and then work in terms of the abstraction directly Accept procedure as arguments or return procedures as values (define (cube x) (* x x x))
E N D
CS220Programming Principles 프로그래밍의 이해 2003 가을학기 Class 3 한 태숙
Higher Order Procedure • Building Abstraction : assign a name to common pattern and then work in terms of the abstraction directly • Accept procedure as arguments or return procedures as values (define (cube x) (* x x x)) (define (hf f x) (f x))
A simple common pattern (* 2 2) (* 3 3) (* 4 4) is captured by (define square (lambda (x) (* x x)) (define (square x) (* x x)) • Provides a NAME to the idea of multiplying something by itself
Using lambda for Local Name • Want to compute f(x) = x2/1-x2 • We don’t want to compute x2 twice (define (f x) ((lambda (xsq) (/ xsq (- 1 xsq))) (square x)))
Syntactic sugar (define (inc x) (+ 1 x)) (define inc (lambda (x) (+ 1 x))) (define (f x) (let ((xsq (square x))) (/ xsq (- 1 xsq))))
Three Sums 1 + 2 + … + 100 12+ 22+ … + 1002 1/12 + 1/32 + … + 1/992 (define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers(+ a 1) b))))
(define (sum-squares a b) (if (> a b) 0 (+ (squarea) (sum-squares(+ a 1) b)))) (define (pi-sum a b) (if (> a b) 0 (+ (/ 1 (square a)) (pi-sum(+ a 2) b)))) • Sch-Num x Sch-Num -> Sch-Num
Type and Pattern of Procedure (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) • F x Sch-Num x F x Sch-Num -> Sch-Num • where F =(Sch-Num -> Sch-Num)
Sum as a high-order function (define (inc i) (+ i 1)) (define (sum-integers a b) (sum (lambda (x) x) a inc b)) (define (sum-squares a b) (sum square a inc b)) (define (pi-sum a b) (sum (lambda (i)(/ 1 (square i))) a (lambda (i) (+ i 2)) b))
Numerical Integration • òabf dx =[f(a)+f(a+dx)+…+f(b)]dx (define (integral f a b dx) (define (add-dx x) (+ x dx) (* (sum f a add-dx b) dx)) (integral square 0 1 0.001) (integral (lambda (x) (* x x x)) 0 1 0.001)
Exercise 1.30 (define (sum term a next b) (define (iter i result) (if < > < > (iter < > < >))) (iter < > < >)) ( > i b) result (next i) (+ (term i) result) a 0
Constructing with Lambda • (lambda (<formal-param>) <body>) (define (plus4 x) (+ x 4)) (define plus4 (lambda (x) (+ x 4))) ((lambda (x y z)(+ x y (square z))) 1 2 3) (define (integral f a b dx) (* (sum f (+ a dx) (lambda (x)(+ x dx)) b) dx))
Defining Local Variables(I) (define (f x y) (define a (+ 1 (* x y))) (define b (- 1 y)) (+ (* x (square a)) (* y b) (* a b)))
Defining Local Variables(II) (define (f x y) (define (f-helper a b) (+ (* x (square a)) (* y b) (* a b))) (f-helper (+ 1 (* x y)) (- 1 y)))
Defining Local Variables(III) (define (f x y) ((lambda (a b) (+ (* x (square a)) (* y b) (* a b))) (+ 1 (* x y)) (- 1 y)))
Defining Local Variables(IV) (define (f x y) (let ((a (+ 1 (* x y))) (b (- 1 y))) (+ (* x (square a)) (* y b) (* a b))))
Let expression (let ((<var1> <exp1>) (<var2> <exp2>) … (<varn> <expn>)) <body>) is syntactic sugar for ((lambda (<var1> … <varn>) <body>) <exp1> … <expn>)
Let Expression - Semantics • Scope of a variable is the body of the let (define x 5) (+ (let ((x 3))(+ x (* x 10))) x) ; --> 38 • Variables are bound simultaneously, using values computed outside the let (define x 2) (let ((x 3) (y (+ x 2))) (* x y)) ; -> 12
Exercise 1.34 (define (f g) (g 2)) > (f square) 4 > (f (lambda (z)(* z (+ z 1)))) 6 > (f f) ???
Compute Square Roots - Revisit • Computing Square root of x is a fixed point of the function f(y) = x/y ( = y ) • Fixed point of function f mean find y such that f(y) = y • start with a guess for y • keep applying f over and over until the result doesn’t change very much
Fix-point Operator Fixed-point (define tolerance 0.001) (define (close? u v) (< (abs (- u v)) tolerance)) (define (fixed-point f i-guess) (define (try guess) (let ((next-guess (f guess))) (if (close? next-guess guess) next-guess (try next-guess)))) (try i-guess))
Square root of X as a fixed point • Fixed Point of the function f(y) = x/y (define (sqrt x) (fixed-point (lambda (y) (/ x y)) 1)) • It does not work, because it doesn’t converge - it oscillates
Average Damping (define (average-damp f) (lambda (x) (average x (f x)))) (define (average-damp (lambda (f) (lambda (x) (average x (f x))))) • (Sch-Num -> Sch-Num) -> (Sch-Num -> Sch-Num)
Safe Square Root as a fixed point (define (sqrt x) (fixed-point (average-damp (lambda (y) (/ x y))) 1)) • Linear Iterative Process?
Conventional Approach (define (sqrt x) (define (improve guess) (average guess (/ x guess))) (define (sqrt-iter guess) (if (close? (square guess) x) guess (sqrt-iter (improve guess)))) (sqrt-iter 1))
Why use Fixed-point? • Using fixed-point approach makes us see the idea more clearly • Find cube root -> Fixed-point of y -> x/y2 (define (cuberoot x) (fixed-point (average-damp (lambda (y)(/ x (square y)))) 1))
Procedures as Returned Values • Derivative of function f : Df Df(x) = [f(x+dx) - f(x)] / dx • (Sch-Num -> Sch-Num) -> (Sch-Num -> Sch-Num) • (define (deriv f) (lambda (x) (/ (- (f (+ x dx)) (f x)) dx)))
Derivative of Fun : Df (define deriv (lambda (f) (let ((dx 0.00001)) (lambda (x) (/ (- (f (+ x dx)) (f x)) dx))))) ((deriv square) 10) ----> 20.0000…….
Newton’s Method • To find a zero of a function g such that g(y)=0 • Find a fixed-point of the function f(y) f(y) = y - g(y)/Dg(y)
Newton’s Method: program (define (newton-transform g) (lambda (y) (- y (/ (g y)((deriv g) y))))) (define (newtons-method g guess) (fixed-point (newton-transform g) guess)) • Example: • Square root of x is zero of g(y)=y2-x
Example :Square Root (define (sqrt x) (newtons-method (lambda (y) (- (square y) x)) 1.0)) (define (sqrt x) (fixed-point (average-damp (lambda (y) (/ x y))) 1))
Abstraction and First-class Proc • More general idea for square root as fixed points (define (fixed-point-of-transform g transform guess) (fixed-point (transform g) guess))
(define (sqrt x) (fixed-point-of-transform (lambda (y) (- (square y) x)) newton-transform 1)) (define (sqrt x) (fixed-point-of-transform (lambda (y) (/ x y)) average-damp 1))
First Class Citizens • May be named by variables • May be passed as arguments to procedures • May be returned as the results of procedures • May be included in data structures