module E3 where
import Data.List
dialogue :: IO ()
dialogue = do
putStrLn "Hello! Who are you?"
name <- getLine
putStrLn $ "Nice to meet you, " ++ name ++ "!"
newtype UnOrdPair a = UOP (a,a)
instance Functor UnOrdPair where
fmap = undefined
instance Applicative UnOrdPair where
pure = undefined
(<*>) = undefined
type Guest = String
type Room = Maybe Guest
newtype Hotel = Hot [Room]
initialFullHotel :: Hotel
initialFullHotel = Hot [ Just $ "Guest" ++ show n | n <- [(1::Integer)..] ]
instance Show Hotel where
show (Hot rooms) = "Hot [" ++ substring ++ ", ... ]" where
substring = intercalate ", " $ map show (take 10 rooms)
accommodateSingle :: Hotel -> Guest -> Hotel
accommodateSingle (Hot h) newGuest = undefined
accommodateFiniteGroup :: Hotel -> [Guest] -> Hotel
accommodateFiniteGroup (Hot h) group = undefined
accommodateGroup :: Hotel -> [Guest] -> Hotel
accommodateGroup (Hot h) group = undefined
accommodateFinitelyManyGroups :: Hotel -> [[Guest]] -> Hotel
accommodateFinitelyManyGroups (Hot h) groups = undefined -- Hint: use a fold!
accommodateArbitraryGroups :: Hotel -> [[Guest]] -> Hotel
accommodateArbitraryGroups (Hot h) groups = undefined
data Tree a = Leaf a | Branch (Tree a) (Tree a)
deriving (Eq,Ord,Show)
instance Functor Tree where
-- fmap :: (a -> b) -> Tree a -> Tree b
fmap f (Leaf x) = Leaf (f x)
fmap f (Branch left right) = Branch (fmap f left)
(fmap f right)
instance Applicative Tree where
-- pure :: a -> Tree a
pure = Leaf
-- (<*>) :: Tree (a -> b) -> Tree a -> Tree b
(<*>) ftree (Leaf x) = fmap ($ x) ftree
(<*>) ftree (Branch xl xr) = Branch (ftree <*> xl)
(ftree <*> xr)
instance Foldable Tree where
-- foldr :: (a -> b -> b) -> b -> Tree a -> b
foldr f y (Leaf x) = f x y
foldr f y (Branch l r) = foldr f (foldr f y l) r
instance Traversable Tree where
-- traverse :: Applicative f => (a -> f b) -> t a -> f (t b)
traverse g (Leaf x) = Leaf <$> g x
traverse g (Branch l r) = Branch <$> traverse g l <*> traverse g r