--
{-# Language MultiParamTypeClasses,
FlexibleInstance,
FlexibleContexts #-}
-- PUT YOUR NAME AND EMAIL HERE!!!
-- without your name and email, you will not get feedback!
module HW1 where
import Data.Char (chr,ord)
-- Question # 1
data Tree a = Tip a | Fork (Tree a) (Tree a)
instance Show a => Show (Tree a) where
show (Tip a) = "(Tip "++ show a ++ ")"
show (Fork x y) = "(Fork " ++ show x ++" "++ show y ++")"
-- add your instances here
-- Question #2
class Convertible a b where
into:: a -> b
outof:: b -> a
instance Convertible ([a],[b]) [(a,b)] where
into (x,y) = zip x y
outof xs = unzip xs
-- add your instances here
-- Question #3
data TagExp = I Int | C Char | F (TagExp -> TagExp) | D String [TagExp]
encodeL :: [Int] -> TagExp
encodeL [] = D "[]" []
encodeL (x:xs) = D ":" [I x,encodeL xs]
decodeL:: TagExp -> [Int]
decodeL (D "[]" []) = []
decodeL (D ":" [I x,xs]) = x : decodeL xs
decodeL other = error "Not a Tagged list"
-- Question 4
class Convertible a TagExp => Generic a where
toString:: a -> String
equal:: a -> a -> Bool
flatten:: a -> [Int]
unflatten:: [Int] -> (a,[Int])
instance (Generic a,Generic b) => Generic (a,b) where
flatten (a,b) = flatten a ++ flatten b
unflatten xs = ((x,y), zs)
where (x,ys) = unflatten xs
(y,zs) = unflatten ys
--