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

posted on 2014-09-15 15:59  一生只想往前飞  阅读(128)  评论(0编辑  收藏  举报

导航