Skip to content

Instantly share code, notes, and snippets.

@dcb9
Last active August 24, 2017 01:09
Show Gist options
  • Save dcb9/646d2786a09429bf326812dbae212ed4 to your computer and use it in GitHub Desktop.
Save dcb9/646d2786a09429bf326812dbae212ed4 to your computer and use it in GitHub Desktop.
sicp-1.8 created by dcb9 - https://repl.it/KVqC/7
(define (square x) (* x x))
(define (double x) (+ x x))
(define (athird x) (/ x 3))
(define (cube x) (* x x x))
(define (abs x) (if (< x 0) (- 0 x) x))
(define (good-enough? guess x)
(< (abs (- (cube guess) x)) 0.0000001))
(define (improve guess x)
(athird (+ (/ x (square guess)) (double guess))))
(define (cbrt-iter guess x)
(if (good-enough? guess x)
guess
(cbrt-iter (improve guess x) x)))
(define (cbrt x)
(cbrt-iter 1.0 x))
(cbrt 9)
; (cbrt 64)
; (cbrt 27)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment