【发布时间】:2014-09-02 05:37:32
【问题描述】:
我正在编写一个程序,它应该能够模拟许多尝试使用轮盘赌的鞅投注系统的实例。我希望main 接受一个参数,给出要执行的测试次数,多次执行测试,然后打印获胜次数除以测试总数。我的问题是,我没有得到一个 Bool 列表,我可以过滤它来计算成功,我有一个 IO Bool 列表,我不明白如何过滤它。
这里是源代码:
-- file: Martingale.hs
-- a program to simulate the martingale doubling system
import System.Random (randomR, newStdGen, StdGen)
import System.Environment (getArgs)
red = [1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36]
martingale :: IO StdGen -> IO Bool
martingale ioGen = do
gen <- ioGen
return $ martingale' 1 0 gen
martingale' :: Real a => a -> a -> StdGen -> Bool
martingale' bet acc gen
| acc >= 5 = True
| acc <= -100 = False
| otherwise = do
let (randNumber, newGen) = randomR (0,37) gen :: (Int, StdGen)
if randNumber `elem` red
then martingale' 1 (acc + bet) newGen
else martingale' (bet * 2) (acc - bet) newGen
main :: IO ()
main = do
args <- getArgs
let iters = read $ head args
gens = replicate iters newStdGen
results = map martingale gens
--results = map (<-) results
print "THIS IS A STUB"
就像我在我的 cmets 中一样,我基本上想将 (<-) 映射到我的 IO Bool 列表上,但据我了解,(<-) 实际上不是一个函数,而是一个关键字。任何帮助将不胜感激。
【问题讨论】:
-
你想要
sequence:: (Monad m) => [m a] -> m [a]