From: Ramakrishnan Muthukrishnan Date: Thu, 27 May 2010 19:45:33 +0000 (+0530) Subject: solution to 1.44. Extremely interesting problem X-Git-Url: https://git.rkrishnan.org/specifications/components/com_hotproperty/%22doc.html/flags/%3C?a=commitdiff_plain;h=7943585fa78030c9746c9c05706b5e02566f409d;p=sicp.git solution to 1.44. Extremely interesting problem --- diff --git a/src/sicp/ex1_44.clj b/src/sicp/ex1_44.clj new file mode 100644 index 0000000..e6cf38b --- /dev/null +++ b/src/sicp/ex1_44.clj @@ -0,0 +1,50 @@ +(ns sicp.ex1_44 + (:use [clojure.contrib test-is] + [sicp utils] + [sicp ch1_3] + [sicp ex1_43])) + +(def dx1 0.00001) + +(defn smooth [f] + (fn [x] + (average (f (- x dx1)) + (f x) + (f (+ x dx1))))) + +;; smooth takes a function as argument. repeated takes a function +;; which needs to be repeated. Here it is the smooth function and +;; the number of times it needs to be repeated. We call the function +;; returned by repeated with f. +(defn smooth-repeatedly [f n] + ((repeated smooth n) f)) + +;; borrowing an idea from Drew Hess' solutions, we generate a test impulse +;; function +(defn impulse-maker [x a] + (fn [y] (if (= y x) + a + 0))) + +;; we define a function which has an impulse at x=2 with a value 10. +(defn my-impulse-func [x] + ((impulse-maker 2 10) x)) + +(comment +((smooth my-impulse-func) 2) +;;=> 3.3333333 +user> ((smooth my-impulse-func) 1) +;;=> 0.0 +user> ((smooth (smooth my-impulse-func)) 2) +;;=> 3.3333333 +user> ((smooth (smooth (smooth my-impulse-func))) 2) +;;=> 2.5925925 + +;; now using our new function +((smooth-repeatedly my-impulse-func 1) 2) +;;=> 3.3333333 +user> ((smooth-repeatedly my-impulse-func 2) 2) +;;=> 3.3333333 +user> ((smooth-repeatedly my-impulse-func 3) 2) +;;=> 2.5925925 +) \ No newline at end of file