```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 ``` ```#! /usr/bin/env runghc -- simplistic Monty Hall simulator import System.Random import Control.Monad montyOpensGoatDoor car sel = do idx <- randomRIO (0, (length cand) - 1) return \$ cand !! idx where cand = [x | x <- [0, 1, 2], x /= car, x /= sel] switch ini open = do return \$ [x | x <- [0, 1, 2], x /= ini, x /= open] !! 0 montyHall sw = do car <- randomRIO (0::Int, 2::Int) iniSel <- randomRIO (0::Int, 2::Int) opened <- montyOpensGoatDoor car iniSel if sw then do finSel <- switch iniSel opened return \$ if finSel == car then 1 else 0 else return \$ if iniSel == car then 1 else 0 addm a b = do x <- a y <- b return (x + y) main = do sw <- foldr addm (return 0) \$ take 1024 . repeat \$ montyHall True st <- foldr addm (return 0) \$ take 1024 . repeat \$ montyHall False print \$ "switch: " ++ show sw print \$ "stay: " ++ show st ```
 ```1 2 ``` ```"switch: 671" "stay: 346" ```