]> git.rkrishnan.org Git - sicp.git/blob - src/sicp/ex2_34.clj
Solution to 4.30. Extremely enlightening!
[sicp.git] / src / sicp / ex2_34.clj
1 (ns sicp.ex2_34
2   (:use [clojure.test]
3         [sicp [ch2-2 :only (accumulate)]]))
4
5 ;; horner's rule. Extensively used in signal processing world
6 ;; Assume that the coefficients of the polynomial are arranged
7 ;; in a sequence, from a0 through an. 
8 (defn horner-eval [x coeff-seq]
9   (accumulate (fn [this-coeff higher-terms]
10                 (+ this-coeff
11                    (* x higher-terms)))
12               0
13               coeff-seq))
14
15 ;; 1 + 3x + 5x^3 + x^5 at x = 2
16 (deftest test-horners-poly
17   (is [= (horner-eval 2 '(1 3 0 5 0 1))
18          79]))