Created
May 30, 2020 15:11
-
-
Save alexandreaquiles/4d09e441a3537aa16af9c11006585bd8 to your computer and use it in GitHub Desktop.
Square Implementation from SICP
This file contains hidden or 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 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