用于分析数据的树输出的库 [英] Library for tree output of data for analysis

查看:75
本文介绍了用于分析数据的树输出的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为便于分析数据,我想使用一个库来获得以下代码:

  data SomeType = A [String] Int | B | C Int推导(Eq,Ord,Show)

main = do
让theData = A [a,b,c] 9:C 3:B:[ ]
putStr $ treeString theData - `treeString`是隐含的库函数

会产生类似于以下的输出:

   -  A:
| - - a
| | - b
| | - c
| - 9
- C:
| - 3
- B

是否有这样一个库?或者,也许更好的方法来解决这个问题? Data.Tree drawTree drawForest 函数具有相似的格式,因此您可以编写一个函数将您的数据结构转换为 Tree String 然后使用 drawTree

  import Data.Tree 

data SomeType = A [String] Int | B | C Int派生(Eq,Ord,Show)

toTree :: SomeType - >树字符串
toTree(A xs n)=节点A[节点*(地图(翻转节点[])xs),节点(节点n)[]]
toTree B =节点 B[]
toTree(C n)=节点C[Node(show n)[]]

main = do
let theData = A [a ,b,c] 9:C 3:B:[]
putStr $ drawTree(Node*(map toTree theData))

输出:
$ b

  * 
|
+ - A
| |
| + - *
| | |
| | + - a
| | |
| | + - b
| | |
| | ` - c
| |
| ` - 9
|
+ - C
| |
| ` - 3
|
` - B


For convenient analysis of data I'd like to use a library which for the following code:

data SomeType = A [String] Int | B | C Int deriving (Eq, Ord, Show)

main = do 
  let theData = A ["a", "b", "c"] 9 : C 3 : B : []
  putStr $ treeString theData -- `treeString` is the implied library function

would produce an output similar to the following:

- A:
| - - a
| | - b
| | - c
| - 9
- C:
| - 3
- B

Is there such a library? Or maybe a better approach to such a problem?

解决方案

Data.Tree has drawTree and drawForest functions with similar formatting, so you can write a function to convert your data structure to a Tree String and then use drawTree.

import Data.Tree

data SomeType = A [String] Int | B | C Int deriving (Eq, Ord, Show)

toTree :: SomeType -> Tree String
toTree (A xs n) = Node "A" [Node "*" (map (flip Node []) xs), Node (show n) []]
toTree B        = Node "B" []
toTree (C n)    = Node "C" [Node (show n) []]

main = do 
  let theData = A ["a", "b", "c"] 9 : C 3 : B : []
  putStr $ drawTree (Node "*" (map toTree theData))

Output:

*
|
+- A
|  |
|  +- *
|  |  |
|  |  +- a
|  |  |
|  |  +- b
|  |  |
|  |  `- c
|  |
|  `- 9
|
+- C
|  |
|  `- 3
|
`- B

这篇关于用于分析数据的树输出的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆