Skip to content

Instantly share code, notes, and snippets.

@bokuo-okubo
Created November 15, 2015 13:49
Show Gist options
  • Save bokuo-okubo/c4be7946aba175f8b494 to your computer and use it in GitHub Desktop.
Save bokuo-okubo/c4be7946aba175f8b494 to your computer and use it in GitHub Desktop.
(define (square x)
(* x x))
(define (abs x)
(cond ((< x 0) (- x))
((>= x 0) x)))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))
(define (improve guess x)
(average guess (/ x guess)))
(define (average x y)
(/ (+ x y) 2))
(define (powpow x pow acc)
(cond ((= pow 0) acc)
((> pow 0) (powpow x (- pow 1) (* x acc)))))
(define (power x y)
(powpow x y 1))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) (power (/ 1 10) 10)))
(define (sqrt x)
(sqrt-iter 1.0 x))
(print (sqrt 9))
(print (sqrt 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment