Permalink
Browse files

completed exercies though 1.8

  • Loading branch information...
0 parents commit 8dd6b6a5d52f6b3be8f047082061b8326dd211a8 @cosmin committed Jan 18, 2010
Showing with 54 additions and 0 deletions.
  1. +2 −0 exercise-1-2.ss
  2. +14 −0 exercise-1-3.ss
  3. +20 −0 exercise-1-7.ss
  4. +18 −0 exercise-1-8.ss
@@ -0,0 +1,2 @@
+(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5)))))
+ (* 3 (- 6 2) (- 2 7)))
@@ -0,0 +1,14 @@
+(define (square x) (* x x))
+
+(define (sum-of-squares x y)
+ (+ (square x) (square y)))
+
+(define (sum-of-largest-squares a b c)
+ (cond ((and (> a c) (> b c)) (sum-of-squares a b))
+ ((and (> a b) (> c b)) (sum-of-squares a c))
+ (else (sum-of-squares b c))))
+
+(= (sum-of-largest-squares 3 4 5) 41)
+(= (sum-of-largest-squares 3 4 4) 32)
+(= (sum-of-largest-squares 3 3 5) 34)
+(= (sum-of-largest-squares 3 3 3) 18)
@@ -0,0 +1,20 @@
+(define (square x) (* x x))
+
+(define (improve guess x)
+ (average guess (/ x guess)))
+
+(define (average x y) (/ (+ x y) 2 ))
+
+(define (good-enough? old-guess guess)
+ (< (abs (- old-guess guess)) (* old-guess 0.001)))
+
+(define (sqrt* x)
+ (square-iter 0 1.0 x))
+
+(define (square-iter old-guess guess target)
+ (if (good-enough? old-guess guess)
+ guess
+ (square-iter guess (improve guess target) target)))
+
+(sqrt 2)
+(sqrt 1241324123847120938471234)
@@ -0,0 +1,18 @@
+(define (square x) (* x x))
+
+(define (improve guess target)
+ (/ (+ (/ target (square guess)) (* 2 guess))
+ 3))
+
+(define (good-enough? old-guess new-guess)
+ (< (abs (- old-guess new-guess)) (* old-guess 0.0001)))
+
+(define (cube-root x)
+ (cube-iter 0 1.0 x))
+
+(define (cube-iter old-guess new-guess target)
+ (if (good-enough? old-guess new-guess)
+ new-guess
+ (cube-iter new-guess (improve new-guess target) target)))
+
+(cube-root 8)

0 comments on commit 8dd6b6a

Please sign in to comment.