qiubit
qiubit

Reputation: 4816

Changing properties of an object

I've got a type representing a tree:

type 'a tree =
    | Null
    | Node of 'a * 'a tree * 'a tree * 'a tree ref;; 

And my objective is to write a function 'a tree -> unit that takes tree and changes it in such a way, that every node has a reference to the next node in infix order (the last node points to Null).

How to change a given tree without returning it?

Upvotes: 0

Views: 120

Answers (1)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66823

Here's a function that makes a tree node point to itself:

 let circulate tn =
     match tn with
     | Null -> ()
     | Node (_, _, _, tr) -> tr := tn

Upvotes: 1

Related Questions