gscheme/examples/rootfinder.scm

33 lines
609 B
Scheme
Raw Normal View History

2022-08-05 05:28:40 -04:00
(define rootfinder
(let ((epsilon 1e-8))
(lambda (p a b)
(let ((mid (/ (+ a b) 2.0)))
(if (< (- b a) epsilon) mid
(let
((s1 (if (> (p a) 0) 'pos 'neg))
(s2 (if (> (p mid) 0) 'pos 'neg)))
(if (eq? s1 s2)
(rootfinder p mid b)
(rootfinder p a mid))))))))
(define sqrteq
(lambda (a)
(lambda (x)
(- (* x x) a))))
(define r5 (rootfinder (sqrteq 5) 0 5))
r5
(* r5 r5)
(define cbrteq
(lambda (a)
(lambda (x)
(- (* x x x) a))))
(define cr7 (rootfinder (cbrteq 7) 0 7))
cr7
(* cr7 cr7 cr7)
(define pi (rootfinder (lambda (x) (cos (/ x 2))) 3 4))
pi