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.
Code:
Types:
Variables:
_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
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 <[hidden email]> wrote:
_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
In reply to this post by Jack Vice
Floating is the type class of types that are floating point. The two most common instances are Float and Double. Integral is the class of integer types. Most commonly Int and Integer. When you see a type like that it means type a1 is both a Floating and also an Integral. Intellectually that is impossible, but as far as ghc is concerned there could be a type that is an instance of both classes, so it allows it. But when you try to call it, there's no type in scope that you can affix to a1 to please it, so it will always error.The reason it has both Floating and Integral on the same type is that you are using several functions on various arguments of your function that imply that the types of those arguments must be instances of various classes. fromIntegral :: (Integral a, Num b) => a -> b sumOfMinusMeans :: (Eq t, Floating t) => Int -> t -> [[t]] -> t sqrt :: Floating a => a -> a -- (this function may not be a factor) I strongly recommend you write out a type for stdDev, but fix a1 to a concrete type and then ghc can tell you why that type won't work. stdDev :: Int -> Int -> [Double] -> [[Double]] -> [Double] • No instance for (Integral Double) arising from a use of ‘fromIntegral’ stdDev :: Int -> Int -> [Integer] -> [[Integer]] -> [Double] • No instance for (Floating Integer) arising from a use of ‘sumOfMinusMeans’ And then think really hard about what types you want stdDev to accept. Rework its the definition until the compiler is happy. I suspect it is an extraneous fromIntegral. On Thu, Aug 23, 2018 at 6:26 AM, Jack Vice <[hidden email]> wrote:
_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
David and Kyle, Thank you both. I declared the types and added a 'fromIntegral' and got things working! On Thu, Aug 23, 2018 at 8:35 AM David McBride <[hidden email]> wrote:
_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |