I’ve defined a polymorphic data type called Tree a as follows:
data Tree a = Leaf | Node a (Tree a) (Tree a)
I want to define a function mapT that takes a function and applies it to every data item of type a within a tree of type Tree a. The essential purpose of this function would be to operate on trees the same way the map function operates on list so I’ve written a function based off of that idea:
mapT :: (a -> b) -> Tree a -> Tree b
mapT f Leaf = Leaf
mapT f ((Tree a) left right) = (Tree a) (mapT f left) (mapT f right)
However, when I run this, I get a Parse error in pattern: (Tree a) and I can’t figure out what’s wrong. Does anyone have any idea how to get passed this?
Try:
Note that your
Leafconstructor doesn’t have any data associated with is, so perhaps you really want:with the corresponding change in
mapT.Also, once you have got
mapTworking for you, you should read up on theFunctortype class, and why you might want to create aFunctorinstance forTree a: