Created
December 2, 2017 01:33
-
-
Save jpsierens/660159233730f449e600a568dbfdd332 to your computer and use it in GitHub Desktop.
scheme-sqrt created by philander - https://repl.it/@philander/scheme-sqrt
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
; Now let’s formalize the process in terms of procedures. We start with a value for the radicand (the number whose square root we are trying to compute) and a value for the guess. If the guess is good enough for our purposes, we are done; if not, we must repeat the process with an improved guess. We write this basic strategy as a procedure: (define (improve guess x) (average guess (/ x guess))) (define (average x y) (/ (+ x y) 2)) (define (square x) (* x x)) (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) (define (sqrt-iter guess x) (if (good-enough? guess x) ; return guess guess ; otherwise (sqrt-iter (improve guess x) x ))) (define (sqrt x) (sqrt-iter 1.0 x)) (sqrt 9) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment