Tôi đã mã Haskell sau:không thể phù hợp dự kiến loại 'Int 'với kiểu thực tế 'Integer'
-- 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
mang đến cho các lỗi sau:
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))]'
tôi nghi ngờ rằng trong ba (0, 0, 0.0)
số 0 là loại Int
. Có phải 0
luôn nhập Int
hoặc là ghci khấu trừ loại là Int
trong trường hợp này? Nếu sau này, làm thế nào để buộc nó phải nhập Integer
thay thế? Hoặc có cái gì khác gây ra lỗi này?
Cảm ơn lời giải thích rõ ràng. Tôi nghĩ rằng '0' là đa hình trong loại của nó, nhưng đôi mắt noob của tôi không thể thấy bất kỳ lý do nào khác cho suy luận kiểu đã đưa ra lỗi. Tất nhiên, tôi chỉ đơn giản là bỏ lỡ khai báo kiểu khai báo của tôi cho 'maxRatio'. –