SICP实例 1.2.1 (阶乘的递归与迭代)

mac2022-06-30  28

递归:

(define (factorial n) (if (= n 1) 1 (* n (factorial(- n 1)))))

(factorial 3)

 

迭代:

(define (factorial n) (fact-iter 1 1 n))

(define (fact-iter product counter max-counter) (if (> counter max-counter) product (fact-iter (* counter product) (+ counter 1) max-counter)))

(factorial 2)

发现了递归与迭代的区别:

递归:先展开后收缩.

迭代:状态用固定数目的状态变量描述.

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

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)