SICP-1.2.4节练习
SICP-1.2.6节练习

SICP-1.2.5节练习

lispor posted @ Feb 18, 2011 04:11:55 PM in Scheme with tags SICP , 1379 阅读

练习 1.20

 
练习1.20:
The process that a procedure generates is of course dependent on the rules used by the
interpreter. As an example, consider the iterative gcd procedure given above. Suppose we were to
interpret this procedure using normal-order evaluation, as discussed in section 1-1-5. (The
normal-order-evaluation rule for if is described in Exercise 1-5.) Using the substitution method
(for normal order), illustrate the process generated in evaluating (gcd 206 40) and indicate the
remainder operations that are actually performed. How many remainder operations are actually
performed in the normal-order evaluation of (gcd 206 40)? In the applicative-order evaluation?
我的解答:
(gcd 206 40)
=>
(if (= 40 0)
    ...)
=>
(gcd 40
     (remainder 206 40))
=>
(if (= (remainder 206 40) 0)
    ...)
=>
(if (= 6 0)
    ...)
=>
(gcd (remainder 206 40)
     (remainder 40 (remainder 206 40)))
=>
(if (= (remainder 40 (remainder 206 40)) 0)
    ...)
=>
(if (= 4 0)
    ...)
=>
(gcd
 (remainder 40 (remainder 206 40))
 (remainder (remainder 206 40)
            (remainder 40
                       (remainder 206 40))))
=>
(if (= (remainder (remainder 206 40)
                  (remainder 40
                             (remainder 206 40)))
       0)
    ...)
=>
(if (= 2 0)
    ...)
=>
(gcd (remainder (remainder 206 40)
                (remainder 40 (remainder 206 40)))
     (remainder (remainder 40 (remainder 206 40))
                (remainder (remainder 206 40)
                           (remainder 40
                                      (remainder 206 40)))))
=>
(if (= (remainder (remainder 40
                             (remainder 206 40))
                  (remainder (remainder 206 40)
                             (remainder 40
                                        (remainder 206 40))))
       0)
    ...)
=>
(if (= 0 0)
    ...)
=>
(remainder (remainder 206 40)
           (remainder 40
                      (remainder 206 40)))
 

登录 *


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