在Haskell中打印自定义四叉树的建议方法?

如何解决在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时,您会看到

example Yeamer output

通过一些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))

How it can look with some styling

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₃
      )

然后只在第一时间显示

all the tree still hidden

,然后单击省略号,然后您可以只展开真正有趣的部分,例如首先

top-level branches unhidden

然后

one subbrach unhidden

然后终于

one branch fully expanded

此功能使所有功能甚至都可用于极其的大树,因为在大树上,完整的视图将是压倒一切的。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-