From: Martin Jambon <martin.jambon@ens-lyon.org>
To: "Daniel Bünzli" <daniel.buenzli@erratique.ch>
Cc: caml-list List <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] Annotated trees
Date: Sun, 06 Jun 2010 22:15:44 -0700 [thread overview]
Message-ID: <4C0C8080.902@ens-lyon.org> (raw)
In-Reply-To: <4C0C7F08.3010403@ens-lyon.org>
Martin Jambon wrote:
> 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 ;-)
See, I even did not consider the possibility of using:
type 'a t = [ `Leaf of string | `Node of 'a t list ] * 'a
Note that point (4) remains valid and based on real experience.
Martin
--
http://mjambon.com/
next prev parent reply other threads:[~2010-06-07 5:15 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-06-06 15:26 Daniel Bünzli
2010-06-07 5:09 ` [Caml-list] " Martin Jambon
2010-06-07 5:15 ` Martin Jambon [this message]
2010-06-07 9:22 ` Daniel Bünzli
2010-06-07 12:01 ` blue storm
2010-06-07 6:45 ` Jacques Garrigue
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4C0C8080.902@ens-lyon.org \
--to=martin.jambon@ens-lyon.org \
--cc=caml-list@yquem.inria.fr \
--cc=daniel.buenzli@erratique.ch \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox