English 中文(简体)
图一
原标题:Mapping Over List In A Tree
  • 时间:2010-12-03 18:05:18
  •  标签:
  • haskell

我需要你帮助解决以下两个职能/问题:

<>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,请看一看一看一看一看。

增 编

最佳回答

<>1>

首先,我注意到,尽管<条码>a是一种单一价值,而不是一个清单。 因此,您应当做如下工作:f a notmap f a

现在,你实际上问:

您认为,由于<条码>c是一份树木清单,<条码>> 树苗/条码”仅需要一 tree树。 因此,你做了些什么? 你们在树木名单上的每一树种上都适用mapmtree,然后将由此产生的树木清单作为新处的树木清单。 你如何这样做? 采用<代码>>在名单上:

mapmtree f (Branch a c) = Branch (f a) (map (mapmtree f) c)

<>2>

如第1条所示,请在<条码>/代码>上对每一树木适用<条码>。 结果将是名单2。 为了把名单变成一个统一名单,你可以使用<条码>-条码>功能,而这正是这一功能。


页: 1 当然,除非你在名单树上重新打上<条码>地图<>。

2 P-4, 2 P-3, 2 FS, 2 NS 由于每个树木都按<条码>mtree2list和<条码>绘制成一个清单。 然后回到一份名单,其中载有<代码>mtree2list。

问题回答

此外,由于你的职能(a->a)给你的树冠以你想要的类型,第一部分是错误的。 你们需要改变这种状况

mapmtree f (Leaf a) = Leaf (f a)




相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签