Skip to content

Instantly share code, notes, and snippets.

@WillNess
Created February 23, 2013 13:02
Show Gist options
  • Save WillNess/5019652 to your computer and use it in GitHub Desktop.
Save WillNess/5019652 to your computer and use it in GitHub Desktop.
How do you define a function of signature h :: M Int -> M Int -> M Int so that h (M x) (M y) = M (x+y) without unwrapping the monad? http://stackoverflow.com/questions/15035468/how-do-you-define-a-function-of-signature-h-m-int-m-int-m-int-so-that-h
g x y = (return . (+x)) =<< y
= (=<<) (return . (+x)) y
= ((=<<) . (return .)) ((+) x) y
= (=<<) . (return .) . (+) $ x y
h mx my = mx >>= (\x ->
my >>= (\y ->
return (x+y) ))
h mx my = do x <- mx
y <- my
return (x+y)
h mx my = liftM2 (+) mx my
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment