Last active
August 29, 2015 14:02
-
-
Save ahmadajmi/20c22923cbb713115475 to your computer and use it in GitHub Desktop.
Square root
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
#lang racket | |
(define square | |
(λ (x) | |
(* x x))) | |
(define average | |
(λ (x y) | |
(/ (+ x y) 2))) | |
(define is-good-enough | |
(λ (guess x) | |
(< (abs (- (square guess) x)) 0.001))) | |
(define improve | |
(λ (guess x) | |
(average guess (/ x guess)))) | |
(define check-sqrt | |
(λ (guess x) | |
(if (is-good-enough guess x) | |
guess | |
(check-sqrt (improve guess x) x)))) | |
(define sqt | |
(λ (x) | |
(check-sqrt 1.0 x))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment