有人能解释为什么这不起作用吗?
main = do
let a = 50
let y = 7
let area = (a ** y)
print (area)
print (a `mod` y)
我希望它能打印出来:
781250000000 -- 50 to the 7th power
1 -- remainder of 50/7
但相反,我得到了一系列模棱两可的类型错误,比如:
test.hs:2:13:
No instance for (Num a0) arising from the literal `50'
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'
简单;看看(**)
和mod
:的类型
Prelude> :t (**)
(**) :: Floating a => a -> a -> a
Prelude> :t mod
mod :: Integral a => a -> a -> a
这是一种罕见的数字类型,同时具有整数的特征和浮点数的特征。你有几个选择来处理这个:
- 使用幂运算可以很好地处理类似整数的数字。例如,
(^) :: (Integral b, Num a) => a -> b -> a
- 使用能够很好地处理浮点数的模运算。例如,
mod' :: Real a => a -> a -> a
- 在调用
(**)
之前,使用realToFrac :: (Real a, Fractional b) => a -> b
显式转换为浮点类型 - 在调用
mod
之前,使用floor :: (RealFrac a, Integral b) => a -> b
(或其他舍入函数)显式转换为类似整数的类型