[Haskell-cafe] Type conversion problems
Glynn Clements
glynn.clements at virgin.net
Sun Jun 13 06:58:36 EDT 2004
Lauri Alanko wrote:
> > fac :: Integer -> Integer
> > fac n = product [1..n]
> >
> > term :: Double -> Integer -> Double
> > term x n = (-1.0::Double)**(fromInteger n) * (x**(fromInteger (2*n +
> > 1))) /
> > (fromInteger (fac (2*n + 1)))
>
> Why do you have all those type annotations? Simply writing directly:
>
> fac n = product [1..n]
> term x n = -1 ** n * (x ** (2 * n + 1)) / fac (2 * n + 1)
>
> gives you functions for which are inferred the types (which you can of
> course also give explicitly if you want):
>
> fac :: (Enum a, Num a) => a -> a
> term :: (Floating a, Enum a) => a -> a -> a
>
> And the type variable a can then be instantiated for Double.
Except that it's arguable that Double shouldn't be an instance of
Enum.
Really, this "solution" is relying upon a misfeature of the language;
one which won't work in the general case. Suppose that fac was a
different function, one which couldn't be defined as returning a
non-integral result without using an explicit (and conceptually
incorrect) type conversion.
--
Glynn Clements <glynn.clements at virgin.net>
More information about the Haskell-Cafe
mailing list