Exercise 1.7

Given this procedure:

1
2
(define (good-enough? guess x)
  (< (abs (- (square guess) x)) 0.001))

We want an improved version of good-enough? that implements the following check:

|guess_{n+1} - guess_{n}| < \frac{guess_{n+1}}{k}

for a value of k=10^5.

1
2
3
4
5
6
7
(define (good-enough-improv? oldguess newguess)
  (< (abs (- newguess oldguess)) (/ newguess 10000)))

(define (sqrt-iter-improv oldguess newguess x)
  (if (good-enough-improv? oldguess newguess)
      newguess
      (sqrt-iter-improv newguess (improve newguess x) x)))

Creative Commons License
This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution 2.0 UK: England & Wales License.

Tags: ,

Leave a Reply

CAPTCHA Image

Powered by WP Hashcash

Please wrap all source codes with [code][/code] tags.