How can I get sqrt
from Int
.
I try so:
sqrt . fromInteger x
But get error with types compatibility.
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.
There is no keyword aux , my guess is this is just the name they used for a local function. Just like you can define top-level values: myValue = 4. or top-level functions: myFunction x = 2 * x.
Haskell has two integral types, namely Int and Integer . Int is the type of limited-precision integers; this means that there is a smallest integer of type Int , namely minBound , and a greatest integer of type Int , namely maxBound .
Num is a typeclass — a group of types — which includes all types which are regarded as numbers. The (Num a) => part of the signature restricts a to number types – or, in Haskell terminology, instances of Num .
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With