Chapter 1, Building Abstractions with Procedures
Section - Formulating Abstractions with Higher-Order Procedures
Exercise 1.37
Procedure definition that generates recursive process:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(define (cont-frac num den k)
(define (aux c)
(cond ((> c k) 0)
(else (/
(num c)
(+
(den c)
(aux (inc c))
)
)
)
)
)
(aux 1)
)
Value of $k$ needed to generate $\frac 1 { \phi }$ correct upto 4 decimal places:
1
2
3
4
5
6
7
; Correct value: 0.618033988749894848204586834365638117720309179805762862135
> (cont-frac (lambda (i) 1.0) (lambda (i) 1.0) 10)
0.6179775280898876
> (cont-frac (lambda (i) 1.0) (lambda (i) 1.0) 11)
0.6180555555555556
Procedure definition that generates iterative process:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(define (cont-frac-itr num den k)
(define (itr rs c)
(cond ((= c 0) rs)
(else
(itr
(/
(num c)
(+ (den c) rs)
)
(dec c)
)
)
)
)
(itr 0 k)
)