Version française
Home     About     Download     Resources     Contact us    
Browse thread
Annotated trees
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Martin Jambon <martin.jambon@e...>
Subject: Re: [Caml-list] Annotated trees
Daniel Bünzli wrote:
> Hello,
> 
> Does anybody have any real world experience in choosing between the
> following two representations for a tree augmented with annotations ?
> 
> type 'a t = 'a * [ `Leaf of string | `Node of 'a t list ]
> type 'a t = [ `Leaf of 'a * string | `Node of 'a * 'a t list ]
> 
> Which one is more convenient to process, pattern match on, makes code
> more readable etc. ?

I normally use the second form.

I see the following advantages over the tuple form:

1. Pattern-matching is more readable because the most important piece of
information comes first:

   `Leaf (_, s) -> ...
 | `Node (_, l) -> ...

instead of:

   (_, `Leaf s) -> ...
 | (_, `Node l) -> ...


2. If needed, writing an annotation_of_t function instead of just using fst is
simple enough and not invasive.

3. It is possible to not annotate certain kinds of nodes, or to have different
types of annotations depending on the kind of node.

4. The tuple version feels like there are 2 different definitions of a tree
node, i.e. it is easy to get confused about whether an annotated node (the
pair) or an non-annotated node (second member of the pair) is expected in one
given place.

5. I got this habit and therefore I won't change my mind and reject all
rational arguments against it ;-)



Martin

-- 
http://mjambon.com/