Skip to content

Instantly share code, notes, and snippets.

@porcow
Created November 4, 2012 20:10
Show Gist options
  • Save porcow/4013465 to your computer and use it in GitHub Desktop.
Save porcow/4013465 to your computer and use it in GitHub Desktop.
SICP Exercise 1.19 Solution
(define (fib n)
(define (even? n)
(= (remainder n 2) 0))
(define (square n)
(* n n))
(define (double n)
(+ n n))
(define (fib-iter a b p q count)
(cond ((= count 0) b)
((even? count) (fib-iter a
b
(+ (square p) (square q))
(+ (square q) (double (* p q)))
(/ count 2)))
(else (fib-iter (+ (* b q) (* a q) (* a p))
(+ (* b p) (* a q))
p
q
(- count 1)))))
(fib-iter 1 0 0 1 n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment