On Fri, 26 Dec 2008 14:43:23 -0600
"Mark Volkmann" <r.mark.volkm...@gmail.com> wrote:

>
>Why does
>
>(for [x (range 3)] (println x))
>
>output
>
>(0
>nil 1
>nil 2
>nil)
>
>when run in the REPL instead of
>
>0
>1
>2
>
>and nothing at all when run from a script?
>

The seq of nils is the return value of `(for ...)'. It is printed
because the REPL shows return values and interpersed with println's
output, because `for' is lazy and the printlns are executed while the
seq is printed.

When run from a script (as a top level form, I assume), the return
value of the form is not used, therefore the tail-functions of the lazy
sequence aren't evaluated and nothing at all is printed. This can be
remedied by surrounding the `(for ...)' form with a `doall' or `dorun'
form, which both evaluate the whole sequence (they differ in their
return values).

In this case `doseq' should be used instead of `for'.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to