Skip to content

Instantly share code, notes, and snippets.

@jpsierens
Created December 2, 2017 01:33
Show Gist options
  • Save jpsierens/660159233730f449e600a568dbfdd332 to your computer and use it in GitHub Desktop.
Save jpsierens/660159233730f449e600a568dbfdd332 to your computer and use it in GitHub Desktop.
scheme-sqrt created by philander - https://repl.it/@philander/scheme-sqrt
; Now let’s formalize the process in terms of procedures. We start with a value for the radicand (the number whose square root we are trying to compute) and a value for the guess. If the guess is good enough for our purposes, we are done; if not, we must repeat the process with an improved guess. We write this basic strategy as a procedure: (define (improve guess x) (average guess (/ x guess))) (define (average x y) (/ (+ x y) 2)) (define (square x) (* x x)) (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) (define (sqrt-iter guess x) (if (good-enough? guess x) ; return guess guess ; otherwise (sqrt-iter (improve guess x) x ))) (define (sqrt x) (sqrt-iter 1.0 x)) (sqrt 9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment