【发布时间】:2016-01-30 01:22:04
【问题描述】:
所以我遇到了一些变量类型的问题。
getOrdenado :: (String,Int,String,Int,Int) -> Int
getOrdenado (_,_,_,a,_) = a
listaOrdenado :: [(String,Int,String,Int,Int)] -> [Int]
listaOrdenado xs = map getOrdenado xs
getOrdenado 从包含在元组列表中的元组中获取某个 Int 值,listaOrdenado 列出所有这些特定的 int。
这个函数应该在以下列表上工作:
firma = [("Ana", 30, "RH", 1500, 3), ("Rui", 40, "Vendas", 1000, 2),
("Luis", 45, "RH", 3333, 5), ("Maria", 55, "Admin", 2000, 4)]
但每当我尝试使用此列表运行 listaOrdenado 时,我都会收到以下错误
Couldn't match type `Integer' with `Int'
Expected type: [(String, Int, String, Int, Int)]
Actual type: [([Char], Integer, [Char], Integer, Integer)]
In the first argument of `listaOrdenado', namely `firma'
In the expression: listaOrdenado firma
In an equation for `it': it = listaOrdenado firma
我不允许在类签名中使用 Integer,只能使用 Int 所以我不知道如何解决这个问题,也不知道为什么会这样表示 firma 中的那些值是 Integer 的
【问题讨论】:
-
您可以通过将
30 :: Int之类的内联类型强制Int作为类型,或者给firma一个类型签名,以便它使用Int而不是Integer(Integer是默认值) -
尝试将
firma :: [(String, Int, String, Int, Int)]放在firma的声明之前。如果您在 GHCi 中定义它,则将其放入文件中。 -
我不能给 firma 任何签名,更具体地说,我不能改变任何关于 firma
-
那么无论您在代码中使用
firma的任何位置,都可以像这样引用它:(firma :: [(String, Int, String, Int, Int)]) -
所以我应该将此行添加到我的代码中:
标签: haskell integer int type-conversion