Created
February 14, 2010 13:16
-
-
Save rares/304013 to your computer and use it in GitHub Desktop.
Monad
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
type Choice a = [a] | |
choose :: [a] -> Choice a | |
choose xs = xs | |
-- Join | |
join :: Choice (Choice a) -> Choice a | |
join choices = concat choices | |
-- Bind | |
(>>=) :: Choice a -> (a -> Choice b) -> Choice b | |
choices >>= f = join (map f choices) | |
-- Bind (ignore original) | |
(>>) :: Choice a -> Choice b -> Choice b | |
choices >> f = choices >>= \_ -> f | |
-- Return | |
return :: a -> Choice a | |
return x = choose [x] | |
-- Guard | |
mzero :: Choice a | |
mzero = choose [] | |
guard :: Bool -> Choice () | |
guard True = return () | |
guard False = mzero | |
test' :: Choice (Int, Int, Int) | |
test' = do | |
x <- choose [1,2,3] | |
y <- choose [4,5,6] | |
z <- choose [7,8,9] | |
guard ((x + y) * z == 45) | |
return (x, y, z) | |
main = do | |
print(take 1 test') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment