Quick actions

cmd+k|ctrl+k

Navigation

Languages

DiceRoll

Snippet info

Language

Haskell

Visibility

public

Author

davidkaste

Created

2018-03-29T15:33:59Z

Updated

2018-04-04T08:02:25Z

module Main where

import Data.List.Split
import Data.Char
import System.Random
import System.Environment
import Control.Monad

type Rolls = Int
type Sides = Int
type Dice = (Rolls, Sides)

argsToDice :: String -> Dice
argsToDice args = (rolls, sides)
    where [rolls, sides] = map read $ splitOn "D" (map toUpper args)

rollDice :: RandomGen g => Dice -> g -> Int
rollDice (rolls, sides) = sum . take rolls . (randomRs (1, sides))

main :: IO ()
main = do
    args <- getArgs
    x <- liftM (rollDice (argsToDice (head args))) newStdGen
    putStrLn $ show x
INFO