Skip to content

Instantly share code, notes, and snippets.

@JamesMcMahon
Last active November 8, 2015 01:11
Show Gist options
  • Save JamesMcMahon/662abe8970f5ca87019b to your computer and use it in GitHub Desktop.
Save JamesMcMahon/662abe8970f5ca87019b to your computer and use it in GitHub Desktop.
Fibonacci generator in Clojure. Playing with memoization.
; 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