I think I'll be putting my money on (for) returning a cached lazy seq, and (macroexpand) also tells me that there's a lazy-cons hidin in there:
user=> (macroexpand '(for [i (range 1 3)] (do (println i) i))) (let* [iter__4007 (clojure.core/fn iter__7 [s__8] (clojure.core/when-first [i s__8] (clojure.core/when true (if true (clojure.core/lazy-cons (do (println i) i) (iter__7 (clojure.core/rest s__8))) (recur (clojure.core/rest s__8))))))] (iter__4007 (range 1 3))) So the side-effects are only executed once, and then the result is cached, and if you ask Rich he'd probably tell you that that is by design. On Tue, Dec 16, 2008 at 7:02 AM, Brian Doyle <brianpdo...@gmail.com> wrote: > I'll take a crack at this. It may appear that the doall and dorun return > something > different with subsequent calls but they don't actually. The doall always > returns > the sequence (1 2) and dorun always returns nil. > > The first time (doall x) is called the for loop executes and prints 1 2 (a > side effect > and is not returned from the for loop) and then returns the seq. The second > time > it's called x is already assigned the seq and just returns it. It does not > execute > the for loop again. > > The dorun call is similar, but instead of returning the seq of the for loop, > it always > returns nil. According to the api docs, dorun is used to force side > effects. > > On Mon, Dec 15, 2008 at 9:19 PM, wubbie <sunj...@gmail.com> wrote: >> >> Hello, >> >> doall and dorun returns different results from seond run on... >> e.g. >> user=> (def x (for [i (range 1 3)] (do (println i) i))) >> #'user/x >> user=> (doall x) >> 1 >> 2 >> (1 2) >> user=> (doall x) >> (1 2) >> user=> (doall x) >> (1 2) >> user=> >> >> user=> (def x (for [i (range 1 3)] (do (println i) i))) >> #'user/x >> user=> (dorun x) >> 1 >> 2 >> nil >> user=> (dorun x) >> nil >> user=> (dorun x) >> nil >> user=> >> >> >> What's the explanation for that? >> >> thanks >> sun >> >> >> >> > > > > > -- Venlig hilsen / Kind regards, Christian Vest Hansen. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---