I wrote my data type:
data Tree a = Leaf | Node a Color (Tree a) (Tree a) deriving (Show)
And I want to use it, but I don't understand how. By design I want to create a function that returns an empty tree (tree with Leaf
), but I don't understand what I should pass to the constructor, I tried something like this:
emptyTree:: Tree a
emptyTree = Tree
I also get the following warning
Data constructor not in scope: Tree :: t0 -> Tree a • Perhaps you meant ‘True’
P.S
I have already imported the module with the tree type
CodePudding user response:
The type constructor is Tree
, that is to construct a type, not data. The data constructor(s) here are Leaf
and Node
, these are thus used to construct objects. Type constructors and data constructors can have the same name, but since the "type world" and the "value world" do not (really) interact, that does not create confusion for the compiler.
Here you thus can use Leaf
or Node
. Here Leaf
is the only sensical thing, so:
emptyTree:: Tree a
emptyTree = Leaf
But you can use Leaf
anywhere in the code, so defining this is not necessary.