[Haskell-beginners] Runtime error “Could not deduce (Integral Float) arising from a use of..”

Jack Vice jack.vice at gmail.com
Thu Aug 23 10:26:05 UTC 2018


I am trying to calculate the standard deviation for each index in a list of
lists of floats. Using Ubuntu 18.04, ghc 8.0.2. I am getting the following
runtime error which I have googled and still don't understand what
"Integral Float" is exactly or even which parameter is causing the trouble.

*Main> let z = stdDev 0 2 y x
<interactive>:250:9: error:
• Could not deduce (Integral Float) arising from a use of ‘stdDev’
  from the context: Floating a
    bound by the inferred type of z :: Floating a => [a]
    at <interactive>:250:5-38
• In the expression: stdDev 0 (length (head (x))) y x
  In an equation for ‘z’: z = stdDev 0 (length (head (x))) y x

Code:

-- i is start index, l is length of each list, ms is list of means,
--    xs is Matrix
stdDev i l ms xs
     | i < l     = sqrt(fromIntegral(sumOfMinusMeans i (ms!!i) xs) /
                             fromIntegral(l)):(stdDev (i+1) l ms xs)
     | otherwise = []

--i is index, m is mean for the index
sumOfMinusMeans i m (x:xs)
     | xs == []     = (x!!i - m)**2
     | i < length x = (x!!i - m)**2 + (sumOfMinusMeans i m xs)
     | otherwise    = 0

Types:

*Main> :t stdDev
stdDev
  :: (Floating a1, Floating a, Integral a1) =>
     Int -> Int -> [a1] -> [[a1]] -> [a]

*Main> :t sumOfMinusMeans
sumOfMinusMeans :: (Eq t, Floating t) => Int -> t -> [[t]] -> t

Variables:

*Main> y
[380.0,1.0]
*Main> x
[[600.0,1.0],[400.0,1.0],[170.0,1.0],[430.0,1.0],[300.0,1.0]]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180823/a985c906/attachment.html>


More information about the Beginners mailing list