
Hi, I was writing some code for Tree. 1 : data Tree a = NULL | Node a (Tree a) (Tree a) works fine. But, 2 : data Tree a = NULL | Tree a (Node a) Tree a does not work. GHCI says Not in scope " type constructor or class 'Node'. Why is 2 declaration not valid though second definition looks much more closer to what a binary tree actually means ? Regards Nishant Verma

On Fri, May 15, 2015 at 4:48 AM, Nishant
1 : data Tree a = NULL | Node a (Tree a) (Tree a) works fine. But, 2 : data Tree a = NULL | Tree a (Node a) Tree a does not work. GHCI says Not in scope " type constructor or class 'Node'.
Why is 2 declaration not valid though second definition looks much more closer to what a binary tree actually means ?
Because Haskell isn't english. A constructor is parsed as the name of the constructor being defined followed by zero or more types. Your first version defines the constructor name Node followed by the types a, Tree a and Tree a. The second one defines the constructor name Tree followed by the types a, Node a and Tree a. You have to define the type Node for this to work. So you could do: data Node a = Node a data Tree a = NULL | Tree (Tree a) (Node a) (Tree a) But then you have to say "Tree NULL (Node 1.0) NULL" to a tree holding 1.0 with empty left and right subtrees. If you do it they way you said first, you can just say "Node 1.0 Null Null". Maybe you want "date Tree a = NULL | Node (Tree a) a (Tree a)"? Then you you can say "Node NULL 1.0 NULL". Or even "Date Tree a = NULL | Tree (Tree a) a (Tree a)", for "Tree NULL 1.0 NULL"?

Thanks Mike. The statement " A constructor is parsed as the name of the
constructor being defined followed by zero or more types" made everything
clear.
On Fri, May 15, 2015 at 3:57 PM, Mike Meyer
On Fri, May 15, 2015 at 4:48 AM, Nishant
wrote: 1 : data Tree a = NULL | Node a (Tree a) (Tree a) works fine. But, 2 : data Tree a = NULL | Tree a (Node a) Tree a does not work. GHCI says Not in scope " type constructor or class 'Node'.
Why is 2 declaration not valid though second definition looks much more closer to what a binary tree actually means ?
Because Haskell isn't english. A constructor is parsed as the name of the constructor being defined followed by zero or more types. Your first version defines the constructor name Node followed by the types a, Tree a and Tree a. The second one defines the constructor name Tree followed by the types a, Node a and Tree a. You have to define the type Node for this to work.
So you could do:
data Node a = Node a data Tree a = NULL | Tree (Tree a) (Node a) (Tree a)
But then you have to say "Tree NULL (Node 1.0) NULL" to a tree holding 1.0 with empty left and right subtrees. If you do it they way you said first, you can just say "Node 1.0 Null Null".
Maybe you want "date Tree a = NULL | Node (Tree a) a (Tree a)"? Then you you can say "Node NULL 1.0 NULL". Or even "Date Tree a = NULL | Tree (Tree a) a (Tree a)", for "Tree NULL 1.0 NULL"?
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-- Nishant
participants (2)
-
Mike Meyer
-
Nishant