
Hello. I would like to pretty print a tree in a way that its structure is easily perceived. For instance, consider the declarations: data Node a = Node a [Node a] type Tree a = [ Node a ] t = [ Node "a" [ Node "b" [] , Node "c" [ Node "c1" [] , Node "c2" [] ] , Node "d" [ Node "d1" [ Node "d1a" [] ] , Node "d2" [] ] ] ] Then the resulting of pretty printing the given tree would be something like the following: a | +-------------+ | | | b c d | | +---+ +---+ | | | | c1 c2 d1 d2 | d1a There is the module Text.PrettyPrint.HughesPJ, but it lacks examples on how to use the pretty print combinators, and it is not well docomented. I would like to see solutions for this problem, or clues on how to solve it. Regards, José Romildo

Perhaps drawTree on
http://www.haskell.org/ghc/docs/latest/html/libraries/containers/Data-Tree.h...
2009/5/14 José Romildo Malaquias
Hello.
I would like to pretty print a tree in a way that its structure is easily perceived.
For instance, consider the declarations:
data Node a = Node a [Node a]
type Tree a = [ Node a ]
t = [ Node "a" [ Node "b" [] , Node "c" [ Node "c1" [] , Node "c2" [] ] , Node "d" [ Node "d1" [ Node "d1a" [] ] , Node "d2" [] ] ] ]
Then the resulting of pretty printing the given tree would be something like the following:
a | +-------------+ | | | b c d | | +---+ +---+ | | | | c1 c2 d1 d2 | d1a
There is the module Text.PrettyPrint.HughesPJ, but it lacks examples on how to use the pretty print combinators, and it is not well docomented.
I would like to see solutions for this problem, or clues on how to solve it.
Regards,
José Romildo _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Thu, May 14, 2009 at 03:58:18PM -0300, José Romildo Malaquias wrote:
Then the resulting of pretty printing the given tree would be something like the following:
a | +-------------+ | | | b c d | | +---+ +---+ | | | | c1 c2 d1 d2 | d1a
There is the module Text.PrettyPrint.HughesPJ, but it lacks examples on how to use the pretty print combinators, and it is not well docomented.
Text.PrettyPrint.HughesPJ is actually not a good tool for this, since it is for pretty-printing line-oriented material (i.e. source code). I actually have a library I was developing a while ago to do the sort of pretty-printing/layout of text that you're looking for, using a box model similar to that of TeX, but I never released it. Hopefully I'll get around to polishing and releasing it this summer. -Brent

José Romildo Malaquias wrote:
Hello.
I would like to pretty print a tree in a way that its structure is easily perceived.
For instance, consider the declarations:
data Node a = Node a [Node a]
type Tree a = [ Node a ]
t = [ Node "a" [ Node "b" [] , Node "c" [ Node "c1" [] , Node "c2" [] ] , Node "d" [ Node "d1" [ Node "d1a" [] ] , Node "d2" [] ] ] ]
Then the resulting of pretty printing the given tree would be something like the following:
a | +-------------+ | | | b c d | | +---+ +---+ | | | | c1 c2 d1 d2 | d1a
If you're just curious about how one would write such a thing, you can look at Data.Trie.Internal.showTrie[1]--- it's horizontal rather than vertical, and it doesn't center labels above their children, but it should give you a starting idea. Data.Map and Data.IntMap also have examples (showTree, showTreeWith) which are a bit simpler. This is a common homework assignment (because it's a great exercise!) though I haven't seen any prepackaged generic solutions. Perhaps we need more enterprising students :) [1] http://community.haskell.org/~wren/bytestring-trie/src/Data/Trie/Internal.hs -- Live well, ~wren

Hello, Or --- if you just want pretty trees and you are not confined to the command line: You can generate GraphViz code and use that program to draw your tree in PostScript. (There is also a GraphViz-package, but generating the code yourself is easy.) Matthias.
participants (5)
-
Andrew Wagner
-
Brent Yorgey
-
José Romildo Malaquias
-
Matthias Görgens
-
wren ng thornton