[英]Haskell - An error related to types
I have these functions in a Haskell file and they work fine : 我在Haskell文件中具有这些功能,它们可以正常工作:
func1 :: Integer -> (Integer,Integer) -> [[String]] -> ([Char],[Char],[Char],[Char]) -> (Integer,Integer)
func1 distance agent mymap moves = func5 (func3 agent (func2 distance agent mymap) moves)
func2 :: Integer -> (Integer,Integer) -> [[String]] -> [(Integer,Integer)]
func3 :: (Ord a, Ord b) => (b,a) -> [(b,a)] -> ([Char],[Char],[Char],[Char]) -> [(b,a)]
func4 :: (Int,Int) -> (Int,Int) -> ([Char],[Char],[Char],a) -> ([Char],[Char],[Char],[Char]) -> [[[Char]]] -> [[[Char]]]
func5 [(a,b)] = (a,b)
But when I write this function : 但是当我写这个函数时:
func6 agent distance mymap moves moves2 = func4 agent (func1 distance agent mymap moves) moves moves2 mymap
I get this error : 我收到此错误:
*ERROR "play.hs":176 - Type error in application
* * * Expression : moveWithFood agent (giveNearestCorrect distance agent mymap moves) moves moves2 mymap
* * * Term : giveNearestCorrect distance agent mymap moves
* * * Type : (Integer,Integer)
* * * Does not match : (Int,Int)*
Same error with ghci: ghci同样的错误:
play.hs:176:93:
Couldn't match expected type `Integer' against inferred type `Int'
Expected type: (Integer, Integer)
Inferred type: (Int, Int)
In the second argument of `giveNearestCorrect', namely `agent'
In the second argument of `moveWithFood', namely
`(giveNearestCorrect distance agent mymap moves)'
Failed, modules loaded: none.*
I tried several things to solve it but I couldn't succeed. 我尝试了几种方法来解决它,但未能成功。 Can you tell me what I should do? 你能告诉我该怎么办吗? Thanks. 谢谢。
As pigworker pointed out, Int
and Integer
are not the same type. 正如Pigworker指出的那样, Int
和Integer
不是同一类型。 If you have just a few points where you need a "translation", fromIntegral
might be the way to go. 如果只有几个地方需要“翻译”,那么fromIntegral
可能是fromIntegral
的方法。
For common applications Int
is often good enough (and faster than Integer
), so I would suggest you try to use this exclusively. 对于常见的应用程序, Int
通常足够好(并且比Integer
更快),因此,我建议您尝试专门使用它。
Another possibility would be using the Num
type-class. 另一种可能性是使用Num
类型类。 Here is an example for a function that works for both Int
and Integer
: 这是一个适用于Int
和Integer
的函数的示例:
func1 :: Num a => a -> (a, a) -> [[String]] -> ([Char],[Char],[Char],[Char]) -> (a, a)
You might need to use some fromIntegral
calls inside, depending on your original implementation. 您可能需要在内部使用一些fromIntegral
调用,具体取决于您的原始实现。
You are using agent
as the second argument to func1
/ giveNearestCorrect
(so it must be an (Integer, Integer)
, according to the type signature), and also as the first argument to func4
/ moveWithFood
(so it must be an (Int, Int)
). 您正在使用agent
作为func1
/ giveNearestCorrect
的第二个参数(因此,根据类型签名,它必须是(Integer, Integer)
),也用作func4
/ moveWithFood
的第一个参数(因此,它必须是(Int, Int)
)。
agent
cannot be both an (Integer, Integer)
and an (Int, Int)
. agent
不能同时是(Integer, Integer)
和 (Int, Int)
。 Choose one, and stick to it: I would follow the advice in pigworker's comment. 选择一个并坚持下去:我会遵循养猪工人评论中的建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.