From: Ramakrishnan Muthukrishnan Date: Tue, 3 Aug 2010 09:55:13 +0000 (+0530) Subject: solution to 2.34 X-Git-Url: https://git.rkrishnan.org/specifications/components/com_hotproperty/somewhere?a=commitdiff_plain;h=875f88e6147c03369ccec1d13336569d7e4af755;p=sicp.git solution to 2.34 --- 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