Skip to content

Instantly share code, notes, and snippets.

@lan496
Created April 27, 2015 10:05
Show Gist options
  • Select an option

  • Save lan496/e85c27c4492e985c4bf8 to your computer and use it in GitHub Desktop.

Select an option

Save lan496/e85c27c4492e985c4bf8 to your computer and use it in GitHub Desktop.
(define (square x) (* x x))
(define (cube x) (* x x x))
(define (abs x)
(if (< x 0)
(- x)
x
)
)
(define (improve guess x)
(/ (+ (/ x (square guess)) (* 2 guess))
3
)
)
(define (good-enough? guess x)
(< (abs (/ (- (cube guess) x) x))
0.00001
)
)
(define (cube-root-iter guess x)
(if (good-enough? guess x)
guess
(cube-root-iter (improve guess x)
x
)
)
)
(define (cube-root x)
(cube-root-iter 1.0 x)
)
(display (cube-root 8))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment