私はとても眠いので、次のコードを書きました(混乱を示すために変更されました):
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]
maxFactorのタイプは次のとおりです。
maxFactor :: Integral ([a] -> a) => ([a] -> a) -> [a] -> a
これは次のことを意味するのではありません:
- の最初の入力
facは型クラスでなければなりませんIntegral(例fac 10)。 - の定義には
maxFactor、がありfac x、xも型クラスでなければならないIntegralので、したがって、maxFactorの型はmaxFactor :: (Integral a) => a ->...のようなもので始まり、次に何か他のものになりますか?ただし、その場合は、の戻り値がまたはであるため、このコードがコンパイルされるのはなぜmaxFactorですかx。headこの推論の行に従うと、同じタイプではありません。
ここで何が欠けていますか?
事前にご入力いただきありがとうございます。