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.

54 lines
1.1 KiB

(define (square x)
(* x x))
(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 (nice-sqrt x)
; (fixed-point (average-damp (lambda (y) (/ x y)))
; 1.0))
(define (cube-root x)
(fixed-point (average-damp (lambda (y) (/ x (square y))))
(define dx 0.00001)
(define (deriv g)
(lambda (x) (/ (- (g (+ x dx)) (g x)) dx)))
(define (cube x)
(* x x x))
(define (newton-transform g)
(lambda (x) (- x (/ (g x) ((deriv g) x)))))
(define (newtons-method g guess)
(fixed-point (newton-transform g) guess))
;(define (nice-sqrt x)
; (newtons-method
; (lambda (y) (- (square y) x)) 1.0))
(define (fixed-point-of-transform g transform guess)
(fixed-point (transform g) guess))
(define (nice-sqrt x)
(lambda (y) (- (square y) x)) newton-transform 1.0))