class Fluffy f where furry :: (a -> b) -> f a -> f b -- Exercise 1 -- Relative Difficulty: 1 instance Fluffy [] where --furry :: (a -> b) -> [a] -> [b] furry _ [] = [] furry f (x:xs) = f x : furry f xs -- Exercise 2 -- Relative Difficulty: 1 instance Fluffy Maybe where --furry :: (a -> b) -> Maybe a -> Maybe b furry _ Nothing = Nothing furry f (Just a) = Just $ f a -- Exercise 3 -- Relative Difficulty: 5 instance Fluffy ((->) t) where --furry :: (a -> b) -> (t -> a) -> (t -> b) 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 :: (a -> b) -> EitherLeft t a -> EitherLeft t b furry f (EitherLeft (Left a)) = EitherLeft $ Left $ f a furry _ (EitherLeft (Right t)) = EitherLeft $ Right t -- Exercise 5 -- Relative Difficulty: 5 instance Fluffy (EitherRight t) where --furry :: (a -> b) -> EitherRight t a -> EitherRight t b furry f (EitherRight (Right a)) = EitherRight $ Right $ f a furry _ (EitherRight (Left a)) = EitherRight $ Left a class Misty m where banana :: (a -> m b) -> m a -> m b unicorn :: a -> m a -- Exercise 6 -- Relative Difficulty: 3 -- (use banana and/or unicorn) furry' :: (a -> b) -> m a -> m b furry' f ma = banana (unicorn . f) ma -- Exercise 7 -- Relative Difficulty: 2 instance Misty [] where --banana :: (a -> [b]) -> [a] -> [b] banana f = concat . map f --unicorn :: a -> [a] unicorn = (:[]) -- Exercise 8 -- Relative Difficulty: 2 instance Misty Maybe where --banana :: (a -> Maybe b) -> Maybe a -> Maybe b banana _ Nothing = Nothing banana f (Just a) = f a --unicorn :: a -> Maybe a unicorn = Just -- Exercise 9 -- Relative Difficulty: 6 instance Misty ((->) t) where --banana :: (a -> t -> b) -> (t -> a) -> (t -> b) banana atb ta = \t -> atb (ta t) t --unicorn :: a -> (t -> a) unicorn a = \t -> a -- Exercise 10 -- Relative Difficulty: 6 instance Misty (EitherLeft t) where --banana :: (a -> EitherLeft b) -> EitherLeft t a -> EitherLeft t b banana f (EitherLeft (Left a)) = f a banana _ (EitherLeft (Right b)) = EitherLeft $ Right b --unicorn :: a -> EitherLeft t a unicorn = EitherLeft . Left -- Exercise 11 -- Relative Difficulty: 6 instance Misty (EitherRight t) where --banana :: (a -> EitherRight t b) -> EitherRight t a -> EitherRight t b banana f (EitherRight (Right a)) = f a banana f (EitherRight (Left b)) = EitherRight $ Left b --unicorn :: a -> EitherRight t a unicorn = EitherRight . Right -- Exercise 12 -- Relative Difficulty: 3 jellybean :: (Misty m) => m (m a) -> m a jellybean = banana id -- Exercise 13 -- Relative Difficulty: 6 apple :: (Misty m) => m a -> m (a -> b) -> m b apple ma mab = banana (\a -> banana (\ab -> unicorn (ab a)) mab) ma -- Exercise 14 -- Relative Difficulty: 6 moppy :: (Misty m) => [a] -> (a -> m b) -> m [b] moppy [] _ = unicorn [] moppy (a:as) f = banana (\b -> banana (\bs -> unicorn (b:bs)) (moppy as f)) (f a) -- Exercise 15 -- Relative Difficulty: 6 -- (bonus: use moppy) sausage :: (Misty m) => [m a] -> m [a] sausage mas = moppy mas id -- Exercise 16 -- Relative Difficulty: 6 -- (bonus: use apple + furry') banana2 :: (Misty m) => (a -> b -> c) -> m a -> m b -> m c banana2 f ma mb = apple mb $ furry' f ma -- 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 f ma mb mc = apple mc $ banana2 f ma mb -- 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 f ma mb mc md = apple md $ banana3 f ma mb mc newtype State s a = State { state :: (s -> (s, a)) } -- Exercise 19 -- Relative Difficulty: 9 instance Fluffy (State s) where --furry :: (a -> b) -> State s a -> State s b furry f (State ssa) = State $ \s -> let (s', a) = ssa s in (s', f a) -- Exercise 20 -- Relative Difficulty: 10 instance Misty (State s) where --banana :: (a -> State s b) -> State s a -> State s b banana f (State ssa) = State $ \s -> let (s', a) = ssa s (State ssb) = f a in ssb s' --unicorn :: a -> State s a unicorn a = State $ \s -> (s, a)
I solve this exercise to remember Haskell once again after not using it for a while. I notice as I solve it more and more my style of thought is beginning to change.
This post first appeared on Me In Words | A Compendium Of Plagiarized Ideas, please read the originial post: here