Created
August 25, 2018 02:32
-
-
Save thdaraujo/65663d912ff4bbb9afec5f8ca973fd11 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
(defn average [x y] (* 0.5 (+ x y))) | |
(defn improve [guess x] (average guess (/ x guess))) | |
(defn square [x] (* x x)) | |
(defn abs [x] (if (< 0 x) x (- 0 x))) | |
(defn good-enough? [guess x] | |
(< (abs (- x (square guess))) 0.0000001)) | |
(defn sqrt-iter [guess x] | |
(if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) | |
(defn sqrt [x] (sqrt-iter 1.0 x)) | |
(sqrt 2.0) | |
(sqrt 9) | |
(sqrt 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment