[Haskell-beginners] Runtime error “Could not deduce (Integral Float) arising from a use of..”
Kyle Murphy
orclev at gmail.com
Thu Aug 23 12:10:35 UTC 2018
GHC has deduced that the third and fourth arguments of stdDev must be
members of both the Floating and Integral classes based on the functions
used on them. GHC has also deduced that the types of y and x are [Float]
and [[Float]] respectively. The error is that GHC has no definition in
scope that would make Float a member of the Integral class.
How to fix this is a bit more than I can go into right now, but will likely
involve carefully converting from type to type at particular points in
stdDev. As a first step, it's always a good idea to explicitly tell GHC
what YOU think the types are for a function, and then let it tell you where
it disagrees.
On Thu, Aug 23, 2018, 6:26 AM Jack Vice <jack.vice at gmail.com> wrote:
> 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]]
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180823/988e7a15/attachment-0001.html>
More information about the Beginners
mailing list