module TypeClassExample where import Chap3 member x [] = False member x (y:ys) = x == y || member x ys subset xs ys = all (\ x -> member x ys) xs instance Eq a => Eq (Tree a) where Empty == Empty = True Branch l1 n1 r1 == Branch l2 n2 r2 = l1 == l2 && n1 == n2 && r1 == r2 _ == _ = False type Eq' a = (a -> a -> Bool, a -> a -> Bool) eq' :: Eq' a -> (a -> a -> Bool) eq' = \ (e, _) -> e ne' :: Eq' a -> (a -> a -> Bool) ne' = \ (_, n) -> n member' :: Eq' a -> a -> [a] -> Bool member' d x [] = False member' d x (y:ys) = eq' d x y || member' d x ys subset' :: Eq' a -> [a] -> [a] -> Bool subset' d xs ys = all (\ x -> member' d x ys) xs {- eqDirectionDic :: Eq' Direction eqDirectionDic = (eqDirection, \ a b -> not (eqDirection a b)) where Up `eqDirection` Up = True Down `eqDirection` Down = True Left `eqDirection` Left = True Right `eqDirection` Right = True _ `eqDirection` _ = False eqTreeDic :: Eq' a -> Eq' (Tree a) eqTreeDic (eqA, _) = (eqTree, \ a b -> not (eqTree a b)) where Empty `eqTree` Empty = True Branch l1 n1 r1 `eqTree` Branch l2 n2 r2 = l1 `eqTree` l2 && n1 `eqA` n2 && r1 `eqTree` r2 _ `eqTree` _ = False -} {- Prelude‚É’č‹`ŤĎ‚Ý data Maybe a = Just a | Nothing -} myLookup :: Eq a => a -> [(a, b)] -> Maybe b myLookup x ((n,v):rest) = if n==x then Just v else myLookup x rest myLookup x [] = Nothing fix :: (a -> a) -> a fix f = f (fix f) genericLength :: Num a => [b] -> a genericLength [] = 0 genericLength (_:xs) = 1 + genericLength xs