首页 > 代码库 > Exercise 1.9

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?

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

第一个函数:

  inc (+ (dec a) b),递归调用之后,还需要回到原函数进行inc计算,因此,是一个recursive process 

第二个函数:

  尾递归,是一个iterative process

Exercise 1.9