首页 > 其他分享 >sicp每日一题[1.46]

sicp每日一题[1.46]

时间:2024-09-06 11:47:26浏览次数:17  
标签:guess good 每日 iterative sicp define enough 1.46 improve

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

标签:guess,good,每日,iterative,sicp,define,enough,1.46,improve
From: https://www.cnblogs.com/think2times/p/18399962

相关文章

  • sicp每日一题[1.45]
    Exercise1.45WesawinSection1.3.3thatattemptingtocomputesquarerootsbynaivelyfindingafixedpointofy->x/ydoesnotconverge,andthatthiscanbefixedbyaveragedamping.Thesamemethodworksforfindingcuberootsasfixedpointsof......
  • 每日OJ_牛客_最长递增子序列(dp/贪心模板)
    目录牛客_最长递增子序列(dp/贪心模板)解析代码牛客_最长递增子序列(dp/贪心模板)最长公共子序列__牛客网解析代码在一个序列中找最长递增子序列,动态规划的典型应用,下面是两个模版CISdp模板:#include<iostream>#include<vector>usingnamespacestd;intLIS(vect......
  • 每日搜索论坛总结:2024年8月30日
    以下是今天在搜索论坛上发生的事件回顾,通过搜索引擎圆桌会议和其他网络搜索论坛的视角。Yelp起诉了Google,搜索营销行业对此感到好笑。Google为GoogleAds推出了新标签诊断和同意管理设置。Google表示不会在页面上计数文字或链接。Google正在统一Google商务资料和Google本地服......
  • 软设每日打卡——已知字符集{ a, b, c, d, e, f },若各字符出现的次数分别为{ 6, 3, 8,
    【题目】已知字符集{a,b,c,d,e,f},若各字符出现的次数分别为{6,3,8,2,10,4},则对应字符集中各字符的哈夫曼编码可能为( )        A.00,1011,01,1010,11,100        B.11,100,110,000,0010,01        C.10,1011,11,001......
  • sicp每日一题[1.44]
    Exercise1.44Theideaofsmoothingafunctionisanimportantconceptinsignalprocessing.Iffisafunctionanddxissomesmallnumber,thenthesmoothedversionoffisthefunctionwhosevalueatapointxistheaverageoff(x-dx),f(x),andf(x+......
  • 每日一题 背包,dp,兵营力量训练
    首先,读完这题我一开始有点懵,分析了条件后还是不知道怎么分配比较完美,一开始想一直给最小的那个分配呗,但这不知道分配的力量是多少,没有一个界线,所以要找一个界线,最后还是看了别人的参考答案,用二分确定会变的界线,然后bool数组检查有没有达到界线,没达到的都分配力量,分配的力量......
  • 每日OJ_牛客_最长公共子序列(dp模板)
    目录牛客_最长公共子序列(dp模板)解析代码牛客_最长公共子序列(dp模板)最长公共子序列__牛客网解析代码子序列即两个字符串中公共的字符,但不一定连续。        从题干中可以提取出问题:求字符串s和t的最长公共子序列假设LCS(m,n)为长度为m的字符串s与长度为n的......
  • 软设每日一练10——某文件系统在磁盘上建立了位示图(bitmap),记录磁盘的使用情况。
    【题目】某文件管理系统在磁盘上建立了位示图(bitmap),记录磁盘的使用情况。若计算系统的字长为32位,磁盘的容量为300GB,物理块的大小为4MB,那么位示图的大小需要(      )个字。        A.1200    B.2400    C.6400    D.9600      ......
  • sicp每日一题[1.43]
    Exercise1.43Iffisanumericalfunctionandnisapositiveinteger,thenwecanformthenthrepeatedapplicationoff,whichisdefinedtobethefunctionwhosevalueatxisf(f(...(f(x))...)).Forexample,iffisthefunctionx->x+1,......
  • 2024.9.2 Python,用栈写每日温度,等差数列划分,子串所有可能性,等差数列划分,深度优先搜索
    1.每日温度给定一个整数数组temperatures,表示每天的温度,返回一个数组answer,其中answer[i]是指对于第i天,下一个更高温度出现在几天后。如果气温在这之后都不会升高,请在该位置用0来代替。示例1:输入:temperatures=[73,74,75,71,69,72,76,73]输出:[1,1,4,2,......