On Thu, May 19, 2011 at 12:52 PM, siyu798 <siyu...@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.

> (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?

>                        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