]> git.rkrishnan.org Git - sicp.git/commitdiff
solution to 2.34
authorRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Tue, 3 Aug 2010 09:55:13 +0000 (15:25 +0530)
committerRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Tue, 3 Aug 2010 09:55:13 +0000 (15:25 +0530)
src/sicp/ex2_34.clj [new file with mode: 0644]

diff --git a/src/sicp/ex2_34.clj b/src/sicp/ex2_34.clj
new file mode 100644 (file)
index 0000000..15dd200
--- /dev/null
@@ -0,0 +1,18 @@
+(ns sicp.ex2_34
+  (:use [clojure.test]
+        [sicp [ch2-2 :only (accumulate)]]))
+
+;; horner's rule. Extensively used in signal processing world
+;; Assume that the coefficients of the polynomial are arranged
+;; in a sequence, from a0 through an. 
+(defn horner-eval [x coeff-seq]
+  (accumulate (fn [this-coeff higher-terms]
+                (+ this-coeff
+                   (* x higher-terms)))
+              0
+              coeff-seq))
+
+;; 1 + 3x + 5x^3 + x^5 at x = 2
+(deftest test-horners-poly
+  (is [= (horner-eval 2 '(1 3 0 5 0 1))
+         79]))
\ No newline at end of file