[Haskell-cafe] Mixed-type arithmetic, or type coercion
Neil Mitchell
ndmitchell at gmail.com
Tue Mar 13 16:51:02 EDT 2007
Hi
> Arithmetic operators in haskell appear to require their operands to
> have the same type. How can I do arithmetic between operands of
> different types?
You probably don't want to.
> Alternatively, how do I coerce a value from being an Int to being a Double?
You ask Hoogle:
http://haskell.org/hoogle/?q=Integer+-%3E+Double
The answer is fromInteger/toInteger as required.
> calc :: Int -> Double -> Double
> calc count weight = fromInteger (toInteger count) * weight
toInteger promotes the Int to an Integer, fromInteger then converts
that to a Double.
Thanks
Neil
More information about the Haskell-Cafe
mailing list