开发者

Having a little problem with folding nested lists

So开发者_运维问答 I'm trying to write a function that folds a custom data type which is basically just a nested list. Any help would be greatly appreciated! Here's what I have so far.

data MyTree a = Node a [MyTree a] deriving (Show, Read, Eq)  

foldMT :: MyTree a -> [a]
foldMT (Node x) = [x]
foldMT (MyTree x) = concatMap foldMT x

I get the following error. It's referring to the last statement of foldMT.

Not in scope: data constructor `MyTree'


That is because MyTree is not a constructor. Your fold instead needs to be something like:

foldMT (Node x ts) = x : concatMap foldMT ts
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜