Reputation: 15
i'm very new in haskell and i have a problem. I want to take list for ex. [0,1,0,0,1,1,0,1] and put the elements in a Tree structure which is;
data Tree = Leaf Int | Node String (Tree) (Tree)
so far wrote the below code but it gives an error.
bdd (x:xs)= if elem x [0..9] then Leaf x else Node x (Tree) (Tree)
thank you for your help!
Upvotes: 0
Views: 234
Reputation: 42796
You are not using the Tree
constructor on the recursive call of your function, bdd should return a Tree
, wich is build with Leaf
or Node
, but notice that Node should be build with 2 trees with also should be built with Leaf
or Node
, not Tree
bdd :: [Int] -> Tree
bdd (x:xs)= if elem x [0..9] then Leaf x else Node (show x) (bdd xs) (bdd xs)
You should check how you want to properly build the tree, this is just an example.
Upvotes: 2
Reputation: 48631
Your code tries to put x
in a Leaf
, requiring it to be an Int
, and also tries to put out in a Node
, requiring it to be a String
. Certainly Int
and String
are different types, so this won't compile. Also, Tree
is a type constructor, so you can't use it in terms. What's your code actually supposed to do?
Upvotes: 2