diff --git a/chapter2/accumulate.scm b/chapter2/accumulate.scm new file mode 100644 index 0000000..92561d8 --- /dev/null +++ b/chapter2/accumulate.scm @@ -0,0 +1,7 @@ +;;; accumulate function taking a "sequence" list + +(define (accumulate op initial sequence) + (if (null? sequence) + initial + (op (car sequence) + (accumulate op initial (cdr sequence))))) diff --git a/chapter2/horner-eval.scm b/chapter2/horner-eval.scm new file mode 100644 index 0000000..b9af6c5 --- /dev/null +++ b/chapter2/horner-eval.scm @@ -0,0 +1,10 @@ +;;; evaluates a polynomial at x using horner algorithm + +(load "accumulate.scm") + +(define (horner-eval x coefficient-sequence) + (+ (car coefficient-sequence) (accumulate (lambda (this-coeff higher-terms) (* (+ this-coeff higher-terms) x)) + 0 + (cdr coefficient-sequence)))) + +(display (horner-eval 2 (list 1 3 0 5 0 1)))