Created
September 10, 2012 03:15
-
-
Save jolisper/3688628 to your computer and use it in GitHub Desktop.
SICP exercise 1.6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment