Commit 36bc8d65 by Hans-Peter Deifel 🐢

### wta: Implement Probability in terms of Data.Scientific

```This already does all we need, so instead of duplicating the whole
implementation, just use it.```
parent e7905578
 ... ... @@ -302,4 +302,5 @@ executable random-wta , text , random >= 1.1 && <1.2 , mtl >= 2.2 && <2.3 , megaparsec >= 7 , megaparsec >= 7 && <8 , scientific >= 0.3 && <0.4
 {-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} module Probability (Probability(..), readProbability, decide) where import System.Random import Text.Printf import Data.Scientific data Probability = Probability Integer Int newtype Probability = Probability Scientific deriving newtype (Show) readProbability :: String -> Either String Probability readProbability input = case input of "0" -> Right (Probability 0 0) readProbability input = Probability <\$> case input of "0" -> Right (scientific 0 0) ('0':'.':rest) -> case reads rest of [(digits, "")] -> Right (Probability digits (length rest)) [(digits, "")] -> Right (scientific digits (negate (length rest))) _ -> failure "1" -> Right (Probability 1 0) "1" -> Right (scientific 1 0) ('1':'.':rest) | all (=='0') rest -> Right (Probability 1 0) | all (=='0') rest -> Right (scientific 1 0) | otherwise -> failure _ -> failure where failure = Left "Could not parse probability" instance Show Probability where show (Probability digits 0) = show digits show (Probability digits exp) = "0." ++ printf "%0*d" exp digits decide :: Probability -> IO Bool decide (Probability digits exp) = do decide (Probability science) = do let digits = coefficient science let exp = negate (base10Exponent science) randomNumber <- randomRIO (0, (10^exp)-1) return \$ randomNumber < digits
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!