[Haskell-cafe] Pretty printing a tree

José Romildo Malaquias j.romildo at gmail.com
Thu May 14 14:58:18 EDT 2009


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


More information about the Haskell-Cafe mailing list