Last active
July 7, 2022 07:53
-
-
Save Porges/750e25622a4432ad1d944ee3ebc0f3fc to your computer and use it in GitHub Desktop.
Revisions
-
Porges revised this gist
Jul 7, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -25,7 +25,7 @@ main = do where percent :: Double -> String percent result = printf "%0.4f%%" (result * 100) data D = Skull | Sword | D10 deriving (Eq, Ord, Show) -
Porges revised this gist
Jul 7, 2022 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,6 @@ module Main where import Data.Foldable (for_) import Control.Monad.Bayes.Class (MonadSample, uniformD) import Control.Monad.Bayes.Enumerator (enumerate) import Text.Printf (printf) -
Porges created this gist
Jul 7, 2022 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ {-# LANGUAGE RankNTypes, ConstraintKinds, BlockArguments #-} module Main where import Data.Foldable (for_) import Control.Monad (replicateM) 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 "%4.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