Skip to content

Instantly share code, notes, and snippets.

@bedekelly
Created September 19, 2019 03:39
Show Gist options
  • Save bedekelly/fe99f2d56e3ef9c8196ccdc9340eb372 to your computer and use it in GitHub Desktop.
Save bedekelly/fe99f2d56e3ef9c8196ccdc9340eb372 to your computer and use it in GitHub Desktop.
Haskell Nondeterminism
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]
@bedekelly
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment