Skip to content

Instantly share code, notes, and snippets.

@dcb9
Created August 23, 2017 09:50
Show Gist options
  • Save dcb9/e5a7ec01b6fac74258a37a58e5103e04 to your computer and use it in GitHub Desktop.
Save dcb9/e5a7ec01b6fac74258a37a58e5103e04 to your computer and use it in GitHub Desktop.
sicp-1.7 created by dcb9 - https://repl.it/KVr5/1
(define (average x) (/ x 2))
(define (square x) (* x x))
(define (abs x) (if (< x 0) (- 0 x) x))
; (define (good-enough? guess x)
; (if (< (abs (- (square guess) x)) 0.00000000001)
; #t
; #f))
(define (good-enough? guess x)
(if (< (/ (abs (- guess (improve guess x))) guess) 0.00000000001)
#t
#f))
(define (improve guess x)
(average (+ (/ x guess) guess)))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(define (sqrt x)
(sqrt-iter 1.0 x))
(sqrt 16)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment