[Hugs-users] overloading operators

Alfredo Paz-Valderrama apaz at star.com.pe
Wed Apr 13 00:04:14 EDT 2005


Ok
but whe the Thompson's book says:

--------------------------------------------
Literals are not overloaded, there is no automatic conversion from Int to Float.
.. we will receive an error message if we type

(floor 5.6) + 6.7

since we are trying to add quantities of two different types. We have to convert
the Int to a Float to perform the addtion, thus:

fromIntegral (floor 5.6) + 6.7

where fromInt takes int to corresponding Float.
--------------------------------------------
pag. 45 "The Craft of Functional Programming" second edition


sadly, if I use a integer number it's work!

5 + 6.7

WHY?????

5 is don't Integer like (floor 5.6)?



--
Alfredo Paz-Valderrama
Sociedad Peruana de Computacion


Mensaje citado por Scott Turner <p.turner at computer.org>:

> On 2005 April 12 Tuesday 23:12, Alfredo Paz-Valderrama wrote:
> > If i write this in hugs:
> >
> > 1 / 2
> >
> > I get 0.5 by answer, but the / operator signature is:
> >
> > float -> float -> float
>
> 1 and 2 are overloaded. They can be Integer, Float, etc.
>
> You can think of 2 as meaning (fromInteger 2). FromInteger is defined in the
> Prelude to convert an Integer to any numeric type.
>
> Haskell organizes overloading using type classes. The type class called Num
> contains fromInteger, +, -, and *.  So these operators along with the integer
> literals 1,2, etc. can be used with any numeric type. You can define your own
> numeric type (for example polynomials) and use 1,2, and + with that.
>
> Actually, the / operator is not limited to Float, either. It is overloaded
> for
> the type class "Fractional", which includes Double and Rational.  Hugs will
> tell you the type is
>    Float -> Float -> Float
> because there's a defaulting mechanism so that if multiple types are
> possible,
> Hugs can choose one and give you an answer.
>


----------------------------------------------------------------
This mail sent through StarMail: http://www.star.com.pe


More information about the Hugs-Users mailing list