假设我有一棵二叉树。
main = putStrLn $ printTree tree
data Tree = Empty | Node Int (Tree) (Tree) deriving (Show)
tree = Node 4 (Node 3 Empty (Node 2 Empty Empty)) Empty
printTree :: Tree -> String
printTree x = case x of
Node num treeA treeB -> show num ++ "\n" ++ printTree treeA ++ "\n" ++ printTree treeB
Empty -> "Empty"
输出
*Main> main
4
3
Empty
2
Empty
Empty
Empty
所需的输出(由制表符或双空格分隔即可)
*Main> main
4
3
Empty
2
Empty
Empty
Empty