Is there an universal way of thinking on how to create a fold function for a new data type?
For example, the fold function for the data Tree is:
data Tree t = Leaf | Node t (Tree t) (Tree t)
deriving (Eq,Ord,Show)
treeFold:: (a -> b -> b -> b) -> b -> Tree a -> b
treeFold f e Leaf = e
treeFold f e (Node x l r) = f x (treeFold f e l) (treeFold f e r)
For example, how would I have to create the fold function for the following data?
data Json a = Val a | Obj [(String, Json a)]
I know the type would have to contain 2 functions, one for each ot the cases Val and Obj. What do I have to consider while creating the fold? I hope my question makes sense. I've just came across many different datatypes where it was asked to write a fold function for a data type, and I don't seem to find the pattern.