I have the following two datatypes:
datatype leaf = Slist of string list | Real of real | nil;
datatype 'a tree = Empty | Node of leaf * 'a tree * 'a tree * 'a tree;
The code below goes through all trees of length one/two and forms a list of the values in the leafs.
fun list12(Empty:'a tree) = nil
| list12(Node(leaf leaf1, 'a tree a1, 'a tree a2, 'a tree a3)) =
if (not(a1 = Empty) andalso not(a2 = Empty) andalso not(a3 = Empty))
then list12(a1)::list12(a2)::list12(a3)
else leaf1::list12(a1)::list12(a2)::list12(a3);
The issue is, I get syntax errors such as
stdIn:94.59-94.66 Error: syntax error: deleting TYVAR ID
stdIn:94.71-94.78 Error: syntax error: deleting TYVAR ID
stdIn:94.83-94.93 Error: syntax error: deleting TYVAR ID ID
stdIn:94.93-94.97 Error: syntax error: deleting RPAREN RPAREN EQUALOP
stdIn:94.98-94.102 Error: syntax error: deleting IF LPAREN
stdIn:94.109-94.116 Error: syntax error: deleting EQUALOP ID
The code in itself isn't complicated. Base case is if it's empty, returns null. If it doesn't have three nodes, then I add the value of the leaf and recursively call the function on the nodes. If it does, I just recursively call the function on the nodes without adding the leaf.
It works because it it's empty, it ends that search by adding nil to the list, which does nothing.
I've also tried other cases such as using and
instead of andalso
, as well as other versions of the code such as
| list12(Node(leaf1, Empty, Empty, Empty)) = nil
| list12(Node(leaf2, a1, Empty, Empty)) = leaf2::list12(a1);
| list12(Node(leaf3, b1, b2, Empty)) = leaf3::list12(b1)::list12(b2);
| list12(Node(leaf4, c1, c2, c3)) = list12(c1)::list12(c2)::list12(c3);
but I've found that the above doesn't match all cases.
Any idea on why the syntax errors are appearing?
Side note, why doesn't 1.0 = 2.0
work, but in the summaries it says it works for reals? It only appears to work for integers, and >
, <
, and so on don't work either.