更新时间:2023-11-10 20:31:10
Floating
类型,并且还要求两个操作数和结果都具有相同的类型.
The (**) :: Floating a => a -> a -> a
can only be applied to Floating
types, and furthermore requires that both operands and the result all have the same type.
您的类型签名则表示 x
是 Int
( Int
是 not Floating
类型类的成员),并且 x ** 0.5
应该返回 Float
,因此这违反了的类型签名.(**)
.
Your type signature on the other hand, says that x
is an Int
(an Int
is not a member of the Floating
typeclass), and furthermore x ** 0.5
should return a Float
, so this violates the type signature of (**)
.
我们可以转换 Num
类型类与
We can convert any type that is a member of the Integral
typeclass to any type that is a member of the Num
typeclass with fromIntegral :: (Integral a, Num b) => a -> b
. In this case fromIntegral
will thus convert an Int
to a Float
:
sqrt' :: Int -> Float
sqrt' x = fromIntegral x ** 0.5