On Sun Sep 25 21:51 2011, Dennis Haupt wrote: > (let [rand (new java.util.Random) nextInt (fn [a] (.nextInt rand))] > ((map (print) (iterate ((nextInt "dummy") 0))))) > > > the error is: > java.lang.ClassCastException: java.lang.Integer cannot be cast to > clojure.lang.IFn (NO_SOURCE_FILE:0) > > why does it want to cast my "0" to a function? and how can i get rid > of the dummy parameter [a]?
I don't think it's trying to cast your zero to a function. Instead,
here's what I think is going on:
In this expression:
((nextInt "dummy") 0)
The inner expression gets evaluated to some integer. The end result is:
(<some int> 0)
And that is where an integer is trying to be invoked as a function.
Ultimately, I am not sure that 'iterate' is really the function you
want. If you want an infinite lazy sequence of random integers, you
probably want something more like 'repeatedly':
(let [rand (java.util.Random.)
nextInt #(.nextInt rand)]
(map print (repeatedly nextInt)))
Note that this sequence, being infinite, will not terminate. If you
only want to print the first five random numbers, you can do something
like:
(let [rand (java.util.Random.)
nextInt #(.nextInt rand)]
(map print (take 5 (repeatedly nextInt))))
I hope this helps.
Sincerely,
Daniel Solano Gómez
signature.asc
Description: Digital signature
