Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
34
Task/Monty-Hall-problem/Haskell/monty-hall-problem-1.hs
Normal file
34
Task/Monty-Hall-problem/Haskell/monty-hall-problem-1.hs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import System.Random (StdGen, getStdGen, randomR)
|
||||
|
||||
trials :: Int
|
||||
trials = 10000
|
||||
|
||||
data Door = Car | Goat deriving Eq
|
||||
|
||||
play :: Bool -> StdGen -> (Door, StdGen)
|
||||
play switch g = (prize, new_g)
|
||||
where (n, new_g) = randomR (0, 2) g
|
||||
d1 = [Car, Goat, Goat] !! n
|
||||
prize = case switch of
|
||||
False -> d1
|
||||
True -> case d1 of
|
||||
Car -> Goat
|
||||
Goat -> Car
|
||||
|
||||
cars :: Int -> Bool -> StdGen -> (Int, StdGen)
|
||||
cars n switch g = f n (0, g)
|
||||
where f 0 (cs, g) = (cs, g)
|
||||
f n (cs, g) = f (n - 1) (cs + result, new_g)
|
||||
where result = case prize of Car -> 1; Goat -> 0
|
||||
(prize, new_g) = play switch g
|
||||
|
||||
main = do
|
||||
g <- getStdGen
|
||||
let (switch, g2) = cars trials True g
|
||||
(stay, _) = cars trials False g2
|
||||
putStrLn $ msg "switch" switch
|
||||
putStrLn $ msg "stay" stay
|
||||
where msg strat n = "The " ++ strat ++ " strategy succeeds " ++
|
||||
percent n ++ "% of the time."
|
||||
percent n = show $ round $
|
||||
100 * (fromIntegral n) / (fromIntegral trials)
|
||||
21
Task/Monty-Hall-problem/Haskell/monty-hall-problem-2.hs
Normal file
21
Task/Monty-Hall-problem/Haskell/monty-hall-problem-2.hs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import Control.Monad.State
|
||||
|
||||
play :: Bool -> State StdGen Door
|
||||
play switch = do
|
||||
i <- rand
|
||||
let d1 = [Car, Goat, Goat] !! i
|
||||
return $ case switch of
|
||||
False -> d1
|
||||
True -> case d1 of
|
||||
Car -> Goat
|
||||
Goat -> Car
|
||||
where rand = do
|
||||
g <- get
|
||||
let (v, new_g) = randomR (0, 2) g
|
||||
put new_g
|
||||
return v
|
||||
|
||||
cars :: Int -> Bool -> StdGen -> (Int, StdGen)
|
||||
cars n switch g = (numcars, new_g)
|
||||
where numcars = length $ filter (== Car) prize_list
|
||||
(prize_list, new_g) = runState (replicateM n (play switch)) g
|
||||
2
Task/Monty-Hall-problem/Haskell/monty-hall-problem-3.hs
Normal file
2
Task/Monty-Hall-problem/Haskell/monty-hall-problem-3.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
The switch strategy succeeds 67% of the time.
|
||||
The stay strategy succeeds 34% of the time.
|
||||
Loading…
Add table
Add a link
Reference in a new issue