On Sun, Sep 11, 2011 at 8:28 AM, Stuart Halloway <stuart.hallo...@gmail.com> wrote: > cycle actually calls lazy-seq. A quick way to check such things at the REPL > is with source: > user=> (source cycle) > (defn cycle > "Returns a lazy (infinite!) sequence of repetitions of the items in coll." > {:added "1.0" > :static true} > [coll] (lazy-seq > (when-let [s (seq coll)] > (concat s (cycle s))))) > <snip>
Is there any reason not to define it as (defn cycle "Returns a lazy (infinite!) sequence of repetitions of the items in coll." {:added "1.0" :static true} [coll] (let [l (lazy-seq (when-let [s (seq coll)] (concat s l)))) This will have the effect that holding any element of the cycle holds the head; however, even the original holds the head of s as long as the cycle is referenced, so that at worst doubles memory use for finite s, and for finite walks along the cycle with infinite s, as s (or the first N elements of s) get held onto in both cases, but an equal number of cycle elements get held onto with the self-referencing cycle instead of potentially only one at a time. > Now I'll test it with 9876543210, a number which "ev?" was able to > handle: > > user=> (time (mod3 9876543210)) > "Elapsed time: 37759.615 msecs" > 1 > user=> (mod 987654321 3) > 0 > > Whoa! The computation finished in reasonable time, but with the WRONG > answer! How did that happen? > Did I find a bug? > > No, there is simply a typo in your input arg. True, but both numbers are congruent to 0 mod 3, so where did the 1 come from in (time (mod3 9876543210))? -- Protege: What is this seething mass of parentheses?! Master: Your father's Lisp REPL. This is the language of a true hacker. Not as clumsy or random as C++; a language for a more civilized age. -- 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 Note that posts from new members are moderated - please be patient with your first post. 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