Skip to content

Instantly share code, notes, and snippets.

@Porges
Last active July 7, 2022 07:53
Show Gist options
  • Select an option

  • Save Porges/750e25622a4432ad1d944ee3ebc0f3fc to your computer and use it in GitHub Desktop.

Select an option

Save Porges/750e25622a4432ad1d944ee3ebc0f3fc to your computer and use it in GitHub Desktop.

Revisions

  1. Porges revised this gist Jul 7, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 2dozen.hs
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ main = do

    where
    percent :: Double -> String
    percent result = printf "%4.4f%%" (result * 100)
    percent result = printf "%0.4f%%" (result * 100)

    data D = Skull | Sword | D10
    deriving (Eq, Ord, Show)
  2. Porges revised this gist Jul 7, 2022. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion 2dozen.hs
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,6 @@
    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)
  3. Porges created this gist Jul 7, 2022.
    56 changes: 56 additions & 0 deletions 2dozen.hs
    Original 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