简体   繁体   English

无法将预期类型'(Int,Int)'与实际类型'[t0]'相匹配

[英]Couldn't match expected type ‘(Int, Int)’ with actual type ‘[t0]’

why is this giving me error? 为什么这给我错误? I am just trying this as a test to learn Haskell where i am just strapping off the tuples in the second input. 我只是想以此作为学习Haskell的测试,在这里我只是在第二个输入中删除了元组。 Why does this not compile ? 为什么不编译? Thank you 谢谢

test :: (Int,Int) -> [(Int,Int)] -> Int
test [] [] = []
test xs [] = []
test (x,xs) (y:ys)  =   test (x,xs) ys

Thank you @Carcigenicate for pointing out the first error. 感谢@Carcigenicate指出第一个错误。 Now if we have 现在,如果我们有

test :: (Int,Int) -> [(Int,Int)] -> Int
test xs [] = []
test (x,xs) (y:ys)  =   test (x,xs) ys 

I get 我懂了

• Couldn't match expected type ‘Int’ with actual type ‘[t0]’
• In the expression: []
  In an equation for ‘test’: test xs [] = []

Your function signature says that the first argument should be a tuple of 2 ints, but then in your first pattern matching line: 您的函数签名表示,第一个参数应该是2个整数的元组,但是在您的第一条模式匹配行中:

test [] [] = []
      ^

You try to match a tuple against a list. 您尝试将一个元组与一个列表匹配。

  1. The types don't match, thus the error. 类型不匹配,因此出错。

  2. A tuple will never be empty unless that's what type it is. 除非那是一个元组,否则它永远不会为空。 Unlike lists, tuples can't be added to or removed from; 与列表不同,元组不能添加或删除。 they have a fixed size. 他们有固定的大小。

If you said the tuple will have 2 values, it will always have 2 values. 如果您说元组将有2个值,它将始终有2个值。 No need to check if it's empty. 无需检查是否为空。 It looks like you can get rid of that line, since it's not doing anything legal or useful. 看起来您可以摆脱这一限制,因为它没有做任何合法或有用的事情。

Also note your naming is a little confusing. 另请注意,您的命名有些混乱。 Typically, x and xs represent the head and tail of a collection. 通常, xxs代表集合的头和尾。 Tuples don't really have either, just different "slots". 元组实际上也没有,只有不同的“插槽”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将预期类型(Int - > Int - > Int)与实际类型`(t0,t1,t2)'匹配 - couldn't match expected type (Int -> Int -> Int) with actual type `(t0, t1, t2)' 无法将预期类型 `(a1 -> a1 -> a1) -> (t0 a0 -> Int) -> t Int -> Int' 与实际类型 `Int' 匹配 - Couldn't match expected type `(a1 -> a1 -> a1) -> (t0 a0 -> Int) -> t Int -> Int' with actual type `Int' 无法将预期类型`Maybe(String,Int,String)'与实际类型`([Char],t0,[Char])'匹配 - Couldn't match expected type `Maybe (String, Int, String)' with actual type `([Char], t0, [Char])' 无法将期望类型“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] 无法将预期类型“ [Char]”与实际类型“ Int”匹配 - Couldn't match expected type ‘[Char]’ with actual type ‘Int’ 无法将预期类型“Int”与实际类型“Integer”匹配 - Couldn't match expected type `Int' with actual type `Integer' Map function - 无法将预期类型“Int”与实际类型“[a]”匹配 - Map function - Couldn't match expected type `Int' with actual type `[a]' 无法将预期类型“Int”与实际类型“a”匹配 - Couldn't match expected type `Int' with actual type `a' 无法将预期类型“布尔”与实际类型“ Int”匹配 - Couldn't match expected type ‘Bool’ with actual type ‘Int’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM