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 {- 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)