Skip to content

Instantly share code, notes, and snippets.

@mmccollow
Created April 1, 2011 00:30
Show Gist options
  • Save mmccollow/897542 to your computer and use it in GitHub Desktop.
Save mmccollow/897542 to your computer and use it in GitHub Desktop.
My solution to exercise 1.8 from Structure and Interpretation of Computer Programs
#lang scheme
(define (cube-root x)
(define (approx guess x)
(if (good-enough? guess x)
guess
(approx (improve guess x) x)))
(define (improve guess x)
(/ (+ (/ x (square guess)) (* 2 guess)) 3))
(define (good-enough? guess x)
(< (abs (- (cube guess) x)) 0.001))
(define (square x)
(* x x))
(define (cube x)
(* (square x) x))
(define (abs x)
(if (< x 0) (- x)
x))
(approx 1.0 x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment