Get sqrt from Int in Haskell

0xAX picture 0xAX · Jul 14, 2011 · Viewed 43.6k times · Source

How can I get sqrt from Int.

I try so:

sqrt . fromInteger x

But get error with types compatibility.

Answer

augustss picture augustss · Jul 14, 2011

Perhaps you want the result to be an Int as well?

isqrt :: Int -> Int
isqrt = floor . sqrt . fromIntegral

You may want to replace floor with ceiling or round. (BTW, this function has a more general type than the one I gave.)