Created
February 2, 2012 18:14
-
-
Save k0001/1724929 to your computer and use it in GitHub Desktop.
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
module Tony20 ( | |
Fluffy, | |
furry, | |
EitherLeft(..), | |
EitherRight(..), | |
Misty, | |
banana, | |
unicorn, | |
jellybean, | |
apple | |
) where | |
class Fluffy f where -- Functor | |
furry :: (a -> b) -> f a -> f b -- fmap | |
-- Exercise 1 | |
-- Relative Difficulty: 1 | |
instance Fluffy [] where | |
furry = map | |
-- Exercise 2 | |
-- Relative Difficulty: 1 | |
instance Fluffy Maybe where | |
furry = fmap | |
-- Exercise 3 | |
-- Relative Difficulty: 5 | |
instance Fluffy ((->) t) where | |
furry = (.) | |
newtype EitherLeft b a = EitherLeft (Either a b) | |
newtype EitherRight a b = EitherRight (Either a b) | |
-- Exercise 4 | |
-- Relative Difficulty: 5 | |
instance Fluffy (EitherLeft t) where | |
furry f (EitherLeft (Left a)) = EitherLeft (Left (f a)) | |
furry _ (EitherLeft (Right b)) = EitherLeft (Right b) | |
-- Exercise 5 | |
-- Relative Difficulty: 5 | |
instance Fluffy (EitherRight t) where | |
furry f (EitherRight (Right b)) = EitherRight (Right (f b)) | |
furry _ (EitherRight (Left a)) = EitherRight (Left a) | |
class Misty m where -- Monad | |
banana :: (a -> m b) -> m a -> m b -- (=<<) | |
unicorn :: a -> m a -- return | |
-- Exercise 6 | |
-- Relative Difficulty: 3 | |
-- (use banana and/or unicorn) | |
furry' :: (a -> b) -> m a -> m b | |
furry' f = banana (unicorn . f) | |
-- Exercise 7 | |
-- Relative Difficulty: 2 | |
instance Misty [] where | |
banana f = concat . furry f | |
unicorn x = [x] | |
-- Exercise 8 | |
-- Relative Difficulty: 2 | |
instance Misty Maybe where | |
banana _ Nothing = Nothing | |
banana f (Just x) = f x | |
unicorn = Just | |
-- Exercise 9 | |
-- Relative Difficulty: 6 | |
instance Misty ((->) t) where | |
banana f m = \x -> f (m x) x | |
unicorn x = \_ -> x | |
-- Exercise 10 | |
-- Relative Difficulty: 6 | |
instance Misty (EitherLeft t) where | |
banana f (EitherLeft (Left a)) = f a | |
banana _ (EitherLeft (Right b)) = EitherLeft (Right b) | |
unicorn x = EitherLeft (Left x) | |
-- Exercise 11 | |
-- Relative Difficulty: 6 | |
instance Misty (EitherRight t) where | |
banana _ (EitherRight (Left a)) = EitherRight (Left a) | |
banana f (EitherRight (Right b)) = f b | |
unicorn x = EitherRight (Right x) | |
-- Exercise 12 | |
-- Relative Difficulty: 3 | |
jellybean :: (Misty m) => m (m a) -> m a -- join | |
jellybean = banana id | |
-- Exercise 13 | |
-- Relative Difficulty: 6 | |
apple :: (Misty m) => m a -> m (a -> b) -> m b | |
apple = banana . flip furry' | |
-- Exercise 14 | |
-- Relative Difficulty: 6 | |
moppy :: (Misty m) => [a] -> (a -> m b) -> m [b] | |
moppy = error "todo" | |
-- Exercise 15 | |
-- Relative Difficulty: 6 | |
-- (bonus: use moppy) | |
sausage :: (Misty m) => [m a] -> m [a] | |
sausage = error "todo" | |
-- Exercise 16 | |
-- Relative Difficulty: 6 | |
-- (bonus: use apple + furry') | |
banana2 :: (Misty m) => (a -> b -> c) -> m a -> m b -> m c | |
banana2 = error "todo" | |
-- Exercise 17 | |
-- Relative Difficulty: 6 | |
-- (bonus: use apple + banana2) | |
banana3 :: (Misty m) => (a -> b -> c -> d) -> m a -> m b -> m c -> m d | |
banana3 = error "todo" | |
-- Exercise 18 | |
-- Relative Difficulty: 6 | |
-- (bonus: use apple + banana3) | |
banana4 :: (Misty m) => (a -> b -> c -> d -> e) -> m a -> m b -> m c -> m d -> m e | |
banana4 = error "todo" | |
newtype State s a = State { | |
state :: (s -> (s, a)) | |
} | |
-- Exercise 19 | |
-- Relative Difficulty: 9 | |
instance Fluffy (State s) where | |
furry = error "todo" | |
-- Exercise 20 | |
-- Relative Difficulty: 10 | |
instance Misty (State s) where | |
banana = error "todo" | |
unicorn = error "todo" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment