[Haskell-beginners] Error while using type and data constructor

Nishant nishantgeek at gmail.com
Fri May 15 10:57:23 UTC 2015


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 <mwm at mired.org> wrote:

> On Fri, May 15, 2015 at 4:48 AM, Nishant <nishantgeek at gmail.com> 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 at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>


-- 
Nishant
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150515/1f4e843b/attachment.html>


More information about the Beginners mailing list