(ns sicp.ex1_15 (:use [sicp utils] [clojure.contrib trace test-is])) ;; exercise 1.15: sin (x) calculation ;; a. How many times is the procedure p applied when (sine 12.15) ;; is evaluated? ;; b. What is the order of growth in space and number of steps (as ;; a function of a) used by the process generated by the sine ;; procedure when (sine a) is evaluated? (defn p [x] (- (* 3 x) (* 4 (cube x)))) (defn sine [angle] (if (not (> (myabs angle) 0.1)) angle (p (sine (/ angle 3.0))))) ;; solution to (a) => 5 ;; (comment ;; user> (dotrace [p] (sine 12.15)) ;; TRACE t2490: (p 0.049999999999999996) ;; TRACE t2490: => 0.1495 ;; TRACE t2491: (p 0.1495) ;; TRACE t2491: => 0.4351345505 ;; TRACE t2492: (p 0.4351345505) ;; TRACE t2492: => 0.9758465331678772 ;; TRACE t2493: (p 0.9758465331678772) ;; TRACE t2493: => -0.7895631144708228 ;; TRACE t2494: (p -0.7895631144708228) ;; TRACE t2494: => -0.39980345741334 ;; -0.39980345741334 ;; ) ;; solution to b ;; both space and number of steps grows as log3(a) -> log a to the base 3. ;; ;; proof: ;; a * (1/3)^n <= 0.1 ;; => take log to the base 3 on both the sides. ;; Note: Finding the order of space in a recursive process is sort of, equiv ;; to finding the number of deferred operations. Which is in-turn the ;; same as the depth of the evaluation tree.