Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

SICP exercise 1.6

View sicp-exercise-1.6.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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
; 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
Something went wrong with that request. Please try again.