title: Fibonacci numbers in Clojure
body: <p>I'm learning Clojure. Quite basic task is to generate Fibonacci sequence. I end up with pretty much copy of the imperative solution (and list is reversed, huh):</p>
  
  <pre><code>(defn n-fib [n]
    (if (= n 1) '(1)
    (loop [i 2 l '(1 1)]
      (if (= i n)
          l
          (recur (inc i) (cons (+ (fst l) (snd l)) l))))))
  </code></pre>
  
  <p>What is the better way, more functional, concise? Lazy sequences? How to use them? For example, in Haskell using laziness I can write one liner:</p>
  
  <pre><code>fib = 1 : 1 : zipWith + (tail fib) 
  </code></pre>
  
  <p>Note that Haskell solution offers infinite sequence (laziness...). If Clojure both eager and lazy solutions can be (even like get n-length list) I would like to know both.</p>
  
  <p>Update: Another solution I got yields not reversed list, but it uses stack to generate it:</p>
  
  <pre><code>(defn n-fib [n]
    (defn gen [i a b]
      (if (= i 0)
          ()
          (cons (+ a b) (gen (dec i) b (+ a b)))))
    (gen n 0 1))
  </code></pre>