Skip to content

Instantly share code, notes, and snippets.

@drocamor
Created April 9, 2010 01:44
Show Gist options
  • Save drocamor/360780 to your computer and use it in GitHub Desktop.
Save drocamor/360780 to your computer and use it in GitHub Desktop.
(defn pick-a-door [doors]
(nth doors (rand-int (count doors))))
(defn play-the-game [strategy]
(let [*doors* '(1 2 3)
*winning-door* (pick-a-door *doors*) ; Computer picks one door to be a winner
*chosen-door* (pick-a-door *doors*) ; player picks a door
*remaining-doors* (filter (fn [value]
(or (= value *winning-door*)
(= value *chosen-door*))) *doors*)] ; Of the remaining doors, remove one that is not good
(if (= strategy :switch)
(let [second-choice (pick-a-door *remaining-doors*)]
(if (= second-choice *winning-door*)
1.0
0.0))
(if (= strategy :stay)
(if (= *chosen-door* *winning-door*) ; Staying
1.0 ; winning :)
0.0))))) ; losing :(
(defn results [strategy]
(lazy-seq
(cons (play-the-game strategy)
(results strategy))))
(defn mean [coll]
(/ (reduce + coll) (count coll))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment