Why does this not give output

103 views
Skip to first unread message

Cecil Westerhof

unread,
Aug 5, 2014, 11:41:08 AM8/5/14
to clo...@googlegroups.com
I wanted to play again with Clojure. ;-)

I wanted to test the random generator. For this I wrote:
(def intArr (make-array Integer 10))

(for [i (range 10)]
  (aset intArr i (int 0)))
(for [i (range 10000)]
  (let [index (rand-int 10)]
    (aset intArr index (int (inc (aget intArr index))))))
(for [i (range 10)]
  (println (aget intArr i)))


​When run in the REPL it gives the output I expect, but when executed as a program, it does not give any output at all. What is going on here?

​By the way, one run in the REPL gave min value 961 and max value 1044. Are this values that can be expected with a good random generator?​

--
Cecil Westerhof

Michael Klishin

unread,
Aug 5, 2014, 11:43:58 AM8/5/14
to clo...@googlegroups.com, Cecil Westerhof
On 5 August 2014 at 19:41:03, Cecil Westerhof (cldwes...@gmail.com) wrote:
> > ​When run in the REPL it gives the output I expect, but when executed
> as a program, it does not give any output at all. What is going on
> here?

Because `for` is lazy. In the REPL the result has to be computed because it needs
to be converted to a string. Wrap it in `doall`, that will force evaluation. 
--
@michaelklishin, github.com/michaelklishin

Thomas Heller

unread,
Aug 5, 2014, 1:04:58 PM8/5/14
to clo...@googlegroups.com
If you don't need the result of the for loop (which you don't in your example) use doseq.

Same syntax as "for" but not lazy and no return value (well, nil to be exact)

(doseq [i (range 10)]

  (aset intArr i (int 0)))
...

Cecil Westerhof

unread,
Aug 5, 2014, 6:14:00 PM8/5/14
to clo...@googlegroups.com
2014-08-05 19:04 GMT+02:00 Thomas Heller <th.h...@gmail.com>:
If you don't need the result of the for loop (which you don't in your example) use doseq.

Same syntax as "for" but not lazy and no return value (well, nil to be exact)

​I already use dotimes, or is doseq better?​
 

--
Cecil Westerhof

Thomas Heller

unread,
Aug 6, 2014, 3:49:08 AM8/6/14
to clo...@googlegroups.com
dotimes is for doing things n times. doseq is for seqs. Use dotimes when you can, doseq when you can't. 


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/zNA7S-zdL94/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Cecil Westerhof

unread,
Aug 6, 2014, 6:18:55 AM8/6/14
to clo...@googlegroups.com
2014-08-06 9:48 GMT+02:00 Thomas Heller <in...@zilence.net>:
dotimes is for doing things n times. doseq is for seqs. Use dotimes when you can, doseq when you can't. 

​OK, I thought so, but wanted to be sure.​

And I also use doseq on a place where dotimes would not work:
    (doseq [arrayRange [10 100 1000 2000 5000 10000]]

I still have a lot to learn, but I certainly like the expressiveness of clojure.
 
 
On Wed, Aug 6, 2014 at 12:13 AM, Cecil Westerhof <cldwes...@gmail.com> wrote:
2014-08-05 19:04 GMT+02:00 Thomas Heller <th.h...@gmail.com>:

If you don't need the result of the for loop (which you don't in your example) use doseq.

Same syntax as "for" but not lazy and no return value (well, nil to be exact)

​I already use dotimes, or is doseq better?​
 

--
Cecil Westerhof
Reply all
Reply to author
Forward
0 new messages