Slide 18
Slide 18 text
module MontyHall where newGame :: MonadRandom m => m Game
newGame = shuffleM [Car, Goat, Goat]
import System.Random.Shuffle newGames :: MonadRandom m => m [Game]
import Control.Monad.Random.Class newGames = replicateM 100 newGame
import Control.Monad
pickDoor :: Game -> Game
(|>) = flip ($) pickDoor = id
data Door = Goat | Car deriving (Eq, Show) revealGoat :: Game -> Game
type Game = [Door] revealGoat [choice, Goat, x] = [choice, x]
type Strategy = Game -> Door revealGoat [choice, x, Goat] = [choice, x]
play :: Strategy -> Game -> Door stay, switch :: Strategy
play strategy game = stay [firstChoice, alternative] = firstChoice
game switch [firstChoice, alternative] = alternative
|> pickDoor
|> revealGoat main :: IO()
|> strategy main = do
(stayCnt, switchCnt) <- do
playAll :: Strategy -> [Game] -> Int gs <- newGames
playAll strategy games = return (playAll stay gs, playAll switch gs)
map (play strategy) games print ("Stay: " ++ show stayCnt)
|> filter (==Car) print ("Switch: " ++ show switchCnt)
|> length