Created
January 5, 2012 10:43
-
-
Save valexey/1564676 to your computer and use it in GitHub Desktop.
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 method newtons-sqrt (x :: <number>) | |
local method sqrt1 (guess) | |
// note call to other local method | |
if (close-enough? (guess)) | |
guess | |
else | |
sqrt1 (improve (guess)) // note self-recursive call | |
end if | |
end sqrt1, | |
method close-enough? (guess) | |
abs (guess * guess - x) < .0001 | |
end close-enough?, | |
method improve (guess) | |
(guess + (x / guess)) / 2 | |
end improve; | |
sqrt1 (1) | |
end method newtons-sqrt; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment