Created
August 9, 2013 16:32
-
-
Save qiuwei/6195061 to your computer and use it in GitHub Desktop.
sqrt in clojure which follows scheme style
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 sqrt [x] | |
(defn fixed-point [f first-guess] | |
(defn close-enough? [v1 v2] | |
(let [tolerance 0.000000001] | |
(< (Math/abs (- v1 v2)) | |
tolerance))) | |
(defn tryo [guess] | |
(loop [oldo guess | |
newo (f oldo)] | |
(if (close-enough? oldo newo) | |
newo | |
(recur newo (f newo))))) | |
(tryo first-guess)) | |
(fixed-point (fn [y] (/ (+ y (/ x y)) 2)) 1.0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment