Skip to content

Instantly share code, notes, and snippets.

@baioc
Created February 19, 2021 09:55
Show Gist options
  • Save baioc/75ffa4d9c06c65675c2fa205d789cdbd to your computer and use it in GitHub Desktop.
Save baioc/75ffa4d9c06c65675c2fa205d789cdbd to your computer and use it in GitHub Desktop.
Polynomial definition and conversion to Horner form
(import (scheme base))
(define-syntax hornify
(syntax-rules ()
((_ x an) an)
((_ x a pn ...)
(+ a (* x (hornify x pn ...))))))
(define-syntax poly
(syntax-rules ()
((_ (x y ...) (pn ...))
(lambda (x y ...) (hornify x pn ...)))))
(define (quadratic a b c)
(poly (x) (c b a)))
((quadratic 1 -1 -1) 1.618) ; => =~ 0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment