-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise-1.06.scm
44 lines (33 loc) · 1.19 KB
/
exercise-1.06.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
#lang planet neil/sicp
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))
(define (improve guess x)
(average guess (/ x guess)))
(define (average x y)
(/ (+ x y) 2))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (square x)
(* x x))
(define (sqrt x)
(sqrt-iter 1.0 x))
; Alyssa P. Hacker wants an if without a special form using the new-if
; definition below and her friend Eva Lu Ator writes new-if for her as follows:
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
; Alyssa uses new-if to rewrite the square-root program:
(define (sqrt-iter-alyssa guess x)
(new-if (good-enough? guess x)
guess
(sqrt-iter-alyssa (improve guess x)
x)))
(define (sqrt-alyssa x)
(sqrt-iter-alyssa 1.0 x))
; what will happen when calling (sqrt-alyssa 4) is that the program will never
; terminate because cond is not a special form and will require all of its
; arguments to be evaluated before itself can be evaluated and the
; sqrt-iter-alyssa calls will keep running forever.