data Tree a = Nil | Branch (Tree a) a (Tree a) tInsert value Nil = Branch Nil value Nil tInsert value (Branch lt v rt) = if value > v then Branch lt v (tInsert value rt) else Branch (tInsert value lt) v rt tFind value Nil = False tFind value (Branch lt v rt) = if value == v then True else if value < v then tFind value lt else tFind value rt treeToList Nil = [] treeToList (Branch lt v rt) = treeToList lt++[v]++treeToList rt data I7 = I7 Int instance Num I7 where I7 a + I7 b = I7 $(a + b) `mod` 7 I7 a - I7 b = I7 $(a - b) `mod` 7 I7 a * I7 b = I7 $(a * b) `mod` 7 abs = id signum (I7 0) = 0 signum _ = 1 fromInteger a = I7 $ fromInteger a `mod` 7 instance Eq I7 where I7 a == I7 b = a == b instance Ord I7 where compare (I7 a) (I7 b) = compare a b instance Show I7 where show (I7 a) = show a ++ "_7" instance Show a => Show (Tree a) where show Nil = "." show (Branch lt v rt) = "[" ++ show lt ++ " " ++ show v ++ " " ++ show rt ++ "]" instance Eq a => Eq (Tree a) where (Branch lt1 v1 rt1) == (Branch lt2 v2 rt2) = (lt1 == lt2) && (v1 == v2) && (rt1 == rt2) Nil == Nil = True _ == _ = False instance Functor Tree where fmap f Nil = Nil fmap f (Branch lt v rt) = Branch (fmap f lt) (f v) (fmap f rt) instance Foldable Tree where foldr f a Nil = a foldr f a (Branch lt v rt) = let b = foldr f a rt c = f v b in foldr f c lt import Data.Foldable treeToList' = toList