[Haskell-cafe] Prettier pretty-printing of data types?

Johan Holmquist holmisen at gmail.com
Wed Mar 14 07:54:37 CET 2012


I guess you want an automatically derived show that indents, but if
you don't mind defining you own, Data.PrettyPrint is really nice.

Here is an example that produces roughly the same as your example:

import Data.PrettyPrint

tree2doc Leaf = text "Leaf"
tree2doc (Bin x l r) =
    text "Bin" $$
    nest 2 (text (show x) $$
            tree2doc l $$
            tree2doc r)

showTree = render . tree2doc


/Johan

2012/3/13 Johan Tibell <johan.tibell at gmail.com>:
> Hi all,
>
> The derived Show instance is useful, but I sometimes wish for
> something that's easier to read for big data types. Does anyone have
> an implementation of show that draws things in a hierarchical manner?
> Example:
>
> Given
>
>    data Tree a = Leaf | Bin Int a (Tree a) (Tree a)
>
> and
>
>    value = Bin 1 (Bin 2 Leaf Leaf) (Bin 3 Leaf Leaf)
>
> draw as
>
> Bin
>  1
>  Bin
>    2
>    Leaf
>    Leaf
>  Bin
>    3
>    Leaf
>    Leaf
>
> Cheers,
> Johan
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list