Get sqrt from Int in Haskell

前端 未结 3 1809
被撕碎了的回忆
被撕碎了的回忆 2020-12-15 04:07

How can I get sqrt from Int.

I try so:

sqrt . fromInteger x

But get error with types compatibility.

3条回答
  •  囚心锁ツ
    2020-12-15 04:27

    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.)

提交回复
热议问题