Mar 12, 2008

Linear Recursion and Iteration Sample.

A linear recursion code.

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


A linear iteration code.

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

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

No comments: