Reputation: 629
The following code doesn't work when I type test 10
:
test m = if m `mod` 2==0
then m/2
else m
It says the following:
No instance for (Fractional a0) arising from a use of ‘it’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
instance Integral a => Fractional (GHC.Real.Ratio a)
-- Defined in ‘GHC.Real’
instance Fractional Double -- Defined in ‘GHC.Float’
instance Fractional Float -- Defined in ‘GHC.Float’
In the first argument of ‘print’, namely ‘it’
In a stmt of an interactive GHCi command: print it
It might be some integer or double type problems in test n
for integer n
, but I don't know what's wrong.
Upvotes: 1
Views: 321
Reputation: 85827
The problem is that mod
only works for integer types but /
only works for fractional types, so there's no way to actually use your test
function.
You can do this instead:
test m = if m `mod` 2 == 0
then m `div` 2
else m
(div
is integer division.)
Or this:
test m | even m = m `div` 2
| otherwise = m
Or this:
test m
| (d, 0) <- m `divMod` 2 = d
| otherwise = m
Upvotes: 7