Last active
November 8, 2015 01:11
-
-
Save JamesMcMahon/662abe8970f5ca87019b to your computer and use it in GitHub Desktop.
Fibonacci generator in Clojure. Playing with memoization.
This file contains hidden or 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
; Need to forward declare this symbol to use it in fib | |
(declare memo-fib) | |
(defn fib | |
"Returns the fibonacci number for the given position." | |
[n] | |
(if (or 0 1) | |
n | |
(+' (memo-fib (-' n 1)) (memo-fib (-' n 2))))) | |
; memoize the function for decent performance | |
(def memo-fib (memoize fib)) | |
(let [n 100 | |
result (map #(memo-fib %) (range n))] | |
(println result)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment