On Fri, Dec 26, 2008 at 10:43 PM, 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

This is because println returns nil every time it's run.

user=> (println "test")
test
nil
user=>

Also:

user=> (for [x (range 3)] nil)
(nil nil nil)
user=>

So the result of the for is a seq containing three nils.  The output
from println is interspersed with the printing of the seq.

> and nothing at all when run from a script?

It doesn't print anything when run from a script because range returns
a lazy sequence.  Here's a recent post with a similar question:

http://groups.google.com/group/clojure/browse_thread/thread/53a32d222c3b40e3#

Chouser said the following:

> > This does not work - prints nothing - why?:
> > (defn pretty-print-row [row]
> >  (map print row))
>
> Because 'map' is lazy, and won't evaluate 'print' on any of the items
> sequence unless necessary.  For functions with side-effects (like
> 'print') you'll generally need 'doseq', loop/recur, or 'dorun'.

-- 
Michael Wood <esiot...@gmail.com>

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