From: Ramakrishnan Muthukrishnan Date: Thu, 25 Mar 2010 03:27:04 +0000 (+0530) Subject: exercise 1.8 solution. X-Git-Url: https://git.rkrishnan.org/pf/content/en/service/htmlfontify-example.html?a=commitdiff_plain;h=1535cff172539fd8c0dc5ab075bb38d4d63f3a45;p=sicp.git exercise 1.8 solution. --- diff --git a/chapter1/ch1_1.clj b/chapter1/ch1_1.clj index bcb6329..118022e 100644 --- a/chapter1/ch1_1.clj +++ b/chapter1/ch1_1.clj @@ -235,4 +235,30 @@ user> (sqrt 9) user> (sqrt 81) 9.000000000007091 -;; exercise 1.8: +;; exercise 1.8: cube root +(defn cube [x] + (* x x x)) + +(defn improve [guess x] + (/ (+ (/ x (square guess)) (* 2 guess)) 3)) + +(defn cubert-iter [old-guess new-guess x] + (if (good-enough? old-guess new-guess x) + new-guess + (cubert-iter new-guess (improve new-guess x) + x))) + +(defn cubert [x] + (cubert-iter x 1.0 x)) + +user> (cubert (cube 2)) +2.000000000012062 +user> (cubert (cube 10)) +10.000000000000002 +user> (cubert (cube 9)) +9.000000000053902 +user> (cubert (cube 0.001)) +0.001000000000000962 +user> (cubert (cube 0.0001)) +1.000000000000001E-4 +user> \ No newline at end of file