Skip to content

Instantly share code, notes, and snippets.

@kagamilove0707
Created June 9, 2013 05:44
Show Gist options
  • Save kagamilove0707/5737785 to your computer and use it in GitHub Desktop.
Save kagamilove0707/5737785 to your computer and use it in GitHub Desktop.
Pairモナドですー>ω< どう役立てるかはあなた次第(`・ω・´)
import Data.Pair
main = print $ (+) <:> (*) <*> pure 2 <*> pair 3 4 -- Pair (5,8)
{-# 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