[ create a new paste ] login | about

Link: http://codepad.org/GWPmOzf8    [ raw code | output | fork ]

azumih - Haskell, pasted on Mar 7:
#! /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


Output:
1
2
"switch: 671"
"stay:   346"


Create a new paste based on this one


Comments: