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
Le 29/10/07, Dupont Corentin
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])
C'est normal, car GHCi est obligé de typer 5 sans autres informations, il lui donne donc le type par défaut pour un littéral entier : Integer. Comme ensuite tu utilises (/) qui ne prend pas un Integer comme opérande... Par contraste : map (/5) [0..10] Là, GHCi a plus d'information sur le typage de 5, il sait qu'il doit s'agir d'un Fractional, il lui donne donc le type Fractional par défaut, c'est à dire Double. Dans un véritable programme, ce problème n'apparaît pas, car GHC dispose toujours de meilleures contraintes de typage (ou la variable n'est pas utilisée). Il n'y a pas vraiment de remède au problème, excepté de mettre un devin dans GHCi, ou peut-être de retarder la compilation des lets... (mais jusqu'à quand ?) -- Jedaï
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
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@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
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.
How would you compute a mean then?
On 10/29/07, Dan Popa
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
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@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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-fr
Le 30/10/07, Dupont Corentin
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. How would you compute a mean then?
No, you can't "divide a real by an integer", but you can introduce the integer in the reals and divide by this. divideByI :: (Fractional a, Integral b) => a -> b -> a divideByI a b = a / fromIntegral b -- Jedaï
The stoty of divison may continue:
The function may be named as an operator
(///):: ....
we can declare it's level of priority and how can it
may associate values: left associative and priority
level 7
infixl 7 ///
And the required operator is ready to be used.
Don't forget: Haskell allow the programmer to define
it's own operators, using 10 levels of priority, being
left, or right or no-associative. Also, data
constructors like : of the lists can be (re)defined as
you wish. Including their priority and associativity!
Dan
--- Chaddaï Fouché
Le 30/10/07, Dupont Corentin
a écrit : 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. How would you compute a mean then?
No, you can't "divide a real by an integer", but you can introduce the integer in the reals and divide by this.
divideByI :: (Fractional a, Integral b) => a -> b -> a divideByI a b = a / fromIntegral b
-- Jedaï _______________________________________________ Haskell-fr mailing list Haskell-fr@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
--- Dupont Corentin
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
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
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
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
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@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-fr
__________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam
length paramétrer protection around
http://mail.yahoo.com _______________________________________________ Haskell-fr mailing list Haskell-fr@haskell.org http://www.haskell.org/mailman/listinfo/haskell-fr
_______________________________________________ Haskell-fr mailing list Haskell-fr@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
participants (3)
-
Chaddaï Fouché -
Dan Popa -
Dupont Corentin