Blame


1 665c255d 2023-08-04 jrmu (define tolerance 0.00001)
2 665c255d 2023-08-04 jrmu (define (fixed-point f first-guess)
3 665c255d 2023-08-04 jrmu (define (close-enough? v1 v2)
4 665c255d 2023-08-04 jrmu (< (abs (- v1 v2)) tolerance))
5 665c255d 2023-08-04 jrmu (define (try guess)
6 665c255d 2023-08-04 jrmu (let ((next (f guess)))
7 665c255d 2023-08-04 jrmu (if (close-enough? guess next)
8 665c255d 2023-08-04 jrmu next
9 665c255d 2023-08-04 jrmu (try next))))
10 665c255d 2023-08-04 jrmu (try first-guess))
11 665c255d 2023-08-04 jrmu
12 665c255d 2023-08-04 jrmu (define (average x y)
13 665c255d 2023-08-04 jrmu (/ (+ x y) 2.0))
14 665c255d 2023-08-04 jrmu (define (average-damp f)
15 665c255d 2023-08-04 jrmu (lambda (x) (average x (f x))))
16 665c255d 2023-08-04 jrmu (define (fixed-point-of-transform g transform guess)
17 665c255d 2023-08-04 jrmu (fixed-point (transform g) guess))
18 665c255d 2023-08-04 jrmu (define (sqrt x)
19 665c255d 2023-08-04 jrmu (fixed-point-of-transform (lambda (y) (/ x y))
20 665c255d 2023-08-04 jrmu average-damp
21 665c255d 2023-08-04 jrmu 1.0))
22 665c255d 2023-08-04 jrmu (define (cube-root x)
23 665c255d 2023-08-04 jrmu (fixed-point-of-transform (lambda (y) (/ x (square y)))
24 665c255d 2023-08-04 jrmu average-damp
25 665c255d 2023-08-04 jrmu 1.0))
26 665c255d 2023-08-04 jrmu
27 665c255d 2023-08-04 jrmu (define (compose f g)
28 665c255d 2023-08-04 jrmu (lambda (x)
29 665c255d 2023-08-04 jrmu (f (g x))))
30 665c255d 2023-08-04 jrmu
31 665c255d 2023-08-04 jrmu (define (test-case actual expected)
32 665c255d 2023-08-04 jrmu (load-option 'format)
33 665c255d 2023-08-04 jrmu (newline)
34 665c255d 2023-08-04 jrmu (format #t "Actual: ~A Expected: ~A" actual expected))
35 665c255d 2023-08-04 jrmu
36 665c255d 2023-08-04 jrmu (define (square x) (* x x))
37 665c255d 2023-08-04 jrmu (define (inc x) (1+ x))
38 665c255d 2023-08-04 jrmu ;; (test-case ((compose square inc) 6) 49)
39 665c255d 2023-08-04 jrmu
40 665c255d 2023-08-04 jrmu (define (repeated f n)
41 665c255d 2023-08-04 jrmu (if (= n 0)
42 665c255d 2023-08-04 jrmu (lambda (x) x)
43 665c255d 2023-08-04 jrmu (compose f (repeated f (- n 1)))))
44 665c255d 2023-08-04 jrmu
45 665c255d 2023-08-04 jrmu ;; (test-case ((repeated square 2) 5) 625)
46 665c255d 2023-08-04 jrmu (test-case (cube-root 5) 1.70997594668)
47 665c255d 2023-08-04 jrmu
48 665c255d 2023-08-04 jrmu
49 665c255d 2023-08-04 jrmu ;; Exercise 1.44. The idea of smoothing a function is an important concept in signal processing. If f is a function and dx is some small number, then the smoothed version of f is the function whose value at a point x is the average of f(x - dx), f(x), and f(x + dx). Write a procedure smooth that takes as input a procedure that computes f and returns a procedure that computes the smoothed f. It is sometimes valuable to repeatedly smooth a function (that is, smooth the smoothed function, and so on) to obtained the n-fold smoothed function. Show how to generate the n-fold smoothed function of any given function using smooth and repeated from exercise 1.43.
50 665c255d 2023-08-04 jrmu
51 665c255d 2023-08-04 jrmu (define dx 0.01)
52 665c255d 2023-08-04 jrmu
53 665c255d 2023-08-04 jrmu (define (smooth f)
54 665c255d 2023-08-04 jrmu (lambda (x)
55 665c255d 2023-08-04 jrmu (/ (+ (f x)
56 665c255d 2023-08-04 jrmu (f (+ x dx))
57 665c255d 2023-08-04 jrmu (f (- x dx)))
58 665c255d 2023-08-04 jrmu 3.0)))
59 665c255d 2023-08-04 jrmu (define (n-fold-smooth f n)
60 665c255d 2023-08-04 jrmu ((repeated smooth n) f))