[Haskell-beginners] Re: Polymorphism question from an OO-speaking newbie

Ertugrul Soeylemez es at ertes.de
Mon May 4 12:57:03 EDT 2009


Ertugrul Soeylemez <es at ertes.de> wrote:

> [...]  If that's all you need to know, you can generalize the function
>
>   foo :: Integer -> Integer -> Integer
>   foo a b = a*a + b*b
>
> to the following:
>
>   foo :: Integral i => i -> i -> i
>   foo a b = a*a + b*b
>
> Now 'foo' is defined for every type, which is an instance of the class
> Integral.  Being an instance of that class precisely means that (+),
> (*) and some other functions are defined for the particular type.
> Since this is all you need to know for 'foo', there is no reason to
> restrict it to Integer.

Sorry, I actually meant the Num class, not the Integer class, but the
example is still valid, because any Integral type is also a Num type.
So here is a more general 'foo':

  foo :: Num a => a -> a -> a
  foo a b = a*a + b*b


Greets,
Ertugrul.


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://blog.ertes.de/




More information about the Beginners mailing list