Haskell一次折叠一层结构
示例
变形(或折叠)为原始递归建模。cata使用代数函数(或折叠函数)逐层分解固定点,以处理每一层。cata需要Functor模板类型的实例f。
cata :: Functor f => (f a -> a) -> Fix f -> a cata f = f . fmap (cata f) . unFix -- list example foldr :: (a -> b -> b) -> b -> List a -> b foldr f z = cata alg where alg Nil_ = z alg (Cons_ x acc) = f x acc