sicp每日一题[1.46]

Exercise 1.46

Several of the numerical methods described in this chapter are instances of an extremely general computational strategy known as iterative improvement. Iterative improvement says that to compute something, we start with aninitial guess for the answer, test if the guess is good enough, and otherwise improve the guess and continue the process using the improved guess as the new guess. Write a procedure iterative-improve that takes two procedures as arguments: a method for telling whether a guess is good enough and a method for improving a guess. iterative improve should return as its value a procedure that takes a guess as argument and keeps improving the guess until it is good enough. Rewrite the sqrt procedure of Section 1.1.7 and the fixed-point procedure of Section 1.3.3 in terms of iterative-improve.


(define tolerance 0.00001)

(define (good-enough? guess target tolerance)
  (< (abs (- guess target)) tolerance))

; 注意:这个函数的两个参数以及返回值都是函数,所以调用的语句外还要传一个参数作为 first-guess
(define (iterative-improve good-enough? improve-guess)
  (lambda (first-guess)
    (define (iter guess)
      (if (good-enough? guess)
          guess
          (iter (improve-guess guess))))
    (iter first-guess)))

; Rewrite Version
; 1.1.7 Square Roots by Newton's Method
(define (sqrt x)
  ((iterative-improve (lambda (guess) (good-enough? (square guess) x tolerance))
                      (lambda (guess) (average guess (/ x guess))))
   1.0))

; 1.3.3 Procedures as General Methods
(define (fixed-point f first-guess)
  ((iterative-improve (lambda (guess) (good-enough? guess (f guess) tolerance))
                      f)
   first-guess))


(sqrt 2)
(sqrt 10)

(fixed-point cos 1.0)
(fixed-point (lambda (x) (+ (sin x) (cos x))) 1.0)


; 执行结果
1.4142156862745097
3.162277665175675
0.7390893414033927
1.2587228743052672
posted @   再思即可  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示