1

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.

2 Answers 2

7

Here's how you read these kind of errors:

No instance for (Fractional Integer)

Translation: your program has an Integer, but you are using one of the methods of the Fractional class on it.

arising from a use of `/'

Translation: The method involved is /, which is part of the Fractional class. Integer is not Fractional, so you cannot apply / to an integer.

Solution: Use div or quot instead.

I can get the same error in ghci easily enough:

Prelude> (1 :: Integer) / (2 :: Integer)

<interactive>:2:16:
    No instance for (Fractional Integer)
      arising from a use of `/'
    Possible fix: add an instance declaration for (Fractional Integer)
    In the expression: (1 :: Integer) / (2 :: Integer)
    In an equation for `it': it = (1 :: Integer) / (2 :: Integer)

Alternate fix: use a Fractional type such as a Rational instead of Integer:

Prelude> (1 :: Integer) `div` (2 :: Integer)
0
Prelude> :m + Data.Ratio
Prelude Data.Ratio> (1 :: Rational) / (2 :: Rational)
1 % 2
Sign up to request clarification or add additional context in comments.

3 Comments

Good answer covering alternatives.. In this particular case, assuming divisors works as advertised, d divides n so Sam can use div to get this Integer answer.
Note that div compute the quotient of an euclidean division, quot is a bit less mathematically interesting since it just mirror around 0, but on the other hand, quot is slightly faster on most hardware (being the CPU instruction) and given the fact that you're working (as many) with natural integers anyway...
@Jedai: And for Integer, they'll be the same speed, since Integer stores the sign and the magnitude separately. At least for Integer larger than a word.
4

Unlike in some languages, the / is not overloaded to work on Integers. This makes sense: Integer "division" is not the same thing as rational divisions. In Haskell

(/) :: Fractional a => a -> a -> a

but as I said, Integer is not Fractional which is why you get

No instance for (Fractional Integer)

Instead you can use the quot or div functions which perform integer division.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.