-- Mon Feb 13 08:00:32 CST 2017
data Gate = Ajar | Shut
-- deriving (Show) Gives us the default definition of show(.)
deriving (Eq, Read, Ord)
g1 = Ajar
g2 = Shut
checkOpen :: Gate -> Bool
checkOpen Ajar = True
checkOpen _ = False
prettyPrint :: Gate -> String
prettyPrint Ajar = "The gate is open"
prettyPrint Shut = "The gate is closed"
shortPrettyPrint Ajar = "open"
shortPrettyPrint Shut = "closed"
instance Show Gate where
show g = shortPrettyPrint g
flipGate :: Gate -> Gate
flipGate Ajar = Shut
flipGate _ = Ajar
class Flippable a where
flp :: a -> a
instance Flippable Gate where
flp g = flipGate g
instance Flippable Bool where
flp b = not b
data Color = Red | Orange | Yellow | Green | Blue | Indigo | Violet
deriving (Show, Read, Eq, Ord, Enum, Bounded)
c1 = Yellow
c2 = Indigo
rainbow = [(minBound :: Color) .. (maxBound :: Color)]
waterGate n = wGHelper 1 n ls
where ls = [Shut | _<-[1..n]]
wGHelper i n gs | i == n = (flipList bList gs)
| otherwise = wGHelper (i+1) n (flipList bList gs)
where bList = [ j `mod` i == 0 | j <- [1..n]]
flipList :: Flippable a => [Bool] -> [a] -> [a]
flipList _ [] = []
flipList (b:bs) (g:gs) | b = (flp g):flipList bs gs
| otherwise = g:flipList bs gs
---------------------------------------
data OwnList a = Nil | Cons a (OwnList a)
-- deriving (Show)
ol = Cons 10 (Cons 2 (Cons 6 Nil))
instance (Show a) => Show (OwnList a) where
show Nil = "<>"
show (Cons x xs)= (show x)++":"++(show xs)
sumOwnList :: OwnList Integer -> Integer
sumOwnList Nil = 0
sumOwnList (Cons x xs) = x + (sumOwnList xs)
data DList a b = DNil | DConsA a (DList a b) | DConsB b (DList a b)
instance (Show a, Show b) => Show (DList a b) where
show DNil = "<>"
show (DConsA x xs)= (show x)++":"++(show xs)
show (DConsB x xs)= (show x)++":"++(show xs)
dl = DConsA 'a' (DConsA 'x' (DConsB True (DConsA 'c' (DConsB False DNil))))
{-
data Expr = Val Int
| Add Expr Expr
| Mul Expr Expr
-}