[Haskell-cafe] Type inference problem with division (/)
catamorphism at gmail.com
Tue Oct 30 20:36:14 EDT 2007
On 10/30/07, noa <noandrews at gmail.com> wrote:
> I have the following function:
> theRemainder :: [String] -> [String] -> Double
> theRemainder xs xt = sum( map additional (unique xs) )
> additional x = poccur * (inf [ppos,pneg]) --inf takes [Double]
> 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:
> 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
Tim Chevalier * catamorphism.org * Often in error, never in doubt
"After three days without programming, life becomes meaningless." --
More information about the Haskell-Cafe