[英]Couldn't match expected type ‘[Int]’ with actual type ‘Int’ haskell
I'm trying to do an exercise that gives a list of every path of a binary tree from leaf to root我正在尝试做一个练习,给出一个二叉树从叶到根的每条路径的列表
here's my code:这是我的代码:
data Tree = Leaf Int | Node (Tree) Int (Tree)
go (Leaf a) = (a : []) : []
go (Node l n r) = insev((go l):(go r)) n
insev :: [[a]] -> a -> [[a]]
insev [[]] x = []
insev (h:t) x = (insb h x) : (insev t x)
insb [] num = num : []
insb (h:t) num = h:(insb t num)
it should be correct from logical perspective but i am new to haskell and i don't know why i get this error从逻辑的角度来看它应该是正确的,但我是 haskell 的新手,我不知道为什么会出现此错误
Main.hs:21:19: error:
• Couldn't match type ‘[Int]’ with ‘Int’
Expected: [[Int]]
Actual: [[[Int]]]
• In the expression: insev ((go l) : (go r)) n
In an equation for ‘go’:
go (Node l n r) = insev ((go l) : (go r)) n
|
21 | go (Node l n r) = insev ((go l):(go r)) n
| ^^^^^^^^^^^^^^^^^^^^^^^
Main.hs:21:34: error:
• Couldn't match type ‘Int’ with ‘[Int]’
Expected: [[[Int]]]
Actual: [[Int]]
• In the second argument of ‘(:)’, namely ‘(go r)’
In the first argument of ‘insev’, namely ‘((go l) : (go r))’
In the expression: insev ((go l) : (go r)) n
|
21 | go (Node l n r) = insev ((go l):(go r)) n
| ^^^^
Main.hs:21:41: error:
• Couldn't match expected type ‘[Int]’ with actual type ‘Int’
• In the second argument of ‘insev’, namely ‘n’
In the expression: insev ((go l) : (go r)) n
In an equation for ‘go’:
go (Node l n r) = insev ((go l) : (go r)) n
|
21 | go (Node l n r) = insev ((go l):(go r)) n
Without even looking at the rest of the code, this is never * going to fly:甚至不看代码的rest ,这永远不会飞:
(go l) : (go r)
Presumably go
is returning something of the same type in both calls, but the arguments to (:)
must have different types:大概go
在两个调用中返回相同类型的东西,但是 arguments 到(:)
必须有不同的类型:
(:) :: a -> [a] -> [a]
Perhaps you want (++)
instead, whose arguments must have the same types:也许你想要(++)
而不是,它的 arguments 必须具有相同的类型:
(++) :: [a] -> [a] -> [a]
* Okay, yes, it is possible to write go
in a way that invalidates the next sentence's assumption that it is returning the same type in both calls. *好的,是的, go
的编写方式可能会使下一句假设它在两次调用中返回相同类型的假设无效。 But that's a pretty unusual situation to be in, and one you have to pretty consciously try to arrive at.但这是一种非常不寻常的情况,你必须非常有意识地尝试达到这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.