私はState
と再帰を一緒に使用するいくつかのコードを作成しています。おそらくこの部分は実際には必要ではありません(それは相対的に初心者であっても、すでに私にとっては不器用な感じです)。しかし、おそらくそうする部分があります。MonadRandom、State and Monadトランスフォーマー
私の初期の純粋な実装は完全です決定論的(入札の選択は、単に機能validBids
が提供する最初のオプションです):
bidOnRound :: (DealerRules d) => d -> NumCards -> State ([Player], PlayerBids)()
bidOnRound dealerRules cardsThisRound = do
(players, bidsSoFar) <- get
unless (List.null players) $ do
let options = validBids dealerRules cardsThisRound bidsSoFar
let newBid = List.head $ Set.toList options
let p : ps = players
put (ps, bidsSoFar ++ [(p, newBid)])
bidOnRound dealerRules cardsThisRound
そして、私からそれを呼び出す:今、私は私がにランダム性を持参する必要があることを承知している
playGame :: (DealerRules d, ScorerRules s) => d -> s -> StateT Results IO()
...
let (_, bidResults) = execState (bidOnRound dealerRules cardsThisRound) (NonEmpty.toList players, [])
これと、コード。 IO
どこにでもゴミを捨てることも、手作業で丸ごとランダムに種まちを渡すことも望ましくありません。MonadRandomまたは何かを使用する必要があります。私が使っている図書館はそれを有効に使う。これは賢明な選択ですか?
ここに私が試したものです:
bidOnRound :: (DealerRules d, RandomGen g) => d -> NumCards -> RandT g (State ([Player], PlayerBids))()
bidOnRound dealerRules cardsThisRound = do
(players, bidsSoFar) <- get
unless (List.null players) $ do
let options = Set.toList $ validBids dealerRules cardsThisRound bidsSoFar
rnd <- getRandomR (0 :: Int, len options - 1)
let newBid = options List.!! rnd
let p : ps = players
put (ps, bidsSoFar ++ [(p, newBid)])
bidOnRound dealerRules cardsThisRound
が、私はすでに不快だ、プラス例えば、これを呼び出す方法を考え出すことはできませんexecState
と組み合わせてevalRand
を使用してなど、私は他の人対MonadRandom
、RandGen
とmtl
で読むより、少ない必ず私がやっているのだ...私はきれいにランダム性を兼ね備えなければならない方法
とState
とどのように行います私はこれらを正しく呼んでいますか?
ありがとうございます!
EDIT:参考のためfull current source on Github。
これは素晴らしいです、ありがとう。私は今夜それを正しく処理します。私は現在のGithubのソースに実際にリンクしていたはずです...今すぐやります。 – declension