I wrote this code for a beautiful tree print:
data Node = A | B | C | Tree Node [Node] deriving (Show, Eq) printTree' :: Int -> Node -> [String] printTree' spaceCount (Tree node children) = (printTree' spaceCount node) ++ concat (map (\child -> printTree' (spaceCount+2) child) children) printTree' spaceCount leaf = [(replicate spaceCount ' ') ++ (show leaf)] printTree :: Node -> String printTree node = unlines (printTree' 0 node)
Output Example:
*Main> putStr $ printTree $ Tree A [Tree A [A,B,C], C] A A A B C C
Now I would like to do this for the implementation of show . This approach is close, but I cannot find a way to invoke the built-in show :
instance Show Node where show (Tree node children) = printTree (Tree node children) show _ = "node... can I call the built-in show here?"
(In this example, I can just deal with A, B, and C. But in the real code, there are many types of node.)
source share