Talk:Clojure Programming/Examples/Lazy Fibonacci

The lazy fibonacci seqence does not work anymore. Current Clojure no longer knows lazy-cons. lazy-seq should be used instead, so, something like:

(defn fibonacci ([] (fibonacci 0 1)) ([m n]     (lazy-seq       (cons m (fibonacci n (+ n m))))))

(take 20 (fibonacci))

or:

(def fib-seq ((fn rfib [a b]      (lazy-seq (cons a (rfib b (+ a b))))) 0 1))

(take 20 fib-seq)

¿Why not change the obsolete versions?

The second example on corecursion doesn't work ... ?? (def fib-seq (cons 1 (reductions + (first fib-seq) fib-seq))) ?? ... and I can't imagine it ever worked. fib-seq is not a reduction of all itself, except the first three terms. So better delete it.

The reductions based needs to also access the fib-seq var from within a lazy-seq/lazy-cat call like so:

(def fib-seq (lazy-seq (cons 0 (reductions + 1 fib-seq)))) (take 20 fib-seq) (0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181)