【发布时间】:2016-05-01 19:01:28
【问题描述】:
我正在尝试解决一个需要计算整数的“小”除数的问题。我只是对所有数字进行暴力破解,直到给定数字的平方根,所以要得到 10 的除数,我会写:
[k|k<-[1...floor(sqrt 10)],rem 10 k<1]
这似乎运作良好。但是只要我把它插入一个函数
f n=[k|k<-[1...floor(sqrt n)],rem n k<1]
实际上调用这个函数,我确实得到了一个错误
f 10
No instance for (Floating t0) arising from a use of `it'
The type variable `t0' is ambiguous
Note: there are several potential instances:
instance Floating Double -- Defined in `GHC.Float'
instance Floating Float -- Defined in `GHC.Float'
In the first argument of `print', namely `it'
In a stmt of an interactive GHCi command: print it
据我所知,将结果打印到控制台的实际 print 函数会造成问题,但我无法找出问题所在。它说type 是模棱两可的,但该函数显然只能返回一个整数列表。然后我再次检查了类型,f 的(推断)类型是
f :: (Floating t, Integral t, RealFrac t) => t -> [t]
我可以理解f应该能够接受任何实数值,但是谁能解释为什么return类型应该是Integral或int以外的任何东西?
【问题讨论】: