You haven't included a definition for what happens when the tree you are inserting into is Nil
, which presumably looks like
genTree x Nil = singleton x
You tried to do this with the line
genTree x (Node e l r) = if (Node e l r == Nil)
then singleton x
else ...
but if you think about it, you'll see that cannot work. The pattern match ensures that the tree you are looking at is of the form Node _ _ _
, and so it is never Nil
. That is, the test in your if
expression always evaluates to False
.