Skip to content

Instantly share code, notes, and snippets.

@ahmadajmi
Last active August 29, 2015 14:02
Show Gist options
  • Save ahmadajmi/20c22923cbb713115475 to your computer and use it in GitHub Desktop.
Save ahmadajmi/20c22923cbb713115475 to your computer and use it in GitHub Desktop.
Square root
#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