Skip to content

Instantly share code, notes, and snippets.

@jsp1611
Last active October 16, 2015 11:57
Show Gist options
  • Save jsp1611/28ce8e05bcef28196f54 to your computer and use it in GitHub Desktop.
Save jsp1611/28ce8e05bcef28196f54 to your computer and use it in GitHub Desktop.
Exercise 1.11 in SICP. First attempt is linear recursive.
;; Define a function f such that:
;; f(n) = n if n<3 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) if n> 3
;; This is the recursive implementation that doesn't scale well
(define (f n)
(if (< n 3)
n
(+ (f (- n 1))
(* 2 (f (- n 2)))
(* 3 (f (- n 3))))
)
)
;; Example call that will take some time to complete
(f 18)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment