SICP 第一章学习笔记


不是参考答案,只是自己的解,可能有谬误。

练习 1.1

10

12

8

3

6

a

b

19

(= a b ) #这个猜不出来是什么结果,运行一下发现是 #f

4

16

6

16

练习 1.2

(/ (+ 5 (+ 4 ( – 2 (- 3 ( + 6 (/ 4 5)))))) (* (- 2 7) (- 6 2) 3))

练习 1.3

(define (biggerTwo a b c)
  (if (< a b)
      (if (< a c)
          (+ b c)
          (+ a b))
      (if (< b c)
          (+ a c)
          (+ a b))))

Report this ad

练习 1.4

(define (a-plus-abs-b a b)
  ((if (> b 0) + -) a b))

上面的定义意思是如果 b 大于 0,那么 a + b,如果 b 小于 0,那么 a – b。所以结果就是 a 加上绝对值 b。因为运算符也可以当作组合式的普通的值,所以这样做是合法的。

练习 1.5

(define (p) (p))
(define (test x y)
  (if (= x 0)
      0
      y))
# 而后求值
   (test 0 (p))

上面的程序,如果解释器采用应用序求值,那么会短路,结果为0。

上面的程序,如果解释器采用正则序求值,那么会展开式子,导致陷入无限循环。因为求值 p 会递归会自身,而这个递归没有结束条件。

Exercise 1.9. Each of the following two procedures defines a method for adding two positive integers in terms of the procedures inc, which increments its argument by 1, and dec, which decrements its argument by 1.

(define (+ a b)
  (if (= a 0)
      b
      (inc (+ (dec a) b))))

(define (+ a b)
  (if (= a 0)
      b
      (+ (dec a) (inc b))))

Using the substitution model, illustrate the process generated by each procedure in evaluating (+ 4 5). Are these processes iterative or recursive?

答:第一个函数是recursive的。第二个函数是iterative的。因为第二个没有展开和递归调用自己。第一个是递归了的。

Exercise 1.10. The following procedure computes a mathematical function called Ackermann’s function.

(define (A x y)
  (cond ((= y 0) 0)
        ((= x 0) (* 2 y))
        ((= y 1) 2)
        (else (A (- x 1)
                 (A x (- y 1))))))

What are the values of the following expressions?

(A 1 10)

(A 2 4)

(A 3 3)

Consider the following procedures, where A is the procedure defined above:

(define (f n) (A 0 n))

(define (g n) (A 1 n))

(define (h n) (A 2 n))

(define (k n) (* 5 n n))

Give concise mathematical definitions for the functions computed by the procedures f, g, and h for positive integer values of n. For example, (k n) computes 5n2.

答:(f n) 计算 2y。(g n) 计算 2的n+1次方。(h n) 计算 2的2n+2次方。

正确答案:

(f n): 2n

(g n): 0 for n=0, 2^{n} for n>0

(h n): 0 for n=0, 2 for n=1, 2^2^… (n-1 times) for n>1