I'm studying ML in class and I've run into a homework problem I am stuck on. I've spent all day yesterday searching but made little progress and we did not talk about this in class, so I am hoping you guys can help me.
We are given:
datatype which = STRING of string | INT of int
Part 1. We are told we need to create another datatype named whichTree for a binary tree containing the values of type which where data is only at the leaves of the tree.
Part 2. We need to create a whichSearch function having type whichTree -> int -> bool returning true or false based on whether the int is in the tree.
This is what I have so far:
datatype which = STRING of string | INT of int;
datatype whichTree = Empty | Node of int*whichTree*whichTree;
val t1 = Node(6, Node(4,Empty,Empty), Node(15, Node(11,Empty,Empty), Node(24,Empty,Empty)));
val t2 = Node(157,Empty,Empty);
val t3 = Node(102,t1,t2);
fun whichSearch (i, Empty) = false
| whichSearch (i, Node(entry, left, right)) =
if i = entry then true
else whichSearch (i, left)
orelse whichSearch (i, right);
The problem I am now facing is this:
- My
whichTreedoes not contain the typewhich. I am not sure how I can fix that. - I should have my
whichSearchfunction of typewhichTree -> int -> boolbut it isint * whichTree -> booland am working on trying to figure out how to fix things. I am not sure how I would go about fixing this as I have to specify a value foriinwhichSearchto search for. I'm searching on this but any tips would be great.
Can anyone help? If so thank you! And thank you to the guys who already responded.