I'm still rather new to Haskell and I've encountered a problem that probably shouldn't be too hard to solve but totally stumped me.
I've written a function:
maxFor l n = n * m * (m + 1) / 2 where m = l `div` n
The function belongs to a small module which loads without problems, but throws two errors that are embarrassingly cryptic to me, whenever I try to use the function after loading the module:
<interactive>:182:1:
No instance for (Integral a0) arising from a use of `maxFor'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Integral Int -- Defined in `GHC.Real'
instance Integral Integer -- Defined in `GHC.Real'
instance Integral GHC.Types.Word -- Defined in `GHC.Real'
In the expression: maxFor 999 5
In an equation for `it': it = maxFor 999 5
<interactive>:182:8:
No instance for (Num a0) arising from the literal `999'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Num Double -- Defined in `GHC.Float'
instance Num Float -- Defined in `GHC.Float'
instance Integral a => Num (GHC.Real.Ratio a)
-- Defined in `GHC.Real'
...plus three others
In the first argument of `maxFor', namely `999'
In the expression: maxFor 999 5
In an equation for `it': it = maxFor 999 5
I understand it's telling me my function is missing a proper type declaration, but I don't understand how to write it without the compiler nagging again. I've tried countless variants and that I don't understand the actual problem doesn't help me to solve the problem.
Part of the problem might be that I'm using (/)
and div
in one function, so the needed type declaration will probably contain Fractional
and/or Integral
, since:
(/) :: Fractional a => a -> a -> a
and:
div :: Integral a => a -> a -> a
But that's as far as I've got and I'm stuck. How would I write the type declaration for maxFor
? What am I doing wrong? Am I missing or overlooking something that should be obvious?
Any help and constructive feedback is greatly appreciated.
EDIT: I've found this answer on stack overflow, which already helps a little. I'd still appreciate help, but I will of course delete the question, if it turns out I'm being an idiot and end up being downvoted.