内容 1.2 Procedures and the Processes They Generate · 22
练习 自检推理
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) 时产生的计算过程。这些计算过程是迭代型的还是递归型的?
SICP source code scheme
(define (+ a b)
(if (= a 0)
b
(inc (+ (dec a) b))))
(define (+ a b)
(if (= a 0)
b
(+ (dec a) (inc b)))) 我的笔记 自动保存