Created
November 4, 2012 17:50
-
-
Save enigmaticape/4012763 to your computer and use it in GitHub Desktop.
Iterative function f(n) from SICP exercise 1.11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; f(n) = n if n < 3, | |
; f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) if n > 3 | |
; | |
; iterative (via tail recursion optimisation) | |
(define (F-iter a b c count) | |
(if (= count 0) | |
c | |
(F-iter (+ a (* 2 b) (* 3 c)) | |
a | |
b | |
(- count 1) ) )) | |
(define (F n) | |
(if (< n 3) | |
n | |
(F-iter 2 1 0 n))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From a discussion of the solution to SICP exercise 1.11 at http://www.enigmaticape.com/blog/sicp-exercise-1-11-iterative-function-fn/