I am trying to implement a function treeToString that uses this data type:
datatype Tree = LEAF of string | NODE of Tree list;
and this tree:
val L1a = LEAF "a"
val L1b = LEAF "b"
val L1c = LEAF "c"
val L2a = NODE [L1a, L1b, L1c]
val L2b = NODE [L1b, L1c, L1a]
val L3 = NODE [L2a, L2b, L1a, L1b]
val L4 = NODE [L1c, L1b, L3]
val L5 = NODE [L4]
The function takes an argument like treeToString L5 which will output the string ((cb((abc)(bca)ab)))
The problem is I cannot figure out the correct way to differentiate between when the function takes in a LEAF or a NODE type. I wrote the code below to try and test this but I am getting errors. Anyone know the syntax in order to get this to work? Once I get the parameters down, the actual recursion should be easy.
(* treeToString *)
fun treeToString(LEAF str) = str
| treeToString(NODE h::t) =
h;