Last active
July 7, 2022 07:53
-
-
Save Porges/750e25622a4432ad1d944ee3ebc0f3fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE RankNTypes, ConstraintKinds, BlockArguments #-} | |
module Main where | |
import Data.Foldable (for_) | |
import Control.Monad.Bayes.Class (MonadSample, uniformD) | |
import Control.Monad.Bayes.Enumerator (enumerate) | |
import Text.Printf (printf) | |
-- helpers | |
type Random t = forall m. MonadSample m => m t | |
die :: Int -> Random Int | |
die n = uniformD [1..n] | |
-- main part | |
main :: IO () | |
main = do | |
let outcomes = enumerate rollTwo | |
for_ outcomes \(outcome, r) -> do | |
putStrLn (show outcome ++ " " ++ percent r) | |
where | |
percent :: Double -> String | |
percent result = printf "%0.4f%%" (result * 100) | |
data D = Skull | Sword | D10 | |
deriving (Eq, Ord, Show) | |
specialD :: Random D | |
specialD = do | |
roll <- die 12 | |
pure case roll of | |
11 -> Skull | |
12 -> Sword | |
d -> D10 | |
data Outcome = TwoSkull | OneSkull | D100 | OneSword | TwoSword | |
deriving (Eq, Ord, Show) | |
rollTwo :: Random Outcome | |
rollTwo = do | |
d1 <- specialD | |
d2 <- specialD | |
pure case (d1, d2) of | |
(Sword, Sword) -> TwoSword | |
(Sword, _) -> OneSword | |
(_, Sword) -> OneSword | |
(Skull, Skull) -> TwoSkull | |
(Skull, _) -> OneSkull | |
(_, Skull) -> OneSkull | |
(D10, D10) -> D100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment