module Tree where import PrettierPrinter data Tree = Node String [Tree] showTree (Node s ts) = group (text s <> nest (length s) (showBracket ts)) showBracket [] = nil showBracket ts = text "[" <> nest 1 (showTrees ts) <> text "]" showTrees [t] = showTree t showTrees (t:ts) = showTree t <> text "," <> line <> showTrees ts showTree' (Node s ts) = text s <> showBracket' ts showBracket' [] = nil showBracket' ts = bracket "[" (showTrees' ts) "]" -- Text in the book (pages 225 and 242) leaves out the ' in the recursive calls, -- but that doesn't produce the output shown on page 225 showTrees' [t] = showTree' t showTrees' (t:ts) = showTree' t <> text "," <> line <> showTrees' ts tree = Node "aaa" [ Node "bbbbb" [ Node "ccc" [], Node "dd" [] ], Node "eee" [], Node "ffff" [ Node "gg" [], Node "hhh" [], Node "ii" [] ] ] testtree w = putStrLn (pretty w (showTree tree)) testtree' w = putStrLn (pretty w (showTree' tree))