data InternalTree a = ILeaf
| IBranch a (InternalTree a) (InternalTree a)
deriving Show
takeTree :: Int -> InternalTree a -> InternalTree a
takeTree 0 t = ILeaf
takeTree n ILeaf = ILeaf
takeTree n (IBranch a x y) = IBranch a (takeTree (n-1) x) (takeTree (n-1) y)
takeTreeWhile :: (a -> Bool) -> InternalTree a -> InternalTree a
takeTreeWhile f ILeaf = ILeaf
takeTreeWhile f (IBranch a x y) = if (f a)
then IBranch a (takeTreeWhile f x)
(takeTreeWhile f y)
else ILeaf |

data InternalTree a = ILeaf
| IBranch a (InternalTree a) (InternalTree a)
deriving Show
takeTree :: Int -> InternalTree a -> InternalTree a
takeTree 0 t = ILeaf
takeTree n ILeaf = ILeaf
takeTree n (IBranch a x y) = IBranch a (takeTree (n-1) x) (takeTree (n-1) y)
takeTreeWhile :: (a -> Bool) -> InternalTree a -> InternalTree a
takeTreeWhile f ILeaf = ILeaf
takeTreeWhile f (IBranch a x y) = if (f a)
then IBranch a (takeTreeWhile f x)
(takeTreeWhile f y)
else ILeaf

(Technical error 13 applies to this exercise.)

This entry was posted on Saturday, July 28th, 2007 at 9:43 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

I think _ should be used when pattern matching the ILeaf cases:

takeTree 0 _ = ILeaf

takeTree _ ILeaf = ILeaf

takeTree n (IBranch x t1 t2) = IBranch x (takeTree (n-1) t1) (takeTree (n-1) t2)

takeTreeWhile _ ILeaf = ILeaf

takeTreeWhile pred (IBranch x t1 t2)

| pred x = IBranch x (takeTreeWhile pred t1)

(takeTreeWhile pred t2)

| otherwise = ILeaf

Also, guard can be used instead of if expression in takeTreeWhile.