Skip to content

Instantly share code, notes, and snippets.

@jamiltron
Created June 27, 2012 21:30
Show Gist options
  • Save jamiltron/3006992 to your computer and use it in GitHub Desktop.
Save jamiltron/3006992 to your computer and use it in GitHub Desktop.
20 Intermediate Haskell Solutions
class Fluffy f where
furry :: (a -> b) -> f a -> f b
-- Exercise 1
-- Relative Difficulty: 1
instance Fluffy [] where
furry _ [] = []
furry f (x:xs) = (f x):(furry f xs)
-- Exercise 2
-- Relative Difficulty: 1
instance Fluffy Maybe where
furry _ Nothing = Nothing
furry f (Just x) = Just (f x)
-- Exercise 3
-- Relative Difficulty: 5
instance Fluffy ((->) t) where
furry = (.)
newtype EitherLeft b a = EitherLeft (Either a b) deriving (Show)
newtype EitherRight a b = EitherRight (Either a b) deriving (Show)
-- -- Exercise 4
-- -- Relative Difficulty: 5
instance Fluffy (EitherLeft t) where
furry _ (EitherLeft (Right x)) = EitherLeft (Right x)
furry f (EitherLeft (Left x)) = EitherLeft (Left (f x))
-- Exercise 5
-- Relative Difficulty: 5
instance Fluffy (EitherRight t) where
furry _ (EitherRight (Left x)) = EitherRight (Left x)
furry f (EitherRight (Right x)) = EitherRight (Right (f x))
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 xs = banana (unicorn . f) xs
-- -- Exercise 7
-- -- Relative Difficulty: 2
instance Misty [] where
banana f [x] = (f x)
unicorn x = [x]
-- Exercise 8
-- Relative Difficulty: 2
instance Misty Maybe where
banana _ Nothing = Nothing
banana f (Just x) = (f x)
unicorn x = Just x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment