Skip to content

Commit

Permalink
Add horner-eval
Browse files Browse the repository at this point in the history
  • Loading branch information
petrgazarov committed Feb 26, 2018
1 parent 168d300 commit 3840513
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions chapter2/accumulate.scm
Original file line number Diff line number Diff line change
@@ -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)))))
10 changes: 10 additions & 0 deletions chapter2/horner-eval.scm
Original file line number Diff line number Diff line change
@@ -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)))

0 comments on commit 3840513

Please sign in to comment.