Download presentation
Presentation is loading. Please wait.
1
Lecture #6 section 1.3.3 pages 68-70 1.3.4 pages72-77
מבוא מורחב
2
Computing (SQRT a) 1. Find a fixed point of the function f(x) = a/x.
2. Define g(x)=x2 –a Find a fixed point of f(x)=x - g(x)/g’(x) מבוא מורחב
3
The derivative. We want to write a procedure with:
Input: a function f: REAL REAL Output: the function f’: REAL REAL deriv: (REAL REAL) (REAL REAL) (define (deriv f) (lambda (x) (define dx 0.001) (/ (- (f (+ x dx)) (f x)) dx))) > ((deriv square) 3) מבוא מורחב
4
Finding fixed points for f(x)
Start with an arbitrary first guess x1 Each time: try the guess, f(x) ~ x ?? If it’s not a good guess try the next guess xi+1 = f(xi) (define (fixed-point f first-guess) (define tolerance ) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (try guess) (let ((next (f guess))) (if (close-enough? guess next) guess (try next)))) (try first-guess))
5
An example: f(x) = 1+1/x (define (f x) (+ 1 (/ 1 x)))
(fixed-point f 1.0) X1 = 1.0 X2 = f(x1) = 2 X3 = f(x2) = 1.5 X4 = f(x3) = X5 = f(x4) = 1.6 X6 = f(x5) = 1.625 X7 = f(x6) = … Real answer: … Note how odd guesses underestimate And even guesses Overestimate. מבוא מורחב
6
Another example: f(x) = y/x
(define (f x) (/ 2 x)) (fixed-point f 1.0) x1 = 1.0 x2 = f(x1) = 2 x3 = f(x2) = 1 x4 = f(x3) = 2 x5 = f(x4) = 1 x6 = f(x5) = 2 x7 = f(x6) = 1 Real answer: … מבוא מורחב
7
How do we deal with oscillation?
Consider f(x)=2/x. If x is a point such that guess < sqrt(2) then 2/guess > sqrt(2) So the average of guess and 2/guess is always an even Better guess. So, we will try to find a fixed point of g(x)= (x + f(x))/2 Notice that g(x) = (x +f(x)) /2 has the same fixed points as f. For f(x)=2/x this gives: g(x)= x + 2/x)/2 מבוא מורחב
8
To find an approximation of x: Make a guess G
Improve the guess by averaging G and x/G Keep improving the guess until it is good enough X = 2 G = 1 X/G = 2 G = ½ (1+ 2) = 1.5 X/G = 4/3 G = ½ (3/2 + 4/3) = 17/12 = X/G = 24/17 G = ½ (17/ /17) = 577/408 = מבוא מורחב
9
average-damp (define (average-damp f) ;outputs g(x)=(x+f(x)/2
(lambda (x) (average x (f x)))) average-damp: (number number) (number number) ((average-damp square) 10) ((lambda (x) (average x (square x))) 10) (average 10 (square 10)) 55 מבוא מורחב
10
Fixed-point II (define (fixed-point-II f first-guess)
(define tolerance ) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (try guess) (let ((next ( (average-damp f) guess))) (if (close-enough? guess next) guess (try next)))) (try first-guess)) מבוא מורחב
11
Computing sqrt and cube-roots.
(define (sqrt x) (fixed-point-II (lambda (y) (/ x y)) 1)) For cube root we want fix point of x=a/x2. So, (define (cbrt x) (fixed-point (lambda (y) (/ x (square y))) 1)) מבוא מורחב
12
Newton’s method A solution to g(x) = 0 is a fixed point of f(x) = x - g(x)/g’(x) (define (newton-transform g) (lambda (x) (- x (/ (g x) ((deriv g) x))))) (define (newton-method g guess) (fixed-point (newton-transform g) guess)) (define (sqrt x) (newton-method (lambda (y) (- (square y) x)) 1.0)) > (sqrt 2) מבוא מורחב
13
composef (define composef (lambda (f g) (lambda (x) (f (g x)))))
composef: (A B), (C A) (C B) composef: (STRING INT), (INT STRING) ( INT INT) composef: ( INT INT), (BOOL INT) ( BOOL INT) מבוא מורחב
14
Chapter 2 – Building abstractions with data
מבוא מורחב
15
Procedural abstraction
Publish: name, number and type of arguments type of answer Guarantee: the procedure behavior Hide: local variables and procedures, way of implementation, internal details, etc. Export only what is needed. מבוא מורחב
16
Data abstraction Publish: name, constructor, selectors,
ways of handling it Guarantee: the behavior Hide: local variables and procedures, way of implementation, internal details, etc. Export only what is needed. מבוא מורחב
17
An example: Rational numbers
We would like to represent rational numbers. A rational number is a quotient a/b of two integers. Constructor: (make-rat a b) Selectors: (numer r) (denom r) Guarantee: (numer (make-rat a b)) = a (denom (make-rat a b)) = b מבוא מורחב
18
Public Methods Methods should come with a guarantee of their actions:
(add-rat x y) (sub-rat x y) (mul-rat x y) (div-rat x y) (print-rat x) (equal-rat? x y) Methods should come with a guarantee of their actions: (equal-rat? (make-rat 2 4) (make-rat 5 10)) (equal-rat? x y) iff x and y have the same numerator and denominator (equal-rat? x y) iff x and y have the same value מבוא מורחב
19
Implementing methods with the constructor and selectors.
(define (add-rat x y) (make-rat (+ (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y)))) (define (sub-rat x y) … (define (mul-rat x y) (make-rat (* (numer x) (numer y)) (* (denom x) (denom y)))) (define (div-rat x y) (make-rat (* (numer x) (denom y)) (* (denom x) (numer y)))) (define (equal-rat? x y) (= (* (numer x) (denom y)) (* (numer y) (denom x)))) מבוא מורחב
20
Pair: A primitive data type.
Constructor: (cons a b) Selectors: (car p) (cdr p) Guarantee: (car (cons a b)) = a (cdr (cons a b)) = b Abstraction barrier: We say nothing about the representation or implementation of pairs. מבוא מורחב
21
Implementing make-rat, numer, denom
(define (make-rat n d) (cons n d)) (define (numer x) (car x)) (define (denom x) (cdr x)) מבוא מורחב
22
Reducing to lowest terms
In our current implementation we keep 10000/20000 As such and not as ½. This: Makes the computation more expensive. Prints out clumsy results. A solution: change the constructor (define (make-rat a b) (let ((g (gcd a b))) (cons (/ a g) (/ b g)))) Note that we do not need to change our program anywhere else. מבוא מורחב
23
Programs that use rational numbers
Abstraction barriers Programs that use rational numbers add-rat sub-rat ……. make-rat numer denom car cdr cons מבוא מורחב
24
Alternative implementation
Abstraction Violation (define (add-rat x y) (cons (+ (* (car x) (cdr y)) (* (car y) (cdr x))) (* (cdr x) (cdr y)))) If we bypass an abstraction barrier, Changes to one level may affect many levels above it. Maintenance becomes more difficult. מבוא מורחב
25
Compound data A closure property: The result obtained by creating a compound data structure can itself be treated as a primitive object and thus be input to the creation of another compound object. Pairs have the closure property: We can pair pairs, pairs of pairs etc. (cons (cons 1 2) 3) 3 2 1 מבוא מורחב
26
Box and pointer diagram
(cons (cons 1 (cons 2 3)) 4) 4 1 3 2 מבוא מורחב
27
Lists (cons 1 (cons 3 (cons 2 nil))) Syntactic sugar: (list 1 2 3) 1 3
מבוא מורחב
28
… Lists (list <x1> <x2> ... <xn>) Same as
(cons <x1> (cons <x2> ( … (cons <xn> nil)))) <x1> <x2> <xn> … מבוא מורחב
29
And so, (cons 3 (list 1 2)) is the same as
(cons 3 (cons 1 (cons 2 nil))) which is (3 1 2) And (cdr (list 1 2 3)) is (cdr (cons 1 (cons 2 (cons 3 nil)))) which is (cons 2 (cons 3 nil)) which is (list 2 3) מבוא מורחב
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.