SICP-1.1.7节练习

SICP-1.1.6节练习

lispor posted @ Feb 11, 2011 01:58:13 AM in Scheme with tags SICP , 2164 阅读

练习 1.1 - 1.5

 
练习 1.1:
Below is a sequence of expressions. What is the result printed by the interpreter in
response to each expression? Assume that the sequence is to be evaluated in the order
in which it is presented.
10
(+ 5 3 4)
(- 9 1)
(/ 6 2)
(+ (* 2 4) (- 4 6))
(define a 3)
(define b (+ a 1))
(+ a b (* a b))
(= a b)
(if (and (> b a) (< b (* a b)))
    b
    a)
(cond ((= a 4) 6)
      ((= b 4) (+ 6 7 a))
      (else 25))
(+ 2 (if (> b a) b a))
(* (cond ((> a b) a)
         ((< a b) b)
         (else -1))
   (+ a 1))
我的解答:
scheme@(guile-user)> 10
10
scheme@(guile-user)> (+ 5 3 4)
12
scheme@(guile-user)> (- 9 1)
8
scheme@(guile-user)> (/ 6 2)
3
scheme@(guile-user)> (+ (* 2 4) (- 4 6))
6
scheme@(guile-user)> (define a 3)
scheme@(guile-user)> (define b (+ a 1))
scheme@(guile-user)> (+ a b (* a b))
19
scheme@(guile-user)> (= a b)
#f
scheme@(guile-user)> (if (and (> b a) (< b (* a b)))
                         b
                         a)
4
scheme@(guile-user)> (cond ((= a 4) 6)
                           ((= b 4) (+ 6 7 a))
                           (else 25))
16
scheme@(guile-user)> (+ 2 (if (> b a) b a))
6
scheme@(guile-user)> (* (cond ((> a b) a)
                              ((< a b) b)
                              (else -1))
                        (+ a 1))
16
 
练习 1.2:
Translate the following expression into prefix form.
5 + 4 + (2 - (3 - (6 + 4/5)))
-----------------------------
       3(6 - 2)(2 - 7)
我的解答:
scheme@(guile-user)> (/ (+ 5 4 (- 2 (- 3 (+ 6 4/5))))
                        (* 3 (- 6 2) (- 2 7)))
-37/150
 
练习 1.3:
Define a procedure that takes three numbers as arguments and returns the sum of the 
squares of the two larger numbers.
我的解答:
(define (square x)
  (* x x))

(define (sum-of-square x y)
  (+ (square x)
     (square y)))

(define (sum-of-square-of-the-two-larger-numbers x y z)
  (cond ((and (<= x y) (<= x z))
         (sum-of-square y z))
        ((and (<= y x) (<= y z))
         (sum-of-square x z))
        (else (sum-of-square x y))))
 
练习 1.4:
Observe that our model of evaluation allows for combinations whose operators are 
compound expressions. Use this observation to describe the behavior of the following
procedure:

     (define (a-plus-abs-b a b)
       ((if (> b 0) + -) a b))
我的解答:
1:(a-plus-abs-b 1 2)
=>((if (> 2 0) + -) 1 2)
=>(+ 1 2)
=>3

2:(a-plus-abs-b 1 -2)
=>((if (> -2 0) + -) 1 -2)
=>(- 1 2)
=>-1
 
练习 1.5:
Ben Bitdiddle has invented a test to determine whether the interpreter he is faced with
is using applicative-order evaluation or normal-order evaluation. He defines the following
two procedures:

     (define (p) (p))

     (define (test x y)
       (if (= x 0)
           0
           y))

Then he evaluates the expression

     (test 0 (p))

What behavior will Ben observe with an interpreter that uses applicative-order evaluation?
What behavior will he observe with an interpreter that uses normal-order evaluation? 
Explain your answer. (Assume that the evaluation rule for the special form if is the same
whether the interpreter is using normal or applicative order: The predicate expression is 
evaluated first, and the result determines whether to evaluate the consequent or the
alternative expression.)
我的解答:
应用序求值解释器<applicative-order>将会陷入死循环,而正则序求值解释器将会返回 0。
在应用序求值解释器中,运行 (test 0 (p)) 表达式时,会分别对 test、0、(p) 三个子表达式求值,
然后用 0 和 (p) 这两个表达式的结果作为 test 表达式的结果的参数再求值,而在求 (p) 表达式时,
这个过程调用其自身,没有退出语句,将会陷入死循环;
在正则序求值解释器中,运行 (test 0 (p)) 表达式时,其代换过程如下:
(test 0 (p))
=>(if (= 0 0)   ; 根据练习中规定的 if 特殊形式的求值规则:
      0         ; 其中的谓词部分先行求值,根据其结果确定随后求值的子表达式部分
      (p))      ; 在此,(p) 表达是不须求值
=>0

 

Avatar_small
IWRose 说:
Feb 11, 2011 04:37:29 PM

对于第一章,表示还没读完,进度和你差不多。学习起来很有意思,就是知识点浓度有点高,看得慢。


登录 *


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