[英]Haskell Tree To List
Haskell here we go! 哈斯克尔在这里我们走了! Okay so here is an example of the code currently used:
好的,这是当前使用的代码示例:
data Tree a = Node (Tree a) (Tree a) | Leaf a | Empty deriving (Show)
main = do
let my_tree = Node (Node (Leaf [1,2,4,9]) (Leaf [3,5,5,8,5])) (Leaf [2,7,5,5,2])
cc my_tree
bb my_tree
aa my_tree
cc my_tree
print("Done")
Simply creating a tree that holds Int lists and need 3 functions to work aa,bb,cc: 只需创建一个包含Int列表并需要3个函数来运行aa,bb,cc的树:
cc - simply outputs a tree cc - 只输出一棵树
bb - ads to each value +2 bb - 广告到每个值+2
aa - outputs a list for each value like: [length,minimum, maximum] aa - 输出每个值的列表,如:[length,minimum,maximum]
now there is a code that does all this almost perfectly, I put it in codepad (ask me if you want): http://codepad.org/ ??????? 现在有一个几乎可以完美完成所有工作的代码,我将其放在键盘中(如果需要,请问我): http ://codepad.org/ ??????? now as you can see the last output of aa is also a tree
现在你可以看到aa的最后一个输出也是一棵树
its probably a quick fix but I cant figure it out - so that aa would output not a tree but a list [] of these: 它可能是一个快速解决方案,但我无法弄清楚-这样,aa将不会输出树,而是输出其中的列表[]:
[length param,minimum param,maximum param]
in other words, does someone know how to output it as a list, not inside tree?? 换句话说,有人知道如何将其作为列表输出,而不是在树内?
so in output instead of: 所以输出而不是:
Node (Node (Leaf [4,1,9]) (Leaf [6,0,8])) (Leaf [5,2,7])
would be 将会
[[4,1,9],[6,0,8],[5,2,7]]
I believe something has to be modified here: 我相信这里必须进行一些修改:
fmap3 ff (Node l r) = Node (fmap2 ff l) (fmap2 ff r)
fmap3 ff (Leaf x) = Leaf (ff x)
Anyone ?? 任何人 ??
This one works 这个作品
data Tree a = EmptyTree | Node a (Tree a) (Tree a)
treeToList :: (Ord a) => Tree a -> [a]
treeToList EmptyTree = []
treeToList (Node root left right) = treeToList left ++ [root] ++ treeToList right
You're trying to perform a reduce operation: turn a tree into a single value (three times: length, maximum, minimum). 您正在尝试执行归约操作:将树变成单个值(三遍:长度,最大值,最小值)。 This cannot be done using a map function, because a map by definition always turns a tree into a tree.
使用map函数无法完成此操作,因为按定义映射始终将树转换为树。
A typical reduce algorithm uses a function to "merge" together two reduced results, and recursively merges the results obtained anywhere in the tree. 典型的reduce算法使用一个函数将两个简化的结果“合并”在一起,然后递归合并树中任何位置获得的结果。 So, you'll be having something like this:
所以,你会有这样的事情:
reduce ff (Node l r) = ff (reduce ff l) (reduce ff r)
reduce ff (Leaf x) = x
Once this is done, you can use a map function to turn your tree of (whatever) into a tree of values to be reduced, and apply the reduce function. 完成此操作后,您可以使用map函数将(任何树)变成要减少的值树,并应用reduce函数。
cc tree = [ reduce (+) (fmap2 length tree) ,
reduce (min) (fmap2 minimum tree) ,
reduce (max) (fmap2 maximum tree) ]
EDIT : you've changed your question while I was answering. 编辑 :当我回答时,您已经更改了您的问题。 You can do this using the above reduction function and the list concatenation function
++
, but list concatenation is not very sexy in terms of performance. 您可以使用上面的归约函数和列表串联函数
++
来执行此操作,但是就性能而言,列表串联并不是很性感。
There's an idiomatic way to turn a tree into a list by using a traversal with an accumulator argument: 通过使用带有累加器参数的遍历,有一种惯用的方法可以将树变成列表:
traverse init (Node l r) = traverse (traverse init r) l
traverse init (Leaf x) = x : init
cc tree = traverse [] (fmap2 h tree)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.