Skip to content

Instantly share code, notes, and snippets.

@rares
Created February 14, 2010 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rares/304013 to your computer and use it in GitHub Desktop.
Save rares/304013 to your computer and use it in GitHub Desktop.
Monad
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