Created
September 19, 2019 03:39
-
-
Save bedekelly/fe99f2d56e3ef9c8196ccdc9340eb372 to your computer and use it in GitHub Desktop.
Haskell Nondeterminism
This file contains 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
data CoinType = Fair | Biased deriving (Show) | |
data Coin = Head | Tail deriving (Eq,Show) | |
toss Fair = [Head, Tail] | |
toss Biased = [Head, Head] | |
pick = [Fair, Biased] | |
experiment = do | |
coin <- pick -- Pick a coin at random | |
result <- toss coin -- Toss it, to get a result | |
guard (result == Head) -- We only care about results that come up Heads | |
return coin -- Return which coin was used in this case | |
--- | |
>> experiment | |
[Biased, Biased, Fair] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credit to https://stackoverflow.com/a/20644753