简体   繁体   English

无法将预期类型“[Int]”与实际类型“Int”相匹配 haskell

[英]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.

相关问题 Haskell无法将期望的类型“ Double”与实际类型“ Int”匹配 - Haskell Couldn't match expected type ‘Double’ with actual type ‘Int’ Haskell:无法将期望的类型[(Int,Int)]与实际的类型IO [[Int,Int)]相匹配 - Haskell: Couldn't match expected type [(Int, Int)] with actual type IO [(Int,Int)] Haskell 错误 - 无法将类型“[Int] -> String”与“[Char]”匹配; 预期类型:字符串; 实际类型:[Int] -> 字符串 - Haskell Error - Couldn't match type `[Int] -> String' with `[Char]'; Expected type: String; Actual type: [Int] -> String 无法匹配预期类型 [Int] Haskell - Couldn't match expected type [Int] Haskell Haskell编程分配,“无法将期望的类型'Int'与实际类型'[a0]-> Int'匹配”以及其他一些错误 - Haskell Programming Assignment, “Couldn't match expected type ‘Int’ with actual type ‘[a0] -> Int’ ”and a few more Errors 无法将期望类型“Int”与实际类型“IO [Int]”匹配 - Couldn't match expected type `Int' with actual type `IO [Int]' 无法将预期类型“Int”与实际类型[Int]匹配 - Couldn't match expected type `Int' with actual type [Int] Haskell 列表理解类型错误:无法将预期类型“Float”与实际类型“Int”匹配 - Haskell list comprehension type error: Couldn't match expected type ‘Float’ with actual type ‘Int’ Haskell:无法预期实际类型'Int'的'Integer'类型 - Haskell: Couldn't expected type 'Integer' with actual type 'Int' Haskell:无法将预期类型“[a0]”与实际类型“([char], [int])”相匹配 - Haskell: Couldn't match expected type '[a0]' with actual type '([char], [int])'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM