Using a good old sequence of vectors: (defn fibo [] (map first (iterate (fn [[a b]] [b (+ a b)]) [0 1])))
Christophe Stuart Halloway a écrit : > I have updated the sample source from the book > (http://tinyurl.com/clojure-samples > ) to the new laziness. Along the way, I replaced the lazy-cons based > implementation of the fibonacci numbers with this: > > (defn fibo > ([] > (concat [0 1] (fibo 0 1))) > ([a b] > (let [n (+ a b)] > (lazy-seq > (cons n (fibo b n)))))) > > Is there a better/more idiomatic approach, without resorting to code > in clojure-contrib? Test your code against the following expression to > flush out stack and heap overflows. > > (rem (nth (fibo) 1000000) 1000) > -> 875 > > Also, the current 'fibs' implementation in clojure.contrib.seq fails > the test above, because it holds the entire sequence as it goes. We > should replace it with whatever the community comes up with on this > thread. > > Cheers, > Stu > > > > > -- Professional: http://cgrand.net/ (fr) On Clojure: http://clj-me.blogspot.com/ (en) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---