In hoc signo vinces (Was: Revamping the numeric classes)
Dylan Thurston
dpt@math.harvard.edu
Fri, 9 Feb 2001 16:49:09 -0500
On Fri, Feb 09, 2001 at 12:55:12PM -0800, William Lee Irwin III wrote:
> class (Eq a, Show a) => Num a where
> (+), (-), (*) :: a -> a -> a
> negate :: a -> a
> abs, signum :: a -> a
> fromInteger :: Integer -> a
> fromInt :: Int -> a -- partain: Glasgow extension
>
> ... So we have two totally inappropriate operations (fromInteger and
> fromInt), ...
I beg to differ on this point. One could provide a default
implementation for fromInt(eger) as follows, assuming a 'zero' and
'one', which do obviously fit (they are the additive and
multiplicative units):
fromInteger n | n < 0 = negate (fromInteger (-n))
fromInteger n = foldl (+) zero (repeat n one)
(Of course, one could use the algorithm in integer exponentiation to
make this efficient.)
Best,
Dylan Thurston