如何解决在Haskell中打印自定义四叉树的建议方法?
在这样的项目中使用四叉树:
data Node = A | B | C deriving (Show)
data QuadTree = Null | Node Node (QuadTree) (QuadTree) (QuadTree) (QuadTree)
deriving Show
这些四叉树的深度最大为5,正如您可以想象的那样,当仅使用派生的Show输出时,很难很快理解树的内容。
在深度1,使用派生的Show,我已经在使用类似的东西:
Node A (Node B Null Null Null Null) (Node B Null Null Null Null) (Node C Null Null Null Null) (Node B Null Null Null Null)
我当前简化此输出的方法是使用:
toArray (Node x child1 child2 child3 child4) = [x] ++ toArray child1 ++ toArray child2 ++ toArray child3 ++ toArray child4
...简化了很多事情:[A,B,C,B]
代替了替代方法,但是随着树的成长,它仍然很快变得难以解释。
是否有人可以分享这些技巧或提示?任何建议都非常感谢。
编辑:为澄清起见,我只希望能够以最少的偏头痛诱导方式将树的内容视为任何可见的内容,同时调试将在树上使用的算法
解决方法
如果您的目的只是获得一个可解释的图形视图,那么我建议不要完全使用纯文本,而应该生成HTML,并让您的浏览器以方便的方式进行布局。 yeamer是一个特别容易使用的库(警告:它具有很大的依赖性)
{-# LANGUAGE OverloadedStrings #-}
import Presentation.Yeamer
import GHC.Exts (IsString(..))
data Node = A | B | C deriving (Show)
data QuadTree = Null | Node Node (QuadTree) (QuadTree) (QuadTree) (QuadTree)
deriving Show
displayQuadTree :: QuadTree -> Presentation
displayQuadTree Null = "Null"
displayQuadTree (Node x ch₀ ch₁ ch₂ ch₃)
= "Node "<>fromString (show x) -- Note: the ── and │ operators
── -- are Unicode Box-Drawing
displayQuadTree ch₀ │ displayQuadTree ch₁ -- characters. You can also
── -- use === and ||| instead.
displayQuadTree ch₂ │ displayQuadTree ch₃
main :: IO ()
main = yeamer . displayQuadTree
$ Node A (Node B Null Null Null Null)
(Node B Null Null Null Null)
(Node C Null Null Null Null)
(Node B Null Null Null Null)
运行此命令并将浏览器指向http://localhost:14910
时,您会看到
通过一些CSS调整,即使对于大树也可以进一步提高可读性:
{-# LANGUAGE OverloadedStrings,QuasiQuotes #-}
import Presentation.Yeamer
import GHC.Exts (IsString(..))
import Text.Cassius
data Node = A | B | C deriving (Show)
data QuadTree = Null | Node Node (QuadTree) (QuadTree) (QuadTree) (QuadTree)
deriving Show
displayQuadTree :: QuadTree -> Presentation
displayQuadTree Null = "Null"
displayQuadTree (Node x ch₀ ch₁ ch₂ ch₃) = "qt-node-box" #% do
"Node "<>fromString (show x)
──
displayQuadTree ch₀ │ displayQuadTree ch₁
──
displayQuadTree ch₂ │ displayQuadTree ch₃
main :: IO ()
main = yeamer . styling
([cassius|
body
background-color: black
color: white
font-size: 24pt
.qt-node-box
border: 1px solid grey
font-size: 70%
|]())
. displayQuadTree
$ Node A
(Node A (Node B Null Null Null Null)
(Node B Null Null Null Null)
(Node C Null Null Null Null)
(Node B Null Null Null Null))
(Node B (Node B Null Null Null Null)
(Node B Null Null Null Null)
(Node C Null Null Null Null)
(Node B Null Null Null Null))
(Node C (Node B Null Null Null Null)
(Node B Null Null Null Null)
(Node C Null Null Null Null)
(Node B Null Null Null Null))
(Node A (Node B Null Null Null Null)
(Node B Null Null Null Null)
(Node C Null Null Null Null)
(Node B Null Null Null Null))
Yeamer还使交互式隐藏和展开子树变得容易:如果将Node
子句更改为
displayQuadTree (Node x ch₀ ch₁ ch₂ ch₃)
= "Node "<>fromString (show x) ── do
"..."
"qt-node-box" #% (
displayQuadTree ch₀ │ displayQuadTree ch₁
──
displayQuadTree ch₂ │ displayQuadTree ch₃
)
然后只在第一时间显示
,然后单击省略号,然后您可以只展开真正有趣的部分,例如首先
然后
然后终于
此功能使所有功能甚至都可用于极其的大树,因为在大树上,完整的视图将是压倒一切的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。