Skip to content

Instantly share code, notes, and snippets.

@qiuwei
Created August 9, 2013 16:32
Show Gist options
  • Save qiuwei/6195061 to your computer and use it in GitHub Desktop.
Save qiuwei/6195061 to your computer and use it in GitHub Desktop.
sqrt in clojure which follows scheme style
(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