Skip to content

Instantly share code, notes, and snippets.

@fronx
Created August 3, 2011 21:08
Show Gist options
  • Save fronx/1123775 to your computer and use it in GitHub Desktop.
Save fronx/1123775 to your computer and use it in GitHub Desktop.
different fibonacci implementations
(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