I’ve been struggling with this for over half an hour. I know it’s something simple, but I am awful at types in Haskell, and even after reading the accepted answers to problems very similar to mine, I still cannot solve my problem – let alone understand it!
The code:
p108 = [filter (\[a,b] -> a>0 && b>0) (diophantinepairs n) | n <- [1..]]
diophantinepairs :: Integer -> [[Integer]]
diophantinepairs n = nub$map sort b
where
a = divisors n
b = [[(n-d), n - (n^2)/d] | d <- a]
The error :
249:39: No instance for (Fractional Integer) arising from a use of `/' Possible fix: add an instance declaration for (Fractional Integer) In the second argument of `(-)', namely `(n ^ 2) / d' In the expression: n - (n ^ 2) / d In the expression: [(n - d), n - (n ^ 2) / d]
Thanks,
Sam.
Here’s how you read these kind of errors:
Translation: your program has an
Integer, but you are using one of the methods of theFractionalclass on it.Translation: The method involved is
/, which is part of theFractionalclass.Integeris notFractional, so you cannot apply/to an integer.Solution: Use
divorquotinstead.I can get the same error in
ghcieasily enough:Alternate fix: use a
Fractionaltype such as aRationalinstead ofInteger: