Type inference problem with division (/)

Hi! I have the following function: theRemainder :: [String] -> [String] -> Double theRemainder xs xt = sum( map additional (unique xs) ) where additional x = poccur * (inf [ppos,pneg]) --inf takes [Double] where xsxt = zip xs xt pi = countPos xr -- countPos returns an Int ni = (length xr) - pi len = length xs len2 = length xr ppos = pi/len2 -- THESE ARE THE PROBLEM pneg = ni/len2 -- THESE ARE THE PROBLEM poccur = (pi+ni)/len xr = (filter ((\y -> (fst y)==x)) (xsxt)) And I am getting this error message with ghc: matrix.hs:54:31: Couldn't match expected type `Double' against inferred type `Int' In the expression: ppos In the first argument of `inf', namely `[ppos, pneg]' In the second argument of `(*)', namely `(inf [ppos, pneg])' How can I change the declaration of ppos nad pneg so they are treated as Double for the inf function? Cheers, Nick -- View this message in context: http://www.nabble.com/Type-inference-problem-with-division-%28-%29-tf4722111... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

On 10/30/07, noa
Hi!
I have the following function:
theRemainder :: [String] -> [String] -> Double theRemainder xs xt = sum( map additional (unique xs) ) where additional x = poccur * (inf [ppos,pneg]) --inf takes [Double] where xsxt = zip xs xt pi = countPos xr -- countPos returns an Int ni = (length xr) - pi len = length xs len2 = length xr ppos = pi/len2 -- THESE ARE THE PROBLEM pneg = ni/len2 -- THESE ARE THE PROBLEM poccur = (pi+ni)/len xr = (filter ((\y -> (fst y)==x)) (xsxt))
And I am getting this error message with ghc:
matrix.hs:54:31: Couldn't match expected type `Double' against inferred type `Int' In the expression: ppos In the first argument of `inf', namely `[ppos, pneg]' In the second argument of `(*)', namely `(inf [ppos, pneg])'
How can I change the declaration of ppos nad pneg so they are treated as Double for the inf function?
ppos = pi/len2; pi and len2 are both Ints, so dividing them gives you an Int. To convert to a Double, write ppos = fromIntegral (pi/len2). (Type :t fromIntegral in ghci to see what else fromIntegral can be used for.) Cheers, Tim -- Tim Chevalier * catamorphism.org * Often in error, never in doubt "After three days without programming, life becomes meaningless." -- James Geoffrey

On 10/30/07, Tim Chevalier
ppos = pi/len2; pi and len2 are both Ints, so dividing them gives you an Int. To convert to a Double, write ppos = fromIntegral (pi/len2). (Type :t fromIntegral in ghci to see what else fromIntegral can be used for.)
You mean pi / fromIntegral len2, right? -- Felipe.

On 10/30/07, Felipe Lessa
On 10/30/07, Tim Chevalier
wrote: ppos = pi/len2; pi and len2 are both Ints, so dividing them gives you an Int. To convert to a Double, write ppos = fromIntegral (pi/len2). (Type :t fromIntegral in ghci to see what else fromIntegral can be used for.)
You mean pi / fromIntegral len2, right?
You're right, that's what I meant. Sorry for the confusion. Cheers, Tim -- Tim Chevalier * catamorphism.org * Often in error, never in doubt "Unfortunately, there is no algorithm for making human relationships work." -- Robin Williams

On 10/30/07, Tim Chevalier
On 10/30/07, noa
wrote: Hi!
I have the following function:
theRemainder :: [String] -> [String] -> Double theRemainder xs xt = sum( map additional (unique xs) ) where additional x = poccur * (inf [ppos,pneg]) --inf takes [Double] where xsxt = zip xs xt pi = countPos xr -- countPos returns an Int ni = (length xr) - pi len = length xs len2 = length xr ppos = pi/len2 -- THESE ARE THE PROBLEM pneg = ni/len2 -- THESE ARE THE PROBLEM poccur = (pi+ni)/len xr = (filter ((\y -> (fst y)==x)) (xsxt))
And I am getting this error message with ghc:
matrix.hs:54:31: Couldn't match expected type `Double' against inferred type `Int' In the expression: ppos In the first argument of `inf', namely `[ppos, pneg]' In the second argument of `(*)', namely `(inf [ppos, pneg])'
How can I change the declaration of ppos nad pneg so they are treated as Double for the inf function?
ppos = pi/len2; pi and len2 are both Ints, so dividing them gives you an Int. To convert to a Double, write ppos = fromIntegral (pi/len2). (Type :t fromIntegral in ghci to see what else fromIntegral can be used for.)
You can't divide Ints with (/) at all -- they aren't Fractional. You'll probably want to either fromIntegral both pi and len2 or use div for integer division. (Also, pi is a bit of a confusing name; you may want to consider using another one.) Shachaf

On Tue, 30 Oct 2007, noa wrote:
I have the following function:
theRemainder :: [String] -> [String] -> Double theRemainder xs xt = sum( map additional (unique xs) ) where additional x = poccur * (inf [ppos,pneg]) --inf takes [Double] where xsxt = zip xs xt pi = countPos xr -- countPos returns an Int ni = (length xr) - pi len = length xs
-> genericLength
len2 = length xr ppos = pi/len2 -- THESE ARE THE PROBLEM pneg = ni/len2 -- THESE ARE THE PROBLEM poccur = (pi+ni)/len xr = (filter ((\y -> (fst y)==x)) (xsxt))
participants (5)
-
Felipe Lessa
-
Henning Thielemann
-
noa
-
Shachaf Ben-Kiki
-
Tim Chevalier