Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] what is the "best" block structure to code a tree structure?

1 view
Skip to first unread message

Arkady Andrukonis

unread,
Apr 17, 2009, 6:02:57 PM4/17/09
to caml...@yquem.inria.fr

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


_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Jon Harrop

unread,
Apr 18, 2009, 12:09:28 PM4/18/09
to grazi...@yahoo.com, caml...@yquem.inria.fr
On Friday 17 April 2009 23:02:43 Arkady Andrukonis wrote:
> Tuareg only helps to match parentheses.

Use ALT+Q to autoindent. One of the major advantages of *not* having
indentation sensitive syntax (and a PITA in Haskell and F#).

--
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e

Goswin von Brederlow

unread,
Apr 20, 2009, 1:30:18 AM4/20/09
to grazi...@yahoo.com, caml...@yquem.inria.fr
Arkady Andrukonis <grazi...@yahoo.com> writes:

> 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))

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

How about that?

I don't get your numbering though. The 20 is too deep and the second
30 not deep enough imho.

Mfg
Goswin

0 new messages