caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* what is the "best" block structure to code a tree structure?
@ 2009-04-17 22:02 Arkady Andrukonis
  2009-04-18 16:16 ` [Caml-list] " Jon Harrop
  2009-04-20  5:29 ` Goswin von Brederlow
  0 siblings, 2 replies; 3+ messages in thread
From: Arkady Andrukonis @ 2009-04-17 22:02 UTC (permalink / raw)
  To: caml-list


Hi,

I would like to find the easiest block structure to represent nested leaves and nodes in a tree structure that works for OCaml. In Common Lisp there is the help of indentation, but I haven't found one for OCaml.

We have one parent node composed of one leaf and a nested node which has  another node with two leaves and a leaf (of the second node). To help illustrate the level of depth we can use numbers 10, 20, and 30.
    (*
-------------------
our type definition
-------------------
    *)
# type tree = Node of tree * tree | Leaf of int;;
    (*
------------
and our tree
------------
    *)
# Node ((Leaf 10), (Node ((Node ((Leaf 20),(Leaf 30))), Leaf 30)));;
-: tree = Node (Leaf 10, Node (Node (Leaf 20, Leaf 30), Leaf 30))

Figuring out the two components to each node and grouping them within parentheses is exceedingly hard for nested nodes without using a block structure. It's great if I can figure this out, but the code is unmaintanable for anyone else. Would you say it is better when written as (I added the periods to prevent email client from removing the whitespace)

Node
..((Leaf 10), (Node
.....((Node ((Leaf 20),
........(Leaf 30))), Leaf 30)));;

or

Node
..((Leaf 10), (Node
................((Node
...................((Leaf 20), (Leaf 30))),
(Leaf 30)));;

or

Node
..((Leaf 10), (Node
................((Node ((Leaf 20), (Leaf 30))),
..............(Leaf 30)));;

I've checked the manual and it recommends using white space for clarity and intent. In symmetrical trees the code is beautiful, not so in asymmetrical ones. Tuareg only helps to match parentheses.

With kindest regards,

kadee


      


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-04-20  5:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-17 22:02 what is the "best" block structure to code a tree structure? Arkady Andrukonis
2009-04-18 16:16 ` [Caml-list] " Jon Harrop
2009-04-20  5:29 ` Goswin von Brederlow

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).