Created
August 3, 2011 21:08
-
-
Save fronx/1123775 to your computer and use it in GitHub Desktop.
different fibonacci implementations
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
| (def first-n-fib | |
| #(loop [l [] a 0 b 1 c 0] | |
| (if (= c %) | |
| l | |
| (recur | |
| (conj l a) | |
| b | |
| (+ a b) | |
| (+ 1 c))))) | |
| (first-n-fib 7) | |
| ; => [0 1 1 2 3 5 8] | |
| ;; | |
| (def first-n-fib (fn[n] | |
| (defn fib [a b] | |
| (cons a (lazy-seq (fib b (+ a b))))) | |
| (take n (fib 0 1)))) | |
| (first-n-fib 7) | |
| ; => (0 1 1 2 3 5 8) | |
| ;; | |
| (def first-n-fib (fn[n] | |
| (let [fib (fn [a b] | |
| (lazy-seq (cons a (fib b (+ a b)))))] | |
| (take n (fib 0 1))))) | |
| (first-n-fib 7) | |
| ; => (0 1 1 2 3 5 8) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment