Exercise 1.11. A function f is defined by the rule that f(n) = n if n<3 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) if n> 3. Write a procedure that computes f by means of a recursive process. Write a procedure that computes f by means of an iterative process.
recursive process:
(define (fn x)
(if (< x 3)
x
(+ (fn (- x 1))
(* 2 (fn (- x 2)))
(* 3 (fn (- x 3))))))
iterative process:
(define (fn-iter f1 f2 f3 count)
(cond ((= count 0) f3)
((= count 1) f2)
((= count 2) f1)
(else (fn-iter (+ f1
(* 2 f2)
(* 3 f3))
f1
f2
(- count 1)))))
(define (fn2 n)
(if (< n 3)
n
(fn-iter 2 1 0 n)))