【发布时间】:2018-05-26 17:10:18
【问题描述】:
我想为我的二叉树实例化显示函数,以这种方式构造:data Tree a = Nil | Leaf a | Branch a (Tree a) (Tree a)。
我想实现像“tree” unix 命令这样的表示。例如:
显示功能是:
> 27
>> 14
>>> 10
>>> 19
>> 35
>>> 31
>>> 42
我想用递归函数将每个“子树”制成表格,但我不知道这是我的实际代码:
instance (Show a)=>Show (Tree a) where
show Nil = ""
show (Leaf e) = show e
show (Branch e ls rs) = show e ++ "\n\t" ++ show ls ++ "\n\t" ++ show rs
所以问题是:我如何实现递归制表函数,因为每次我使用换行和制表一次而不是子树深度
【问题讨论】:
标签: haskell tree instance show