data Tree t = Empty | Node t (Tree t) (Tree t)
我们可以创建 Functor 实例并使用
fmap :: (t -> a) -> Tree t -> Tree a
但是,如果我想要 (Tree t -> a) 而不是 (t -> a) 那么我可以访问整个 (Node t) 而不仅仅是 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)
与折叠相同
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