Skip to content

Instantly share code, notes, and snippets.

@timmc
Forked from thiagofm/fib.clj
Created October 9, 2012 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timmc/3861689 to your computer and use it in GitHub Desktop.
Save timmc/3861689 to your computer and use it in GitHub Desktop.
fib clojure
;; Changes:
;; - use + instead of reduce +
;; - only calculate (+ actual-n next-n) once per iteration
;; - No dangling closing parens
;; - dec instead of (- ... 1) (to taste)
;; - zero? instead of (= ... 0)
;; - reverse the if clauses to take care of the base case first
(defn fib-list
[actual-n next-n iterations-left full-list]
(if (zero? iterations-left)
full-list
(let [n2 (+ actual-n next-n)]
(fib-list n2 actual-n (dec iterations-left) (conj full-list n2)))))
(defn -main
[& args]
(println (fib-list 0 1 10 [0])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment