Skip to content

Instantly share code, notes, and snippets.

@lan496
Last active August 29, 2015 14:20
Show Gist options
  • Save lan496/1bf1558a065a71e7bbcf to your computer and use it in GitHub Desktop.
Save lan496/1bf1558a065a71e7bbcf to your computer and use it in GitHub Desktop.
(define (square x) (* x x))
(define (abs x)
(if (< x 0)
(- x)
x
)
)
(define (average x y)
(/ (+ x y) 2)
)
(define (improve guess x)
(average guess (/ x guess))
)
(define (good-enough? guess x)
(< (abs (/ (- (square guess) x) x))
0.00001
)
)
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x
)
)
)
(define (sqrt x)
(sqrt-iter 1.0 x)
)
(display (sqrt 3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment