[Haskell-cafe] help in tree folding
lrpalmer at gmail.com
Tue May 6 10:31:26 EDT 2008
On Tue, May 6, 2008 at 6:20 AM, patrik osgnach <patrik.osgnach at gmail.com> wrote:
> Hi. I'm learning haskell but i'm stuck on a generic tree folding exercise. i
> must write a function of this type
> treefoldr::(Eq a,Show a)=>(a->b->c)->c->(c->b->b)->b->Tree a->c
> Tree has type
> data (Eq a,Show a)=>Tree a=Void | Node a [Tree a] deriving (Eq,Show)
> as an example treefoldr (:)  (++)  (Node '+' [Node '*' [Node 'x' ,
> Node 'y' ], Node 'z' ])
> must return "+∗xyz"
> any help?
> (sorry for my bad english)
Functions like this are very abstract, but are also quite nice in that
there's basically only one way to write them given the types.
What have you tried so far? This function needs to be recursive, so
what arguments should it give to its recursive calls, and where should
it plug the results?
It also helps, as you're writing, to keep meticulous track of the the
types of everything you have and the type you need, and that will tell
you what you need to write next.
More information about the Haskell-Cafe