Question

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 GHC complains:

No instance for (RealFrac Int) arising from a use of `floor' at ...

However, if I type the following into GHCi, it happily compiles it:

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

My question is: Why am I getting a type error from an expression that compiles successfully in GHCi?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top