SICP-1.1.6节练习
SICP-1.2.1节练习

SICP-1.1.7节练习

lispor posted @ Feb 12, 2011 05:13:50 AM in Scheme with tags SICP , 1685 阅读

练习 1.6 - 1.8

 
练习 1.6:
Alyssa P. Hacker doesn't see why if needs to be provided as a special form. “Why can't I just define it as an ordinary 
procedure in terms of cond?” she asks. Alyssa's friend Eva Lu Ator claims this can indeed be done, and she defines a
new version of if:

(define (new-if predicate then-clause else-clause)
   (cond (predicate then-clause)
      (else else-clause)))

Eva demonstrates the program for Alyssa:

(new-if (= 2 3) 0 5)
5
(new-if (= 1 1) 0 5)
0

Delighted, Alyssa uses new-if to rewrite the square-root program:

(define (sqrt-iter guess x)
  (new-if (good-enough? guess x)
          guess
          (sqrt-iter (improve guess x)
                     x)))

What happens when Alyssa attempts to use this to compute square roots? Explain.
我的解答:
sqrt 函数将会报错:“Error:Stack overflow!”,
因为 sqrt-iter 是一个递归过程,而其过程体内调用 new-if 过程时,
new-if 会首先对它的三个参数求值,进而进入 sqrt-iter 过程体,直至报错!
 
练习 1.7:
The good-enough? test used in computing square roots will not be very effective for finding the square roots of very 
small numbers. Also, in real computers, arithmetic operations are almost always performed with limited precision. This
makes our test inadequate for very large numbers. Explain these statements, with examples showing how the test fails
for small and large numbers. An alternative strategy for implementing good-enough? is to watch how guess changes
from one iteration to the next and to stop when the change is a very small fraction of the guess. Design a square-root 
procedure that uses this kind of end test. Does this work better for small and large numbers?
我的解答:
当所给数小于 0.001 时,所求结果误差较大,如:
scheme@(guile-user)> (sqrt 0.0001)
0.0323084483304812
;<主要是因为 good-enough? 过程中 0.001 数值决定误差大小的

当所给数较大时,求值过程将陷入死循环,如:
scheme@(guile-user)> (sqrt 2e35)
C-c ERROR: In procedure scm-error:
ERROR: User interrupt
;<当 guess 接近于 2e35 的平方根时,guess 值非常大,将会导致计算结果在很大区间波动,以至于再(improve guess)也无法满足 good-enough? 过程>

改进后的 good-enough? 版本的 sqrt:
(define (sqrt x)
  (define last-guess 0)
  (define (good-enough? guess)
    (let ((diff (- guess last-guess)))
      (set! last-guess guess)
      (< (abs (/ diff guess))
         0.001)))
  (define (improve guess)
    (average guess (/ x guess)))
  (define (sqrt-iter guess)
    (if (good-enough? guess)
        guess
        (sqrt-iter (improve guess))))
  (sqrt-iter 1.0))
 
 
练习 1.8:
Newton's method for cube roots is based on the fact that if y is an approximation to the cube root of x, then a better 
approximation is given by the value
x/y^2 + 2y
----------
    3
Use this formula to implement a cube-root procedure analogous to the square-root procedure. (In section 1-3-4 we
will see how to implement Newton's method in general as an abstraction of these square-root and cube-root procedures.)
我的解答:
一、课程版<未改进版>:
(define (curt x)
  (define (good-enough? guess)
    (< (abs (- (cube guess) x))
       0.001))
  (define (improve guess)
    (/ (+ (/ x (square guess))
          (* 2 guess))
       3))
  (define (curt-iter guess)
    (if (good-enough? guess)
        guess
        (curt-iter (improve guess))))
  (curt-iter 1.0))
二、根据练习 1.7 的改进版:
(define (curt x)
  (define last-guess 0)
  (define (good-enough? guess)
    (let ((diff (- guess last-guess)))
      (set! last-guess guess)
      (< (abs (/ diff guess))
         0.001)))
  (define (improve guess)
    (/ (+ (/ x (square guess))
          (* 2 guess))
       3))
  (define (curt-iter guess)
    (if (good-enough? guess)
        guess
        (curt-iter (improve guess))))
  (curt-iter 1.0))
 
 
 
 
 
Leo Nagle 说:
May 01, 2019 11:52:02 PM

With write of these lines on here you did very good work from developers of codes can get better working progresses. If you are interested into hiring of 

top online resume writing services then read reviews from official sites.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter