我用Haskell编写了一个函数
toInt :: Float -> Int
toInt x = round $fromIntegral x
这个函数应该接受一个浮点数并返回相应的整数。我来自C编程背景,在C中,我们可以将它强制转换为int。
然而,在这种情况下,我会得到以下编译错误
No instance for (Integral Float)
arising from a use of `fromIntegral'
In the second argument of `($)', namely `fromIntegral x'
In the expression: round $ fromIntegral x
In an equation for `toInt': toInt x = round $ fromIntegral x
有没有任何解决这个问题的想法?
round 3.2 :: Int
。 - Aleph0Float -> Int
,那么你可以在Hayoo或Hoogle上搜索该类型的函数。 - Redu