Let's say I want to implement a doubly linked list in F# as a class. So far I have started with the following code:
type List<'T>(?value : 'T, ?head : List<'T>, ?tail : List<'T>) =
let mutable v : 'T = defaultArg value Unchecked.defaultof<'T>
let mutable h : List<'T> option = head
let mutable t : List<'T> option = tail
member this.value with get() = v and set(value) = v <- value
member this.head with get() = h and set(head) = h <- head
member this.tail with get() = t and set(tail) = t <- tail
Here, I have 3 questions mainly.
- Is this a good way to implement the doubly linked list in F#?
- What happens when setting the head to some list, does the whole object get copied or do I have only a reference to it? In the best case, I would only want a reference to the object.
- What happens if I use the ref keyword in F#? It's called a reference, but does it work like a pointer in C/C++?