[Haskell-fr] Fractional etc...

Dan Popa popavdan at yahoo.com
Mon Nov 5 13:53:17 EST 2007


--- Dupont Corentin <corentin.dupont at gmail.com> wrote:

> There must be a way to divide a real by an integer??
> My integer is eventually a "number of elements" as
> in the exemple of Dan.

As you can see below it's impossible to divide a Float
by an Integer directly. Haskell is "strongly-typed" .
But there exist a conversion function:

fromInt :: Int->Float

Prelude> (10.2::Float)/(2::Integer)
ERROR - Type error in application
*** Expression     : 10.2 / 2
*** Term           : 10.2
*** Type           : Float
*** Does not match : Integer

Prelude> (10.2::Float)/(2::Int)
ERROR - Type error in application
*** Expression     : 10.2 / 2
*** Term           : 10.2
*** Type           : Float
*** Does not match : Int

Solution:

Prelude> (10.2::Float)/fromInt(2::Int)
5.1

References:
Simon Thompson, Craft of Functional Programming,
second edition, page 44 - basic types and definitions.


> How would you compute a mean then?
> 
> 
> On 10/29/07, Dan Popa <popavdan at yahoo.com> wrote:
> >
> > Why don't you try :
> > > map (/5.0) [0.0 .. 10.0]
> > >
> > > let m = 5.0
> > > map (/m) [0.0 .. 10.0]
> > >
> > I have a similar problem trying a divison by a 
> length
> > of a list. Finaly I have to build a version of
> length
> > called len:
> >
> > len [] = 0.0
> > len (h:t)  = 1.0 + len t
> >
> > Dan
> >
> >
> > --- Dupont Corentin <corentin.dupont at gmail.com>
> wrote:
> >
> > > Salut,
> > > J'ai vraiment du mal à mettre au point un petit
> > > programme...
> > > Il me sort souvent des problèmes du style :
> > >
> > >   No instance for (Fractional Integer)
> > >      arising from use of `/' at
> <interactive>:1:4-7
> > >   Possible fix: add an instance declaration for
> > > (Fractional Integer)
> > >
> > > Par exemple sous ghci si je fait:
> > >
> > > map (/5) [0..10]
> > >
> > > tout se passe bien.
> > > Mais si je me dit "tiens je voudrais bien
> paramétrer
> > > le 5":
> > >
> > > let m = 5
> > > map (/m) [0..10]
> > >
> > > il me sort:
> > >     No instance for (Fractional Integer)
> > >       arising from use of `/' at
> <interactive>:1:4-7
> > >     Possible fix: add an instance declaration
> for
> > > (Fractional Integer)
> > >     In the first argument of `map', namely `(/
> m)'
> > >     In the expression: map ((/ m)) ([0 .. 10])
> > >     In the definition of `it': it = map ((/ m))
> ([0
> > > .. 10])
> > >
> > >
> > > Pourtant les 2 codes me sembles assez
> équivalents!!!
> > > Je peux utiliser des "fromInteger", ce qui
> résout
> > > temporairement le problème.
> > >
> > > Je me retrouve par la suite avec des (de
> mémoire)
> > > Infered type : Int
> > > Expected type : Integer
> > >
> > > Ce qui me laisse dans le flou...
> > >
> > > a+
> > > Corentin
> > > _______________________________________________
> > > Haskell-fr mailing list
> > > Haskell-fr at haskell.org
> > >
> http://www.haskell.org/mailman/listinfo/haskell-fr
> > >
> >
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Tired of spam?  Yahoo! Mail has the best spam
> protection around
> > http://mail.yahoo.com
> > _______________________________________________
> > Haskell-fr mailing list
> > Haskell-fr at haskell.org
> > http://www.haskell.org/mailman/listinfo/haskell-fr
> >
> > _______________________________________________
> Haskell-fr mailing list
> Haskell-fr at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-fr
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


More information about the Haskell-fr mailing list