1 / 34

CS220 Programming Principles

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

Download Presentation

CS220 Programming Principles

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS220Programming Principles 프로그래밍의 이해 2003 가을학기 Class 3 한 태숙

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

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

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

  5. Syntactic sugar (define (inc x) (+ 1 x)) (define inc (lambda (x) (+ 1 x))) (define (f x) (let ((xsq (square x))) (/ xsq (- 1 xsq))))

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

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

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

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

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

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

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

  13. Defining Local Variables(I) (define (f x y) (define a (+ 1 (* x y))) (define b (- 1 y)) (+ (* x (square a)) (* y b) (* a b)))

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

  15. Defining Local Variables(III) (define (f x y) ((lambda (a b) (+ (* x (square a)) (* y b) (* a b))) (+ 1 (* x y)) (- 1 y)))

  16. Defining Local Variables(IV) (define (f x y) (let ((a (+ 1 (* x y))) (b (- 1 y))) (+ (* x (square a)) (* y b) (* a b))))

  17. Let expression (let ((<var1> <exp1>) (<var2> <exp2>) … (<varn> <expn>)) <body>) is syntactic sugar for ((lambda (<var1> … <varn>) <body>) <exp1> … <expn>)

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

  19. Exercise 1.34 (define (f g) (g 2)) > (f square) 4 > (f (lambda (z)(* z (+ z 1)))) 6 > (f f) ???

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

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

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

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

  24. Safe Square Root as a fixed point (define (sqrt x) (fixed-point (average-damp (lambda (y) (/ x y))) 1)) • Linear Iterative Process?

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

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

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

  28. 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…….

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

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

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

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

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

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

More Related