标签:
Exercise 1.9. Each of the following two procedures defines a method for adding twopositive integers in terms of the proceduresinc,which increments its argument by 1, anddec, 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
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/yqj2065/article/details/46939357