I read a project written in OCaml and can't figure out some source code here:
type 'a node = {mutable parent: 'a node; mutable rank: int; label: 'a}
let singleton x =
let rec n = {parent=n; rank=0; label=x} in
n
This code is part of the disjoint set but I don't really understand the recurive function. I used to be a C++ programmer and can easily use a pointer to handle the parent thing.
When I run this code in the OCaml utop the result surprise me. It did generate many node.
Is this code cost much memory since it generate so many node?
How the compiler deal with this without overflow?