Skip to content

Instantly share code, notes, and snippets.

@kocubinski
Created May 9, 2020 22:38
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 kocubinski/5a83151e69d4fba6b976b294a81443b4 to your computer and use it in GitHub Desktop.
Save kocubinski/5a83151e69d4fba6b976b294a81443b4 to your computer and use it in GitHub Desktop.
Exercise 1.11 from SICP
(define (f-11-tree n)
(define (f n depth)
(println-depth depth n)
(if (< n 3)
n
(+ (f (- n 1) (inc depth))
(* 2 (f (- n 2) (inc depth)))
(* 3 (f (- n 3) (inc depth))))))
(f n 0))
;; a <- a + 2b + 3c
;; b <- a
;; c <- b
(define (f11-iter a b c n)
(println a b c n)
;; degenerate cases
(cond ((= 2 n) a)
((= 1 n) b)
((= 0 n) c)
(else
(f11-iter (+ a (* 2 b) (* 3 c))
a
b
(dec n)))))
(define (f11 n)
(f11-iter 2 1 0 n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment