# SICP-1.1.6节练习

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

```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```

```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
```

```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))))```

```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```

```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))
=>(if (= 0 0)   ; 根据练习中规定的 if 特殊形式的求值规则：
0         ; 其中的谓词部分先行求值，根据其结果确定随后求值的子表达式部分
(p))      ; 在此，(p) 表达是不须求值
=>0
```

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

(输入验证码)
or Ctrl+Enter