嗨,這裏是我的代碼如下。Haskell:無法與Int類型[Int]匹配
data Treeof a = Node Int a [Treeof a] deriving (Eq,Show)
add_score :: Int -> Treeof a -> [[Int]]
add_score 0 (Node i a xs) = [[0]]
add_score n (Node i a xs) = [i]:[(map (add_score (n-1)) xs)]
我試圖獲取存儲在樹中每個節點的詮釋,並將其存儲在一個列表的列表,但得到如下所示的錯誤,我不能確定這是爲什麼。
Couldn't match type `[[Int]]' with `Int'
Expected type: Treeof a -> Int
Actual type: Treeof a -> [[Int]]
In the first argument of `map', namely `(add_score (n - 1))'
In the expression: (map (add_score (n - 1)) xs)
Failed, modules loaded: none.
編輯:更改[I]:[(地圖(add_score第(n-1))XS)]至[I] :(圖(add_score第(n-1))XS)
而得到一個類似的錯誤
Couldn't match type `[Int]' with `Int'
Expected type: Treeof a -> [Int]
Actual type: Treeof a -> [[Int]]
In the first argument of `map', namely `(add_score (n - 1))'
In the second argument of `(:)', namely
`(map (add_score (n - 1)) xs)
Failed, modules loaded: none.
你想在這裏做什麼 - 你不要添加任何東西! – Carsten
@Carsten我試圖獲取存儲在樹的節點中的Ints,並在初始節點之後爲樹的第一級的每個分支添加每個分數。 –
請粘貼代碼片段而不是屏幕截圖。 – amalloy