[Haskell-beginners] Empty or Tree?

Adrien Haxaire adrien at haxaire.org
Sat Mar 10 12:06:03 CET 2012


On Sat, Mar 10, 2012 at 10:28:43AM +0000, bahadýr altan wrote:
> Hello everyone. I'm trying to write a code which works on binary trees. When I write code like this  with a tree with empty nodes :
> 
> data Tree  =  Empty | Node  Integer Tree Tree
> 
> function Node a (Node b Empty Empty)  (Node c Empty Empty)
> 
> it works fine. 
> But when I try to create a more generic code like this  which could  work with trees who don't have empty nodes in grandchild level : 
> 
> function Node a (Node b Tree Tree)  (Node c Tree Tree ) 
> 
> 
> I get this error : Undefined data constructor "Tree"
> 
> Can you help me with creating more generic code please?
> Thanks

Hello,

You defined a Tree as an Empty or a Node. So to build it, you can use only Empty or Node, which is what you do in your first function. In the second one however, you use Tree, which is the type, not the constructor.

If you use them it will look like : 

function Node a (Node b t1 t2) (Node c t3 t4)
  where
    t1 = Empty
    t2 = Node d Empty Empty
    ...

Adding the function signature before your function implementatio help sort things out for the parameters you need: t1, to t4, to a to c, etc

Hope that helps.
Adrien

-- 
Adrien Haxaire 
www.adrienhaxaire.org | @adrienhaxaire



More information about the Beginners mailing list