-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathch1-3-4-general-procedures.scm
49 lines (37 loc) · 1.36 KB
/
ch1-3-4-general-procedures.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
; Ch. 1.3.4 Abstractions and first-class procedures
; A general method that finds the fixed point of some
; transformation of the function.
(define (fixed-point-of-transform g transform guess)
(fixed-point (transform g) guess))
(define (sqrt x)
(fixed-point-of-transform (lambda (y) (/ x y))
average-damp
1.0))
(define (cube-root x)
(fixed-point-of-transform (lambda (y) (/ x (square y)))
average-damp
1.0))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Prerequisite code
; Average damping can be expressed as:
(define (average-damp f)
(lambda (x) (average x (f x))))
; tolerance intended for fixed-point procedure
(define tolerance 0.00001)
; procedure to compute fixed-point f(x) = x
(define (fixed-point f first-guess)
(define (close-enough? v1 v2)
(< (abs (- v1 v2)) tolerance))
(define (try guess)
(let ((next (f guess)))
(display "Guess: ")
(display guess)
(newline)
(display "Next: ")
(display next)
(newline)
(if (close-enough? guess next)
next
(try next))))
(try first-guess))
(define (average x y) (/ (+ x y) 2))