data Tree t = Empty | Node t (Tree t) (Tree t)
We can create Functor instance and use
fmap :: (t -> a) -> Tree t -> Tree a
But what if instead of (t -> a) I want (Tree t -> a) so I could have access to a whole (Node t) not just t
treeMap :: (Tree t -> a) -> Tree t -> Tree a
treeMap f Empty = Empty
treeMap f n@(Node _ l r) = Node (f n) (treeMap f l) (treeMap f r)
Same with fold
treeFold :: (Tree t -> a -> a) -> a -> Tree t -> a
Is there any generalization over functions like these?
map :: (f t -> a) -> f t -> f a
fold :: (f t -> a -> a) -> a -> f t -> a