Exercise 1.11

A function \(f\) is defined as follows:

\[f(n)= \begin{cases} f(n-1) + 2f(n-2) + 3f(n-3),& \text{if } n\geq 3\\ n & \text{if } n\lt 3 \end{cases}\]

The following procedure computes \(f\) by means of a recursive process:

(define (f n)
  (if (< n 3)
      n
      (+ (f (- n 1)) (* 2 (f (- n 2))) (* 3 (f (- n 3))))))

The following procedure computes \(f\) by means of an iterative process:

(define (f n)
  (f-iter 2 1 0 n))

(define (f-iter a b c n)
  (if (< n 3)
      a
      (f-iter (+ a (* 2 b) (* 3 c))
              a
              b
              (dec n))))

See this StackOverflow post for more details on the iterative implementation.

Back to exercises