我太困了,我写了以下代码(修改以显示混乱):
fac s = take 10 [s, s `mod` 1 ..]
maxFactor x = if (s == [])
then x
else head <-- this should be 'head x' instead of just 'head'
where s = fac x
但是,这个加载到 ghci(和编译)中就好了。当我执行maxFactor 1
时,它会抱怨(当然):
<interactive>:0:1:
No instance for (Integral ([a0] -> a0))
arising from a use of `maxFactor'
Possible fix:
add an instance declaration for (Integral ([a0] -> a0))
In the expression: maxFactor 1
In an equation for `it': it = maxFactor 1
<interactive>:0:11:
No instance for (Num ([a0] -> a0))
arising from the literal `1'
Possible fix: add an instance declaration for (Num ([a0] -> a0))
In the first argument of `maxFactor', namely `1'
In the expression: maxFactor 1
In an equation for `it': it = maxFactor 1
但是,我不明白这种行为:
fac
的类型是:
fac :: Integral a => a -> [a]
whilemaxFactor
的类型是:
maxFactor :: Integral ([a] -> a) => ([a] -> a) -> [a] -> a
这是否意味着以下内容:
- 的第一个输入
fac
必须是类型类Integral
(例如,fac 10
); - 因为在 的定义中
maxFactor
有fac x
, x 也必须是 typeclassIntegral
,因此,maxFactor
的类型将以maxFactor :: (Integral a) => a ->
... 开头,然后是其他的?但是,如果是这样的话,那么为什么这段代码会编译,因为maxFactor
can bex
or的返回head
,当遵循这条推理时,它不具有相同的类型?
我在这里想念什么?
感谢您提前提供任何输入!