Skip to content

Instantly share code, notes, and snippets.

@enigmaticape
Created November 4, 2012 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enigmaticape/4012763 to your computer and use it in GitHub Desktop.
Save enigmaticape/4012763 to your computer and use it in GitHub Desktop.
Iterative function f(n) from SICP exercise 1.11
; 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)))
@enigmaticape
Copy link
Author

From a discussion of the solution to SICP exercise 1.11 at http://www.enigmaticape.com/blog/sicp-exercise-1-11-iterative-function-fn/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment