
On Wed, 2010-06-30 at 20:01 -0700, Bryce Verdier wrote:
Hi all,
I'm having a real hard time wrapping my head around a problem;
Here is a basic, no frills, function. prime divisor divided | divided == 1 = True | divisor== divided= True | mod divided divisor== 0 = False | otherwise = next_prime where next_prime = prime (divisor + 2) divided
and the calling function: is_prime input = prime 3 input
I'm trying to get the square root of input as an Integer.
In GHCI: :t (toInteger $ round $ sqrt 25) (toInteger $ round $ sqrt 25) :: Integer
but if I change is_prime input = prime 3 (toInteger $ round $ sqrt input)
I get this error: problem3.hs:19:33: No instance for (RealFrac Integer) arising from a use of `round' at problem3.hs:19:33-37 Possible fix: add an instance declaration for (RealFrac Integer) In the first argument of `($)', namely `round' In the second argument of `($)', namely `round $ sqrt input' In the expression: toInteger $ round $ sqrt input
problem3.hs:19:41: No instance for (Floating Integer) arising from a use of `sqrt' at problem3.hs:19:41-50 Possible fix: add an instance declaration for (Floating Integer) In the second argument of `($)', namely `sqrt input' In the second argument of `($)', namely `round $ sqrt input' In the expression: toInteger $ round $ sqrt input Failed, modules loaded: none.
Can someone please help me out and tell me what I'm missing?
Prelude> let x = 25 Prelude> toInteger $ round $ sqrt x <interactive>:1:12: No instance for (RealFrac Integer) arising from a use of `round' at <interactive>:1:12-16 Possible fix: add an instance declaration for (RealFrac Integer) In the first argument of `($)', namely `round' In the second argument of `($)', namely `round $ sqrt x' In the expression: toInteger $ round $ sqrt x <interactive>:1:20: No instance for (Floating Integer) arising from a use of `sqrt' at <interactive>:1:20-25 Possible fix: add an instance declaration for (Floating Integer) In the second argument of `($)', namely `sqrt x' In the second argument of `($)', namely `round $ sqrt x' In the expression: toInteger $ round $ sqrt x Prelude> :t x x :: Integer Prelude> :t 25 25 :: (Num t) => t Left to its own devices ghci chooses "Integer" for the type of x but the constant 25 is the more generic "Num". This is the source of your problems. is_prime2 :: (Floating a, RealFrac a) => a -> Bool is_prime2 input = prime 3 (toInteger $ round $ sqrt input) satisfies the compilation. But do you really need the call to toInteger? The round() call by itself appears to be sufficient. If you just want round then you do not need the type specifier at all. You should look more closely at your definition of prime though. It is easy to give it numbers that has it recursing for ever.