;; exercises 1.46

(define (iterative-improve f enough? first-guess)
  (define (iter guess)
    (if (enough? guess)
        guess
        (iter (f guess))))
  (iter first-guess))

(define (sqrt-iter-imp x)
  (iterative-improve (lambda (guess) (average guess (/ x guess)))
                     (lambda (guess) (< (abs (- (square guess) x)) 0.001))
                     1.0))
(define (fixed-point-iter-imp f first-guess)
  (iterative-improve f
                     (lambda (guess) (< (abs (- guess (f guess))) 0.001))
                     first-guess))

이건 내꺼

;; ex 1.46 - iterative-improve
(define (iterative-improve good? improve)
  (lambda (guess)
    (define (iter v)
      (if (good? v)
          v
          (iter (improve v))))
    (iter guess)))

;; sqrt
(define (sqrt-ii x)
  (define (good? guess)
    (< (abs (- (square guess) x)) 0.001))
  (define (improve guess)
    (/ (+ guess (/ x guess)) 2))
  ((iterative-improve good? improve) 1.0))

(sqrt-ii 9)

;; fixed-point
(define (fixed-point-ii f g)
  (define (good? guess)
    (< (abs (- guess (f guess)) 0.00001)))
  ((iterative-improve good? f) g))

(fixed-point cos 1.0)
신고

티스토리 툴바