data Tree t = Empty | Node t (Tree t) (Tree t)
Мы можем создать экземпляр Functor и использовать
fmap :: (t -> a) -> Tree t -> Tree a
Но что, если вместо (t → a) я хочу (Tree t → a), чтобы я мог получить доступ к целому (Node t) не только t
treeMap :: (Tree t -> a) -> Tree t -> Tree a
treeMap f Empty = Empty
treeMap f [email protected](Node _ l r) = Node (f n) (treeMap f l) (treeMap f r)
То же самое со словом
treeFold :: (Tree t -> a -> a) -> a -> Tree t -> a
Существует ли какое-либо обобщение над такими функциями?
map :: (f t -> a) -> f t -> f a
fold :: (f t -> a -> a) -> a -> f t -> a