Com S 342 meeting -*- Outline -*- * tool makers, or procedures that return procedures (1.3.4) since procedures are tools, procedures that return procedures are tool makers ** examples *** average damping recall average damping, suppose we want to express this general idea for any procedure f (define (average-damp f) (lambda (x) (average x (f x)))) this allows us to formulate square root in terms of this tool maker, and our fixed point tool (define (sqrt x) (fixed-point (average-damp (lambda (y) (/ x y))) 1.0)) Can we do this for cube roots? (define (cube-root x) (fixed-point (average-damp (lambda (y) (/ x (square y)))) 1.0)) Show this in Java... Draw a picture of the meaning of --------------------------------------------------------- DoubleFun dc = new AverageDamp().apply(new Cosine()); [Cosine class] ^ \ dc: [*-]--> -------------------------- | | ... | *------------|--> [DampedFun class] | |------------------------| | | saved_f | *------------|------> |-------------------| / |========================| | ... | *---------|- | double value(double x) | |===================| |------------------------| | double value(...) | | double average( ... ) | |-------------------| -------------------------- --------------------------------------------------------- *** derivatives recall that the derivative transforms one function to another, it's a tool maker Dg(x) = g(x + dx) - g(x) / dx (as dx -> 0) (define (deriv g) (lambda (x) (/ (- (g (+ x dx)) (g x)) dx))) (define dx 0.00001) *** Newton's method for finding the root of a function, g is fixed point of g(x) = x - (g(x)/Dg(x)), where Dg(x) is g' evaluated at x this works well in practice (define (newton-transform g) (lambda (x) (- x (/ (g x) ((deriv g) x))))) (define (newtons-method g guess) (fixed-point (newton-transform g) guess)) for square roots, you want to find zeros of the function y = y^2 - x (define (sqrt x) (newtons-method (lambda (y) (- (square y) x)) 1.0)) *** comparing methods for computing square roots both methods in this section are fixed point computations (define (fixed-point-of-transform g transform guess) (fixed-point (transform g) guess)) ;;; the average damped computation (define (sqrt x) (fixed-point-of-transform (lambda (y) (/ x y)) average-damp 1.0)) ;;; Newton's method (define (sqrt x) (fixed-point-of-transform (lambda (y) (- (square y) x)) newton-transform 1.0)) ** first-class procedures computational elements have first-class status, if they have the following privileges: - can be named - passed as arguments - returned as results - stored in data structures the procedures in Scheme are first-class objects ** Java coding of this use interfaces, and nested classes ** gravational field example ------------------------------------------ GRAVITATIONAL FORCE EXAMPLE (define G ;; TYPE: N * m^2 / kg^2 6.670e-11) (define (square r) ;; TYPE: (-> (m) m^2) (* r r)) (define (grav-force m1 r m2) ;; TYPE: (-> (kg m kg) N) (if (zero? r) 0.0 (/ (* G (* m1 m2)) (square r)))) (define (grav-force-c m1) ;; TYPE: (-> (kg) ;; (-> (m) ;; (-> (kg) ;; N))) (lambda (r) ; m (lambda (m2) ; kg (if (zero? r) 0.0 (/ (* G (* m1 m2)) (square r)))))) ------------------------------------------ ------------------------------------------ USING IT (define mass-of-earth ;; TYPE: kg 5.96e24) (define radius-of-earth ;; TYPE: m 6.37e6) (define earths-force ;; TYPE: (-> (grav-force-c mass-of-earth)) (define force-at-surface ;; TYPE: (-> (earths-force radius-of-earth)) > ;;; force in N on me (force-at-surface 77) > ;;; force in N on 1kg (a liter of coke) (force-at-surface 1) ------------------------------------------ ... 754 ... 9.797 Q: how would you compute forces at distance of moon's orbit by earth? Q: how would you compute forces exerted by the sun? Moral: curried functions can be used to preplan convenient tools so A CURRIED FUNCTION IS A TOOL-MAKER!