Personal tools  Generic number type  From HaskellWiki  Revision
as of 16:11, 20 June 2007 by Byorgey (Talk | contribs)  Jump to:
navigation, search   1 Problem   Can I have a generic numeric
data type in Haskell which covers  Answer: In principle you can
define a type like  data GenericNumber =     Integer Integer   |
Rational Rational   | Double Double and define appropriate
instances for class et. al.   That is, when using s you will
encounter exactly the problems   type would also negate the type
safety that strongly typed numbers provide, putting the burden
back on the programmer to make sure they are using numbers in a
type-safe way. This can lead to subtle and hard-to-find bugs,
for example, if some code ends up comparing two floating-point
values for equality (usually a bad idea) without the programmer
realizing it.  2 Solutions   2.1 average   average xs = sum xs /
fromIntegral (length xs)  and you may prefer  average ::
[GenericNumber] -> GenericNumber average xs = sum xs /
genericNumberLength xs wit