Skip to content

Instantly share code, notes, and snippets.

@samertm
Created December 25, 2011 01:49
Show Gist options
  • Save samertm/1518613 to your computer and use it in GitHub Desktop.
Save samertm/1518613 to your computer and use it in GitHub Desktop.
Square root in scheme
(define (sqrt x)
(define (average lhs rhs)
(/ (+ lhs rhs) 2))
(define (good-enough? guess)
(< (abs (- (square guess) x)) .0001))
(define (improve guess)
(average guess (/ x guess)))
(define (try guess)
(if (good-enough? guess)
guess
(try (improve guess))))
(try 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment