Skip to content

Instantly share code, notes, and snippets.

@cleoold
Created December 9, 2018 03:17
Show Gist options
  • Save cleoold/6ec37a010f40a2cfa0afe90a9c6eccfc to your computer and use it in GitHub Desktop.
Save cleoold/6ec37a010f40a2cfa0afe90a9c6eccfc to your computer and use it in GitHub Desktop.
The sum of a polynomial
;; computes the result of a given polymonial using Horner's method
;; a Poly (polynomial) is one of
;; * (cons Num null)
;; * (cons Num poly)
;; format of the polynomial
;; (list a-0 a-1 a-2 ... a-n) corresponds to
;; (a-0)+(a-1)x^1+(a-2)x^2+...+(a-n)x^n
;; eval-poly: Poly -> Num
(define (eval-poly mypoly x)
(cond [(null? (cdr mypoly)) (car mypoly)]
[else
(+ (car mypoly) (* x (eval-poly (cdr mypoly) x)))]))
; example
; 1+2x+3x^2-4x^3-5x^4 = -479 (x=3)
(eval-poly (list 1 2 3 -4 -5) 3)
"shall be"
-479
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment