I want to make:
data BinTree a b = b | Branch a (BinTree a b) (BinTree a b) deriving Show
But there is an error that b is not a data constructor. How can i fix that because i want my tree to always return b type.
How can i fix that because i want my tree to always return b type.
You need to define a data constructor that wraps the b value:
data BinTree a b = Leaf b | Branch a (BinTree a b) (BinTree a b) deriving Show
Then in functions where you obtain leaves, you can use pattern matching to "unwrap" the value out of the Leaf data constructor.
For example you can obtain a list of the values wrapped in the leaves for a BinTree a b with:
leaves :: BinTree a b -> [b]
leaves (Leaf b) = [b]
leaves (Branch _ la lb) = leaves la ++ leaves lb
You need a data constructor for the leaf; the type alone isn't not sufficient.
data BinTree a b = Leaf b | Branch a (BinTree a b) (BinTree a b)
Is there a reason you allow leaves and interior nodes to store values of different types? Also, you can't represent an empty tree with this data type. You might want
data BinTree a = Empty | Branch a (BinTree a) (BinTree a)
instead. With this, a leaf is now just a Branch value with both children empty: Branch 3 Empty Empty instead of Leaf 3.