I am trying to do following:
10 ** length xs * x
but I get:
No instance for (Floating Int) arising from a use of `**'
Haskell takes a very different approach. It starts by performing type analysis with a type checker to understand your program at compile time. The type checker strictly prohibits type casting and does not allow type errors to be ignored.
The workhorse for converting from integral types is fromIntegral , which will convert from any Integral type into any Num eric type (which includes Int , Integer , Rational , and Double ): fromIntegral :: (Num b, Integral a) => a -> b.
In Haskell, every statement is considered as a mathematical expression and the category of this expression is called as a Type. You can say that "Type" is the data type of the expression used at compile time.
You can use ^
to raise to an integral power. There's no need to convert to float here.
Besides @sepp2k's answer, if you somehow really need to convert from an integer to some other types of Num, use fromIntegral
.
-- # fromIntegral :: (Integral a, Num b) => a -> b
10 ** fromIntegral (length xs) * x
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