LoginSignup
1
0

More than 5 years have passed since last update.

サイコロを振るモナド

Last updated at Posted at 2012-03-16

モナドトランスフォーマーの練習

Dice.hs
{-# OPTIONS -Wall #-}
import Control.Monad
import Control.Monad.Random
import Data.List

sumDice :: (MonadRandom m) => Int -> m Int
sumDice n = liftM sum $ replicateM n $ getRandomR (1,6)

statDice :: (MonadRandom m) => Int -> m [(Int,Int)]
statDice n = do
  liftM histogram $ replicateM n $ sumDice 2
  where
    histogram :: (Ord x) => [x] -> [(x,Int)]
    histogram = map (\xs -> (head xs, length xs)) . group . sort

pureStat :: [(Int, Int)]  
pureStat = evalRand (statDice 360000) (mkStdGen 890135)

main :: IO ()
main = do
  print pureStat
  statDice 360000 >>= print
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0