Created
May 9, 2020 22:38
-
-
Save kocubinski/5a83151e69d4fba6b976b294a81443b4 to your computer and use it in GitHub Desktop.
Exercise 1.11 from SICP
This file contains 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
(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