SOE ex.7.4


data InternalTree a = ILeaf
                    | IBranch a (InternalTree a) (InternalTree a)
  deriving Show

treeZip :: InternalTree a -> InternalTree b -> InternalTree (a,b)
treeZip ILeaf _ = ILeaf
treeZip _ ILeaf = ILeaf
treeZip (IBranch a llt lrt) (IBranch b rlt rrt) =
  IBranch (a,b) (treeZip llt rlt) (treeZip lrt rrt)

treeZipWith :: (a -> b -> c) -> InternalTree a -> InternalTree b -> InternalTree c
treeZipWith f ILeaf _ = ILeaf
treeZipWith f _ ILeaf = ILeaf
treeZipWith f (IBranch a llt lrt) (IBranch b rlt rrt) =
  IBranch (f a b) (treeZipWith f llt rlt) (treeZipWith f lrt rrt)

{- we can define treeZip by using treeZipWith -}
treeZip' :: InternalTree a -> InternalTree b -> InternalTree (a,b)
treeZip' = treeZipWith (,)

Leave a Reply