I'm new to Haskell and I'm having trouble understanding exactly which type annotations or which type signature is needed to get this to work. This test
function itself is useless, but if I can understand how to get it to work, I think it'd help me to understand haskell types better.
test 0 = -1
test n = test (floor (n / 10))
This question seems to be related: Haskell: Why does RealFrac not imply Fractional?
But I can't figure out how to get my example to work. Here's the error I see when I run test 0
:
<interactive>:470:1:
Could not deduce (Integral a10) arising from a use of ‘test’
from the context (Num a)
bound by the inferred type of it :: Num a => a
at <interactive>:470:1-6
The type variable ‘a10’ is ambiguous
Note: there are several potential instances:
instance Integral Foreign.C.Types.CChar
-- Defined in ‘Foreign.C.Types’
instance Integral Foreign.C.Types.CInt
-- Defined in ‘Foreign.C.Types’
instance Integral Foreign.C.Types.CIntMax
-- Defined in ‘Foreign.C.Types’
...plus 28 others
In the expression: test 0
In an equation for ‘it’: it = test 0
<interactive>:470:6:
Could not deduce (Num a10) arising from the literal ‘0’
from the context (Num a)
bound by the inferred type of it :: Num a => a
at <interactive>:470:1-6
The type variable ‘a10’ is ambiguous
Note: there are several potential instances:
instance RealFloat a => Num (Data.Complex.Complex a)
-- Defined in ‘Data.Complex’
instance Data.Fixed.HasResolution a => Num (Data.Fixed.Fixed a)
-- Defined in ‘Data.Fixed’
instance forall (k :: BOX) (f :: k -> *) (a :: k).
Num (f a) =>
Num (Data.Monoid.Alt f a)
-- Defined in ‘Data.Monoid’
...plus 42 others
In the first argument of ‘test’, namely ‘0’
In the expression: test 0
In an equation for ‘it’: it = test 0