我有以下Haskell代码:无法比拟预期的类型'诠释“与实际类型'整数”
-- Problem 69
import ProjectEuler
phi :: Integer -> Integer
phi n = n * product [p - 1 | p <- primeDivisors n] `div` product [p | p <- primeDivisors n]
-- primeDivisors n is a list of the prime divisors of n
maxRatio :: (Int, Int, Double) -> (Int, Int, Double) -> (Int, Int, Double)
maxRatio [email protected](_, _, x) [email protected](_, _, y)
| x > y = t1
| otherwise = t2
main = print (foldl
maxRatio
(0, 0, 0.0)
[(n, phi n, ratio) | n <- [2..max], let ratio = fromIntegral n/(fromIntegral (phi n))]
)
where max = 1000
它提供了以下错误:
Couldn't match expected type `Int' with actual type `Integer'
In the expression: n
In the expression: (n, phi n, ratio)
In the third argument of `foldl', namely
`[(n, phi n, ratio) |
n <- [2 .. max],
let ratio = fromIntegral n/(fromIntegral (phi n))]'
我怀疑的是,在三联(0, 0, 0.0)
0的类型为Int
。是0
总是键入Int
或ghci推断类型为Int
在这种情况下?如果以后,我该如何强制它改为输入Integer
?还是有其他的东西导致这个错误?
感谢您的明确解释。我认为'0'是它的类型的多态,但是我的noob眼睛看不到任何其他类型推理的原因,导致了错误。当然,我只是错过了我的'maxRatio'的显式类型声明。 –