What constitutes a fold for types other than list?
问题 Consider a single-linked list. It looks something like data List x = Node x (List x) | End It is natural to define a folding function such as reduce :: (x -> y -> y) -> y -> List x -> y In a sense, reduce f x0 replaces every Node with f and every End with x0 . This is what the Prelude refers to as a fold . Now consider a simple binary tree: data Tree x = Leaf x | Branch (Tree x) (Tree x) It is similarly natural to define a function such as reduce :: (y -> y -> y) -> (x -> y) -> Tree x -> y