SICP Exercises 1.6 - 1.8
1.6
IF is a special form, meaning that it does not apply the operator (IF) to all of the opperands. New-if however does apply the operator to all the operands and because of the recursive call to (sqrt-iter) it never ends.
1.7
Running (sqrt 1000000000000000) takes an extremely long time to finish (I stopped it so don't really know if it actually does finish). (sqrt 0.00001) produces the result 0.008235 when the correct answer is 0.003162. Changing good-enough? to
(define (good-enough? guess x)
(< (abs (- guess (improve guess x))) .0001))
improves things with both large and small numbers and I get these results when plugging in the same numbers:
> (sqrt 0.00001)
0.003172028655357483
> (sqrt 1000000000000000)
31622776.601683907
>
1.8
$ cat ex1-8.ss (define (square x)
(* x x))
(define (cube x)
(* x x x))
(define (improve guess x)
(/ (/ x (+ (square guess) (* 2 guess))) 3))
(define (good-enough? guess x)
(< (abs (- (cube guess) x)) 0.0001))
(define (cubert-iter guess x)
(if (good-enough? guess x )
guess
(cubert-iter (improve guess x)
x)))
(define (cubert x)
(cubert-iter 1.0 x))

