I'm just getting into Clojure, and I wanted to make sure I am writing code the "Clojure way". The challenge I took on is Zeckendorf numbers (fairly trivial).
(defn fibs-until ([n] (vec (if (< n 3) (range 1 (inc n)) (concat [1 2] (fibs-until n 1 2))))) ([n a b] (let [fib (+ a b)] (if (> fib n)  (cons fib (fibs-until n b fib)))))) (defn zeckendorf ([n] (if (= 0 n) "0" (zeckendorf n (reverse (fibs-until n))))) ([n fibs] (if (= fibs ) "" (let [head (first fibs) tail (rest fibs)] (if (or (> head n) (= 0 n)) (str "0" (zeckendorf n tail)) (str "1" (zeckendorf (- n head) tail)))))))
A few review questions I have:
- Is there a more "Clojure way" to do this?
- I noticed that I am repeating the same (overloading-esque) pattern of defining functions that have two arities, and calling out to the second in the first. Is there a better way to do this?
- I wanted to use
[[head & tail] fibs]rather than
[head (first fibs) tail (rest fibs)]but I get a stack overflow. Why is this?