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
精彩评论