我需要你帮助解决以下两个职能/问题:
<>1>
我必须替换树中的元素。 树木处可以有如下文所示的任何分支。
data Tree a = Leaf a | Branch a [(Tree a)] deriving (Show)
mapmtree :: (a -> a) -> Tree a -> Tree a
mapmtree f (Leaf a) = (f a)
mapmtree f (Branch a c) = Branch (map f a) (mapmtree f c)
我不得不通过这些内容来改变这些内容。 我的问题是最后一行。 “地图”功能接受(Tree a),但分支部门可以有一个分行清单,因此无法汇编上述代码,因为它造成错误。 如何将“地图”功能列入分支部门名单?
这是在装货时的错误:
Couldn t match expected type `Tree a
against inferred type `[Tree a]
In the second argument of `mapmtree , namely `c
In the second argument of `Branch , namely `(mapmtree f c)
In the expression: Branch (map f a) (mapmtree f c)
<>2>
第二项涉及将树变成深度-第一的树木。 这是现在的法典,但被 st,不知道如何进一步:
data Tree a = Leaf a | Branch a [(Tree a)] deriving (Show)
mtree2list :: Tree a -> [a]
mtree2list (Leaf a) = [a]
mtree2list (Branch a c) = a : (mtree2list c)
Need help also how to implement it. The same problem as the above, the Branch can have many sub trees and need to go through them in depth-first to make a list of the elements.
在Haskell,请看一看一看一看一看。
增 编