type Tree = { Root: Node }
and Node = { Tree: Tree }
let rec newTree = { Root = newRoot }
and newRoot = { Tree = newTree }
internal
visibility on the backing fields, which also leads that any function initializing such tree/root records must reside within the same assembly as the type definitions (not too great, but I can live with that).option
s for the mutually dependable fields, but I really want to model that each tree has a root node (no empty trees in my system) and each node has a tree, without having to test for Some
/None
.there is no need to declare a type for root. from a type perspective, a tree is a node. the canonical way to define a tree in f# is like so
type Node =
| N of int * Node list // (inner) node
| L of int // leaf
let tree = N(3, [L(5); L(7)])
its your choice if you define a separate case for the leaf or simply use
type Node = N of int * Node list
int is the node data type. you will customize this or even use a generic.
i often use mutable children collections, then i use records like
type Node = { data: int; mutable children: Node list }
let root = { data=3; children=[] }
root.children <- [{ data=7; children=[] }]