here is my ADT:
data Tree a = Val Integer
|Tree a
|Variable a
I have two questions:
Question 1: use Tree String type to represent some trees?
Question 2: define a function for converting a tree, an element of the datatype Tree String to string: showTree::Tree String -> String
With respect to your second question, coverting a tree into a string, just derive
Showand use theshowfunction:I don’t understand your first question, so I’m just going to talk a bit in hopes that something I say helps you.
Understand that your “tree” data type is not really a tree at all, it’s just a sum data type that can be instantiated by an integer or some type that matches the type variable,
a. The second constructor,Tree, is actually not making your data type recursive – its just a constructor name in the same wayVariableis a constructor name. I think you probably wanted to have subtrees (by usingTreeas a type, not a constructor) – so let’s define your type as:Now you have a constructor named
Branchthat has a left and a right sub-tree. If your variables are supposed to beStrings then you certainly can useTree Stringto represent this: