Some troubles with types
I had a bit of trouble with a type which I used for modeling trees, which can be navigated up, sideways and down, and where nodes can be added left, right and down. Context
I have the following type for nodes:
type node = {
mutable value : int;
parent : node option;
left : node option;
right : node option;
mutable children : node list;
}
Pretty straightforward. Now suppose I need to find a node in the children list and do something like
if node = child then
then OCaml crashes with an out of memory error. Well, I guess that is what circular references and structural equality will do for you, but I wasn't expecting it.
So first question is: Is that something which OCaml should handle better or is just the expected behavior.
Another problem is that I'd rather have the field children be node array than a node list, but I how I would I then initialize it?
Even if somehow I would have managed (Obj.magic and such) to create a dummy value of type node it would not be pretty.
Well, I guess this simply cannot be done..
Posted Mar 28, 2016, 9:51 PM UTC
[deleted]
Wow! That's lot to digest.
And yes, left and right fields should be mutable. Also unique IDs for nodes are a good idea.