Skip to content

Instantly share code, notes, and snippets.

@alexandreaquiles
Created May 30, 2020 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexandreaquiles/4d09e441a3537aa16af9c11006585bd8 to your computer and use it in GitHub Desktop.
Save alexandreaquiles/4d09e441a3537aa16af9c11006585bd8 to your computer and use it in GitHub Desktop.
Square Implementation from SICP
(defn square [x]
(* x x))
(defn average [x y]
(/ (+ x y) 2))
(defn improve [guess x]
(average guess (/ x guess)))
(defn abs [x]
(cond (< x 0) (- x)
:else x))
(defn good-enough? [guess x]
(println "good-enough? -> guess:" guess " , x:" x)
(< (abs (- (square guess) x)) 0.001))
(defn sqrt [x]
(defn sqrt-iter [guess x]
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))
(sqrt-iter 1.0 x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment