Last active
June 22, 2016 11:22
-
-
Save pyldin601/eec59f2904d5c718f9f076df42070364 to your computer and use it in GitHub Desktop.
SICP Tasks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (square a) | |
(* a a)) | |
(define (cube a) | |
(* a a a)) | |
(define (abs a) | |
(if (< a 0) (- a) a)) | |
(define (average a b) | |
(/ (+ a b) 2)) | |
(define (until pred impr init) | |
(define (until-iter guess) | |
(if (pred guess) | |
guess | |
(until-iter (impr guess)))) | |
(until-iter init)) | |
(define (sqrt x) | |
(define (good-enough? guess) | |
(< (abs (- (square guess) x)) 0.001)) | |
(define (improve guess) | |
(average (/ x guess) guess)) | |
(until good-enough? improve 1.0)) | |
(define (cbrt x) | |
(define (good-enough? guess) | |
(< (abs (- (cube guess) x)) 0.001)) | |
(define (improve guess) | |
(/ (+ (/ x (square guess)) (* 2 guess)) 3)) | |
(until good-enough? improve 1.0)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment