我在Haskell中编写了一个函数
toInt :: Float -> Int
toInt x = round $fromIntegral x
Run Code Online (Sandbox Code Playgroud)
它应该接受一个Float并返回等效的Int.我来自C编程背景,在C中,我们可以把它当作(int)x.
但是,在这种情况下,我得到以下编译错误
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
Run Code Online (Sandbox Code Playgroud)
有关如何解决此问题的任何想法?