Created
December 25, 2011 01:49
-
-
Save samertm/1518613 to your computer and use it in GitHub Desktop.
Square root in scheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (sqrt x) | |
(define (average lhs rhs) | |
(/ (+ lhs rhs) 2)) | |
(define (good-enough? guess) | |
(< (abs (- (square guess) x)) .0001)) | |
(define (improve guess) | |
(average guess (/ x guess))) | |
(define (try guess) | |
(if (good-enough? guess) | |
guess | |
(try (improve guess)))) | |
(try 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment