huangzworks / SICP-answers

我的 SICP 解题集

Home Page:http://sicp.readthedocs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

练习 1.7 goodenough 的判断

jtr109 opened this issue · comments

其中 goodenough 的判断应该是 <.

goodenoughtrue 的条件应该是 old-guessnew-guess 足够接近, 所以应该是差的绝对值越小越好. 而且如果使用 >, 大部分情况下, 例如 x = 2, 在第一轮判断中, old-guess = 1, new-guess = 1.5. 则绝对值的差为 0.5, 大于 0.01, 直接跳出递归.

(define (good-enough? old-guess new-guess)
    (< 0.01
       (/ (abs (- new-guess old-guess))
          old-guess)))

考虑到不对原有定义的破坏, 我建议将 old-guess 和 new-guess 的比较放在 goodenough 中处理, 如下:

(define (goodenough? guess x)
  (< (/ (abs (- guess
		(improve guess x)))
        guess)
     (expt 0.1 4)))

完整的代码可以点击这里, 供参考.