Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GHC type error which I do not understand

Tags:

haskell

ghc

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?

like image 534
Joon Avatar asked Jan 24 '23 07:01

Joon


1 Answers

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
like image 90
Joon Avatar answered Jan 25 '23 20:01

Joon