[Haskell-cafe] Mixed-type arithmetic, or type coercion

Dave Hinton beakerchu at googlemail.com
Tue Mar 13 16:41:00 EDT 2007


Arithmetic operators in haskell appear to require their operands to
have the same type.  How can I do arithmetic between operands of
different types?

Alternatively, how do I coerce a value from being an Int to being a Double?

I am new to haskell.  I have read a few tutorials but they do not
mention this issue.  I have read the haddock documentation for Prelude
and Numeric, but cannot find anything that helps here.  (I saw a
couple of numeric conversion functions, but they won't turn an Int
into a Double.)

Toy example:

$ cat eg.hs
calc :: Int -> Double -> Double
calc count weight = count * weight

$ ghc eg.hs

eg.hs:2:
    Couldn't match `Int' against `Double'
        Expected type: Int
        Inferred type: Double
    In the second argument of `(*)', namely `weight'
    In the definition of `calc': calc count weight = count * weight


More information about the Haskell-Cafe mailing list