为什么这个Haskell代码没有编译?

时间:2016-03-22 19:10:32

标签: haskell list-comprehension

我正在尝试计算Haskell中数字的因子列表。这是我目前的代码:

factors :: Int -> [Int]
factors n = map floor [x | x <- [2..sqrt n], n `mod` (floor x) == 0]

然而,当我尝试运行它时,它会抛出这个错误:

largestprimefactor.hs:4:17:
No instance for (RealFrac Int) arising from a use of `floor'
In the first argument of `map', namely `floor'
In the expression:
  map floor [x | x <- [2 .. sqrt n], n `mod` (floor x) == 0]
In an equation for `factors':
    factors n
      = map floor [x | x <- [2 .. sqrt n], n `mod` (floor x) == 0]

largestprimefactor.hs:4:37:
No instance for (Floating Int) arising from a use of `sqrt'
In the expression: sqrt n
In the expression: [2 .. sqrt n]
In a stmt of a list comprehension: x <- [2 .. sqrt n]
Failed, modules loaded: none.

我正在努力学习Haskell,所以这可能只是一个愚蠢的初学者错误,但我已经尝试解决这个问题几个小时但无法找到答案。

0 个答案:

没有答案