; recursive version
(define (f-rec n)
(cond ((<= n 3) n)
(else (+ (f-rec (- n 1))
(* 2 (f-rec (- n 2)))
(* 3 (f-rec (- n 3)))))))
; iterative version
(define (f n)
(cond ((<= n 3) n)
(else (f-iter 1 2 3 3 n))))
(define (f-iter fn-1 fn-2 fn-3 last-calc end)
(cond ((= last-calc end) fn-3)
(else (f-iter fn-2 fn-3 (+ fn-3 (* 2 fn-2) (* 3 fn-1)) (+ last-calc 1) end))))
No comments:
Post a Comment