在Haskell中,我有一些定义函数的问题,因为我的参数类型与所需的类型不匹配。为什么我收到类型错误?
例如,我想写一个函数,该函数需要n :: Int
并生成n
的平方根的从1到floor
的整数列表。因此,我将有一个功能,例如:
list :: Int -> [Int]
本来我所定义的函数,如下所示:
list :: Int -> [Int]
list n = [1 .. floor (sqrt n)]
当我加载sript,存在不匹配的类型的错误消息。但是,我不确定我是否与sqrt
函数或floor
函数的类型不匹配。错误消息如下:
No instance for (Floating Int)
arising from a use of 'sqrt' at pe142.hs:6:22-27
Possible fix: add an instance declaration for (Floating Int)
In the first argument of 'floor', namely '(sqrt n)'
In the expression: floor (sqrt n)
In the expression: [1 .. floor (sqrt n)]
Failed, modules loaded: none.
有人可以向我解释是什么导致错误以及如何修复?