Created
January 26, 2013 15:38
-
-
Save lyuehh/4642942 to your computer and use it in GitHub Desktop.
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
(define (abs x) | |
(cond ((> x 0) x) | |
((= x 0) 0) | |
((< x 0) (- x)))) | |
(define (sequare x) (* x x)) | |
(define (sqrt-iter guess x) | |
(if (good-enough? guess (improve guess x)) | |
(improve guess x) | |
(sqrt-iter (improve guess x) x))) | |
(define (improve guess x) | |
(average guess (/ x guess))) | |
(define (average x y) | |
(/ (+ x y) 2)) | |
(define (sqrt x) | |
(sqrt-iter 1.0 x)) | |
(define (cube-root x) | |
(cube-root-iter 1.0 x)) | |
(define (cube-root-iter guess x) | |
(if (good-enough? guess x) | |
guess | |
(cube-root-iter (improve guess x) x))) | |
(define (good-enough? guess x) | |
(< (abs (- (cube guess) x)) | |
0.001)) | |
(define (improve guess x) | |
(/ (+ (/ x (sequare guess)) (* 2 guess)) | |
3)) | |
(define (cube x) | |
(* x x x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment