-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.3.4-proced-return.rkt
54 lines (43 loc) · 1019 Bytes
/
1.3.4-proced-return.rkt
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
50
51
52
53
54
#lang sicp
(define (average-damp f)
(lambda (x)
(average x (f x))))
(define (deriv g)
(lambda (x)
(/ (- (g (+ x dx)) (g x))
dx)))
(define dx 0.00001)
(define (cube x) (* x x x))
((deriv cube) 5)
(define (newton-transform g)
(lambda (x)
(- x (/ (g x)
((deriv g) x)))))
(define (newtons-method g guess)
(fixed-point (newton-transform g)
guess))
;; find fixed point
(define tolerance 0.00001)
(define (fixed-point f first-guess)
(define (close-enough? v1 v2)
(< (abs (- v1 v2))
tolerance))
(define (try guess)
(let ((next (f guess)))
(if (close-enough? guess next)
next
(try next))))
(try first-guess))
(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 (sqrt-newton x)
(fixed-point-of-transform
(lambda (y) (- (square y) x))
newton-transform
1.0))