I'm having an issue I want to learn more about, and how to avoid. I've got this code
len :: (Num r ) => [a] -> r
len [] = 0
len xs = 1 + len ( tail xs )
avg :: (Num t) => [t] -> Double
avg xs = ( sum xs ) / ( len xs )
Which renders the following error
len.hs:6:9: Couldn't match expected type `Double' against inferred type `t' `t' is a rigid type variable bound by the type signature for `avg' at len.hs:5:12 In the expression: (sum xs) / (len xs) In the definition of `avg': avg xs = (sum xs) / (len xs)
Now, I know this error (thanks to irc.freenode.net#haskell) is a result of the division function
(/) :: (Fractional a) => a -> a -> a
However, I don't know what to do. My avg
function signature should have nothing to do with the division opperators quirks (requiring Fractional
typeclass). So, I'm left thinking the right way to overcome this is by casting to a type that impliments they Fractional
typeclass but I have no idea how, or even if this is right? Any ideas?