1 (define (test-case actual expected)
15 ;; (+ (* 2 (+ (* 2 (+ (* 2 1)
19 ;; (+ (* 2 (+ (* 2 (+ (* 2 (+ (* 2 1)
24 ;; (+ (* 2 (+ (* 2 (+ (* 2 (+ (* 2 (+ (* 2 1)
31 ;; the pattern sort of looks like this in pseudo-code
32 ;; (+ (* 2 (horner-eval 2 (cdr coefficient-sequence)))
33 ;; (car coefficient-sequence))
35 (define (accumulate op initial sequence)
39 (accumulate op initial (cdr sequence)))))
41 (define (horner-eval x coefficient-sequence)
42 (accumulate (lambda (coefficient remaining-horner)
44 (* x remaining-horner)))
46 coefficient-sequence))
48 (test-case (horner-eval 0 '()) 0)
49 (test-case (horner-eval 10 '()) 0)
50 (test-case (horner-eval 0 '(1)) 1)
51 (test-case (horner-eval 2 '(1 3 0 5 0 1)) 79)
52 (test-case (horner-eval -1 '(2 4 1 1 8 3)) 3)
53 (test-case (horner-eval 1.23 '(3 5 2 9 4)) 38.079068639999996)