From 875f88e6147c03369ccec1d13336569d7e4af755 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com> Date: Tue, 3 Aug 2010 15:25:13 +0530 Subject: [PATCH] solution to 2.34 --- src/sicp/ex2_34.clj | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/sicp/ex2_34.clj diff --git a/src/sicp/ex2_34.clj b/src/sicp/ex2_34.clj new file mode 100644 index 0000000..15dd200 --- /dev/null +++ b/src/sicp/ex2_34.clj @@ -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 -- 2.45.2