I am attempting to implement a circular doubly linked list using OCaml type declaration. Here is what I have :
type 'a cList =
{
mutable value : 'a;
mutable left : 'a cList option;
mutable right : 'a cList option
}
;;
The problem comes up when I need to declare a first list containing a single element. Because the element cannot be referenced before being assigned, I cannot get the left and right members of the cell to point on itself. The only workaround I have so far is to allow the left and right members to be of type option, set them to None and then modify them so that they point on the cell itself.
let unitClist v = let a = {
value = v;
left = None;
right = None
}
in
a.left <- Some a;
a.right <- Some a;
a
;;
It works, but it is a bit binding to have to work with the option type when you are sure to have a value. Is there a better way to do it ? Thanks in advance.