-
-
Save timmc/3861689 to your computer and use it in GitHub Desktop.
fib clojure
This file contains 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
;; 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