灯下 登录
计算机科学 / SICP / 1.2.1 Linear Recursion and Iteration

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?

练习 1.9:以下两个过程各定义了一种利用过程 inc(将参数加 1)和 dec(将参数减 1)来加两个正整数的方法。

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

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

使用代换模型,说明每个过程在求值 (+ 4 5) 时产生的计算过程。这些计算过程是迭代型的还是递归型的?

Racket #lang sicp
(define (+ a b)
 (if (= a 0)
 b
 (inc (+ (dec a) b))))

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