tags:

views:

194

answers:

1

I am teaching myself Haskell

I want to write a function that recursively finds the first number that has an integer square root and is smaller than a starting number.

It looks like this:

findFirstSquare :: Int -> Int
findFirstSquare x
    | x <= 0                               = error "This function only works for 1 or above"
    | fromInteger(floor(sqrt(x))) == (sqrt x) = x
    | otherwise                               = intSqrt(x - 1)

But the GHC parser complains about the middle guard when parsing my .hs file, with the following: No instance for (RealFrac Int) arising from a use of `floor' at ...

However, if I type the following into the interactive window, it happily parses it:

 fromInteger(floor(sqrt(4))) == (sqrt 4)

My question is: Why am I getting a parser error for an expression which seems equivalent to one that is parsed correctly from the command line?

+6  A: 

Ok, I figured it out.

The difference is that the constant "4" is overloaded, so interactively sqrt(4) is getting the square root of the Float 4

However my function declares x as an Int, therefore I needed to add a fromIntegral to the calls to sqrt, so that they would work.

Changing the middle guard to the following did the trick:

| fromIntegral(floor(sqrt(fromIntegral(x)))) == (sqrt(fromIntegral(x))) = x
Joon
That's right. Except I'd use `fromInteger` in place of `fromIntegral`.
Alexey Romanov
Incidentally, `fromInteger` is also used by GHCi to do the "overloading" of numeric literals.
Tom Lokhorst