SICP习题 1.9 (递归与迭代初探)

mac2022-06-30  20

(define (plus-Recursive a b) (if (= a 0) b (inc (plus-Recursive (dec a) b))))

(define (inc n) (+ n 1))

(define (dec n) (- n 1))

(plus-Recursive 3 5)

从计算过程中可以很明显地看到伸展和收缩两个阶段,且伸展阶段所需的额外存储量和计算所需的步数都正比于参数 a ,说明这是一个线性递归计算过程。

 

(define (plus-Iteration a b) (if (= a 0) b (plus-Iteration (dec a) (inc b))))

(plus-Iteration 3 5)

从计算过程中可以看到,这个版本的 plus 函数只使用常量存储大小,且计算所需的步骤正比于参数 a ,说明这是一个线性迭代计算过程。

转载于:https://www.cnblogs.com/R4mble/p/7880943.html

相关资源:SICP 习题答案
最新回复(0)