Skip to content

Instantly share code, notes, and snippets.

@thdaraujo
Created August 25, 2018 02:32
Show Gist options
  • Save thdaraujo/65663d912ff4bbb9afec5f8ca973fd11 to your computer and use it in GitHub Desktop.
Save thdaraujo/65663d912ff4bbb9afec5f8ca973fd11 to your computer and use it in GitHub Desktop.
(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