Instantly share code, notes, and snippets.

# jolisper/sicp-exercise-1.6.scm Created Sep 10, 2012

What would you like to do?
SICP exercise 1.6
 ; SICP exercise 1.6 (define (new-if predicate then-clause else-clause) (cond (predicate consecuent) (else alternative))) (define (square x) (* x x)) (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) (define (average x y) (/ (+ x y) 2)) (define (improve guess x) (average guess (/ x guess))) (define (sqrt-iter guess x) (new-if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) ; What happends when Alyssa attemps to use this to compute square roots? Explain. ; When Alyssa attemps to use the new-if procedure the execution run into an infinite ; loop because the new-if procedure always evaluate the else-clause part that invoque ; the sqrt-iter procedure recursively. The result is that the good-enough? procedure ; is never called and the end condition is never reached. ; Note: diferent behavior using natural or decimal numbers (sqrt-iter 1 10.0) ;(sqrt-iter 1 10) (1) (define (sqrt-iter 1 10) (new-if (good-enough? 1 10) 1 (sqrt-iter (improve 1 10) 10))) (2) (define (sqrt-iter 1 10) (new-if (good-enough? 1 10) 1 (sqrt-iter 5.5 10))) (3) (define (sqrt-iter 1 10) (new-if (good-enough? 1 10) 1 (new-if (good-enough? 5.5 10) 5.5 (sqrt-iter (improve 5.5 10) 10)))) (4) (define (sqrt-iter 1 10) (new-if (good-enough? 1 10) 1 (new-if (good-enough? 5.5 10) 5.5 (sqrt-iter 3.659090909090909 10)))) (5) (define (sqrt-iter 1 10) (new-if (good-enough? 1 10) 1 (new-if (good-enough? 5.5 10) 5.5 (new-if (good-enough? 3.659090909090909 10) 3.659090909090909 (sqrt-iter (improve 3.659090909090909 10) 10))))) (6) (define (sqrt-iter 1 10) (new-if (good-enough? 1 10) 1 (new-if (good-enough? 5.5 10) 5.5 (new-if (good-enough? 3.659090909090909 10) 3.659090909090909 (sqrt-iter 3.196005081874647) 10)))) (7) (define (sqrt-iter 1 10) (new-if (good-enough? 1 10) 1 (new-if (good-enough? 5.5 10) 5.5 (new-if (good-enough? 3.659090909090909 10) 3.659090909090909 (new-if (good-enough? 3.196005081874647 10) 3.196005081874647 (sqrt-iter (improve 3.196005081874647 10) 10)))))) (8) (define (sqrt-iter 1 10) (new-if (good-enough? 1 10) 1 (new-if (good-enough? 5.5 10) 5.5 (new-if (good-enough? 3.659090909090909 10) 3.659090909090909 (new-if (good-enough? 3.196005081874647 10) 3.196005081874647 (sqrt-iter 3.16245562280389) 10)))))) (9) (define (sqrt-iter 1 10) (new-if (good-enough? 1 10) 1 (new-if (good-enough? 5.5 10) 5.5 (new-if (good-enough? 3.659090909090909 10) 3.659090909090909 (new-if (good-enough? 3.196005081874647 10) 3.196005081874647 (new-if (good-enough? 3.16245562280389 10) 3.16245562280389 (sqrt-iter (improve 3.16245562280389 10) 10))))))) (10) (define (sqrt-iter 1 10) (new-if (good-enough? 1 10) 1 (new-if (good-enough? 5.5 10) 5.5 (new-if (good-enough? 3.659090909090909 10) 3.659090909090909 (new-if (good-enough? 3.196005081874647 10) 3.196005081874647 (new-if (good-enough? 3.16245562280389 10) 3.16245562280389 (sqrt-iter 3.162277665175675 10))))))) (11) (define (sqrt-iter 1 10) (new-if (good-enough? 1 10) 1 (new-if (good-enough? 5.5 10) 5.5 (new-if (good-enough? 3.659090909090909 10) 3.659090909090909 (new-if (good-enough? 3.196005081874647 10) 3.196005081874647 (new-if (good-enough? 3.16245562280389 10) 3.16245562280389 (new-if (good-enough? 3.162277665175675 10) 10 (sqrt-iter (improve 3.162277665175675 10) 10))))))))