Created
June 9, 2013 05:44
-
-
Save kagamilove0707/5737785 to your computer and use it in GitHub Desktop.
Pairモナドですー>ω< どう役立てるかはあなた次第(`・ω・´)
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
import Data.Pair | |
main = print $ (+) <:> (*) <*> pure 2 <*> pair 3 4 -- Pair (5,8) |
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
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
module Data.Pair (Pair, pair, (<:>)) where | |
import Data.Monoid | |
import Control.Applicative | |
newtype Pair a = Pair (a, a) deriving (Show, Eq, Monoid) | |
infixl 5 <:> | |
pair, (<:>) :: a -> a -> Pair a | |
pair x y = Pair (x, y) | |
(<:>) = pair | |
instance Functor Pair where | |
fmap f (Pair (x, y)) = Pair (f x,f y) | |
instance Applicative Pair where | |
pure x = Pair (x, x) | |
(Pair (f, g)) <*> (Pair (x, y)) = Pair (f x, g y) | |
instance Monad Pair where | |
return = pure | |
(Pair (x, y)) >>= f = case (f x, f y) of | |
(Pair (x', _), Pair (_, y')) -> Pair (x', y') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment