On Thursday, May 19, 2011 4:38:17 PM UTC-4, Ken Wesson wrote:
>
> On Thu, May 19, 2011 at 12:52 PM, siyu798 <siy...@gmail.com> wrote:
> > Hi, I started learning clojure for a few months and this is what I have 
> for
> > the problem, and I find it running very slow if exceeding 100k trials, 
> maybe
> > it's because of using set?  Any feedbacks will be appreciated. thx
> > (require '[clojure.set :as set])
> > (def doors #{:a :b :c})
> > (defn rand-nth-set [s]
> >  (conj #{} (rand-nth (seq s))))
>
> #{(rand-nth (seq s))} should work as well.
>

Actually that's what I had but changed to the current form because of 
personal preference.

> > (defn play
> >   ([] (play nil))
> >   ([switch?]
> >    (let [prize-door  (rand-nth-set doors)
> >          picked-door (rand-nth-set doors)
> >          empty-doors (set/difference doors prize-door)
> >          opened-door (rand-nth-set (set/difference empty-doors 
> picked-door))
> >          picked-door (if switch?
> >                        (set/difference doors opened-door picked-door)
>
> Shouldn't that be wrapped in (first ...) or something?
>
 do you mean wrap the returned picked-door set in (first ...)?  Since this 
is a three doors scenario so there should always be one door left to switch 
to, thus no need to use first.  

For some reasons I always have the impression that it's not idiomatic to use 
chained let form like the play fn here, is there a more idiomatic way to 
write this code?

> >                        picked-door)]
> >      (= picked-door prize-door))))
> > (count (remove #(false? %) (repeatedly 10000 #(play true))))
> > (count (remove #(false? %) (repeatedly 10000 #(play false))))
>
> As for the speed, I'm not sure what the problem is.
>
> -- 
> Protege: What is this seething mass of parentheses?!
> Master: Your father's Lisp REPL. This is the language of a true
> hacker. Not as clumsy or random as C++; a language for a more
> civilized age.
>
>

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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