我试图得到这样的映射功能这对于n叉树的工作,但我在努力.
data NTree a = Leaf a | Node a [NTree a]
ntreeMap :: (a -> b) -> NTree a -> NTree b
ntreeMap f (Leaf x) = Leaf (f x)
ntreeMap f (Node y t) = Node (ntreeMap f y) (ntreeMap f t)
Run Code Online (Sandbox Code Playgroud)
给我
Type error in application *** Expression : ntreeMap f t *** Term : t *** Type : [NTree b] *** Does not match : NTree a
有人能给我一个关于我哪里出错的指针吗?谢谢
你有两个问题.其中之一是,你不必调用ntreeMap递归y的Node情况下,它的类型a,而不是NTree a:
ntreeMap f (Node y t) = Node (f y) (ntreeMap f t)
Run Code Online (Sandbox Code Playgroud)
第二个是t树的列表,你的函数只映射在一棵树上,所以它应该是
ntreeMap f (Node y t) = Node (f y) (map (ntreeMap f) t)
Run Code Online (Sandbox Code Playgroud)