SICP-Exercise 1.9

Exercise 1.9.  Each of the following two procedures defines a method for adding twopositive integers in terms of the procedures inc,which increments its argument by 1, and dec, which decrementsits 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 eachprocedure in evaluating(+ 4 5). Are these processes iterative or recursive?

逐步替代+函数。(+ 2 3 )好了。

第一个+函数:recursive

(+ 2 3)
(inc (+ 1 3))
(inc (inc (+ 0 3)))
(inc (inc 3))
(inc 4)
5

第二个+函数:iterative

(+ 2 3)
(+ 1 4)
(+ 0 5)
5

尾递归的一个特点,被替代的函数+,作为表达式的第一个符号(+ (dec a) (inc b))。


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)); For example, (k n) computes 5n2.

Give concise mathematical definitions for the functions computed bythe proceduresf, g, and h for positive integervalues of n.

解:

(A 1 10);展开

(A 0 (A 1 9))→ 2*(A 1 9)→2*2*(A 1 8)→

所以

(define (f n) (A 0 n)) 表示2*n

(define (g n) (A 1 n))表示2n.

(A 2 4);展开
(A 1 (A 2 3))
(A 0 (- (A 2 3) 1)→不好搞了

我们反过来推算,可以得到,

(A 2 1)→2

(A 2 2)→(A 1 (A 2 1)) →(A 1 2)→22

(A 2 3) →(A 1 (A 2 2))→(A 1 4) → 222

(A 2 4)→(A 1 (A 2 3)))→(A 1 16)→ 2222

所以(define (h n) (A 2 n))表示2的平方的平方的平方的....怎样写啊??(2**2)**n ??


(A 3 3)→(A 2 (A 3 2))...

反过来推算吧

(A 3 1)→2

(A 3 2)→(A 2 (A 3 1))→(A 2 2)→2*2

(A 3 3)→→(A 2 (A 3 2))→(A 2 4)→ 2222

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值