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

递归:

(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