为什么我得到一个"没有实例(分数a0)..."错误?

时间:2015-08-29 20:03:35

标签: haskell types compiler-errors

当我输入test 10时,以下代码无效:

test m = if m `mod` 2==0
             then m/2
             else m

它说:

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

对于整数test nn可能存在一些整数或双重类型问题,但我不知道错误是什么。

1 个答案:

答案 0 :(得分:7)

问题是AUTHORIZATION: <$token> AUTHORIZATION: Token <$token> AUTHORIZATION: Token token=<$token> 仅适用于整数类型,但mod仅适用于小数类型,因此无法实际使用/函数。

您可以这样做:

test

test m = if m `mod` 2 == 0 then m `div` 2 else m 是整数除法。)

或者这个:

div

或者这个:

test m | even m    = m `div` 2
       | otherwise = m