Skip to content

Instantly share code, notes, and snippets.

@fronx
Last active August 29, 2015 14:06
Show Gist options
  • Save fronx/a83cf1136d5bb5fc2d90 to your computer and use it in GitHub Desktop.
Save fronx/a83cf1136d5bb5fc2d90 to your computer and use it in GitHub Desktop.
Pair has two functors
module PairFunctors where
data Fst b a = Fst (a, b)
deriving Show
data Snd a b = Snd (a, b)
deriving Show
instance Functor (Fst b) where
fmap f (Fst (x, y)) = Fst (f x, y)
instance Functor (Snd a) where
fmap f (Snd (x, y)) = Snd (x, f y)
main = do
let pair = (3, 4)
print $ fmap (*2) $ Fst pair -- Fst (6, 4)
print $ fmap (*2) $ Snd pair -- Fst (3, 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment