Get sqrt from Int in Haskell
Asked Answered
M

3

34

How can I get sqrt from Int.

I try so:

sqrt . fromInteger x

But get error with types compatibility.

Mariken answered 14/7, 2011 at 14:55 Comment(0)
C
50

Using fromIntegral:

Prelude> let x = 5::Int
Prelude> sqrt (fromIntegral  x)
2.23606797749979

both Int and Integer are instances of Integral:

  • fromIntegral :: (Integral a, Num b) => a -> b takes your Int (which is an instance of Integral) and "makes" it a Num.

  • sqrt :: (Floating a) => a -> a expects a Floating, and Floating inherit from Fractional, which inherits from Num, so you can safely pass to sqrt the result of fromIntegral

I think that the classes diagram in Haskell Wikibook is quite useful in this cases.

Chlorophyll answered 14/7, 2011 at 14:58 Comment(4)
Could sqrt (fromIntegral x) also be written as sqrt $ fromIntegral x?Cetacean
Yes indeed it could, since explicit application ($) does not bind as tightly as implicit application.Labored
or even sqrt . fromIntegral $ xMilord
I prefer to write it as ((((($)))((sqrt))(fromIntegral (x)))).Alister
S
54

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

Shillelagh answered 14/7, 2011 at 15:23 Comment(3)
ghc warns about this, since it doesn't know which exact type to use between fromIntegral and floor (could be Double,Float, etc.). To fix: isqrt x = floor . sqrt $ (fromIntegral x :: Float), which is less elegant :(Afeard
I like it, anyway. No warns on GHC 8.Demigod
ghc will warn if -Wtype-defaults, -Werror=type-defaults.Illegitimate
C
50

Using fromIntegral:

Prelude> let x = 5::Int
Prelude> sqrt (fromIntegral  x)
2.23606797749979

both Int and Integer are instances of Integral:

  • fromIntegral :: (Integral a, Num b) => a -> b takes your Int (which is an instance of Integral) and "makes" it a Num.

  • sqrt :: (Floating a) => a -> a expects a Floating, and Floating inherit from Fractional, which inherits from Num, so you can safely pass to sqrt the result of fromIntegral

I think that the classes diagram in Haskell Wikibook is quite useful in this cases.

Chlorophyll answered 14/7, 2011 at 14:58 Comment(4)
Could sqrt (fromIntegral x) also be written as sqrt $ fromIntegral x?Cetacean
Yes indeed it could, since explicit application ($) does not bind as tightly as implicit application.Labored
or even sqrt . fromIntegral $ xMilord
I prefer to write it as ((((($)))((sqrt))(fromIntegral (x)))).Alister
L
13

Remember, application binds more tightly than any other operator. That includes composition. What you want is

sqrt $ fromIntegral x

Then

fromIntegral x 

will be evaluated first, because implicit application (space) binds more tightly than explicit application ($).

Alternately, if you want to see how composition would work:

(sqrt .  fromIntegral) x

Parentheses make sure that the composition operator is evaluated first, and then the resulting function is the left side of the application.

Labored answered 14/7, 2011 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.