Skip to content

Instantly share code, notes, and snippets.

@pyldin601
Last active June 22, 2016 11:22
Show Gist options
  • Save pyldin601/eec59f2904d5c718f9f076df42070364 to your computer and use it in GitHub Desktop.
Save pyldin601/eec59f2904d5c718f9f076df42070364 to your computer and use it in GitHub Desktop.
SICP Tasks
(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