Skip to content

Instantly share code, notes, and snippets.

@ExpandingShapes
Created January 16, 2019 20:26
Show Gist options
  • Save ExpandingShapes/b9a9d7501df874a836a479f812244679 to your computer and use it in GitHub Desktop.
Save ExpandingShapes/b9a9d7501df874a836a479f812244679 to your computer and use it in GitHub Desktop.
Eval square root by Newton's method
(define (average x y)(/(+ x y) 2))
(define square(lambda (x)(* x x)))
(define (my-abs x)
(cond ((< x 0)(- x))
((= x 0) 0)
((> x 0) x)))
(define (my-sqrt x)
(define (improve guess)
(average guess (/ x guess)))
(define (good-enough? guess)
(< (my-abs (- (square guess) x))
.001))
(define (try guess)
(if (good-enough? guess)
guess
(try (improve guess))))
(try 1))
(display (my-sqrt 36))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment