Version française
Home     About     Download     Resources     Contact us    
Browse thread
Type problem... possible design problem
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] Type problem... possible design problem
On Sat, Dec 18, 2004 at 02:28:09PM +0000, chris.danx wrote:
> My concern with variants is sharing subgraphs in graphs, will they be 
> shared or copied?

A tree structure like this should be stored shared:

  type 'a tree = Leaf of 'a | Node of 'a tree list

You can prove this; for instance:

# let n = ref 1;;
val n : int ref = {contents = 1}
# let t = Leaf n;;  
val t : int ref tree = Leaf {contents = 1}
# let tree = Node [t; t];;
val tree : int ref tree = Node [Leaf {contents = 1}; Leaf {contents = 1}]
# n := 2;;
- : unit = ()
# tree;;
- : int ref tree = Node [Leaf {contents = 2}; Leaf {contents = 2}]

Rich.

-- 
Richard Jones.  http://www.annexia.org/  http://www.j-london.com/
>>>   http://www.team-notepad.com/ - collaboration tools for teams   <<<
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
http://subjectlink.com/ - Lesson plans and source material for teachers