1 665c255d 2023-08-04 jrmu (define (test-case actual expected)
3 665c255d 2023-08-04 jrmu (display "Actual: ")
4 665c255d 2023-08-04 jrmu (display actual)
6 665c255d 2023-08-04 jrmu (display "Expected: ")
7 665c255d 2023-08-04 jrmu (display expected)
10 665c255d 2023-08-04 jrmu ;; (+ (* 2 1)
12 665c255d 2023-08-04 jrmu ;; (+ (* 2 (+ (* 2 1)
15 665c255d 2023-08-04 jrmu ;; (+ (* 2 (+ (* 2 (+ (* 2 1)
19 665c255d 2023-08-04 jrmu ;; (+ (* 2 (+ (* 2 (+ (* 2 (+ (* 2 1)
24 665c255d 2023-08-04 jrmu ;; (+ (* 2 (+ (* 2 (+ (* 2 (+ (* 2 (+ (* 2 1)
31 665c255d 2023-08-04 jrmu ;; the pattern sort of looks like this in pseudo-code
32 665c255d 2023-08-04 jrmu ;; (+ (* 2 (horner-eval 2 (cdr coefficient-sequence)))
33 665c255d 2023-08-04 jrmu ;; (car coefficient-sequence))
35 665c255d 2023-08-04 jrmu (define (accumulate op initial sequence)
36 665c255d 2023-08-04 jrmu (if (null? sequence)
38 665c255d 2023-08-04 jrmu (op (car sequence)
39 665c255d 2023-08-04 jrmu (accumulate op initial (cdr sequence)))))
41 665c255d 2023-08-04 jrmu (define (horner-eval x coefficient-sequence)
42 665c255d 2023-08-04 jrmu (accumulate (lambda (coefficient remaining-horner)
43 665c255d 2023-08-04 jrmu (+ coefficient
44 665c255d 2023-08-04 jrmu (* x remaining-horner)))
46 665c255d 2023-08-04 jrmu coefficient-sequence))
48 665c255d 2023-08-04 jrmu (test-case (horner-eval 0 '()) 0)
49 665c255d 2023-08-04 jrmu (test-case (horner-eval 10 '()) 0)
50 665c255d 2023-08-04 jrmu (test-case (horner-eval 0 '(1)) 1)
51 665c255d 2023-08-04 jrmu (test-case (horner-eval 2 '(1 3 0 5 0 1)) 79)
52 665c255d 2023-08-04 jrmu (test-case (horner-eval -1 '(2 4 1 1 8 3)) 3)
53 665c255d 2023-08-04 jrmu (test-case (horner-eval 1.23 '(3 5 2 9 4)) 38.079068639999996)