some solutions for MIT Press's "structure and interpretation of computer programs", I guess
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
807 B

(define tolerance 0.00001)
(define (fixed-point f first-guess)
(define (close-enough? v1 v2)
(< (abs (- v1 v2))
(define (try guess)
(let ((next (f guess)))
(if (close-enough? guess next)
(try next))))
(try first-guess))
(define (average x y)
(/ (+ x y) 2))
(define (average-damp f)
(lambda (x) (average x (f x))))
(define (compose f g)
(lambda (x) (f (g x))))
(define (repeated f n)
(if (< n 1)
(lambda (x) x)
(compose f (repeated f (- n 1)))))
(define (get-max-pow n)
(define (iter p r)
(if (< (- n r) 0)
(- p 1)
(iter (+ p 1) (* r 2))))
(iter 1 2))
(define (nth-root n x)
(fixed-point ((repeated average-damp (get-max-pow n))
(lambda (y) (/ x (expt y (- n 1)))))