Haskell の型クラスで遊んで、要素がその型クラスのインスタンスではないリストの型クラスのインスタンスを作成しました。
class Three a where
three :: a -> [a]
instance (Three a) => Three [a] where
three x = [x, x, x]
整数のリストと文字のリストで異なるエラー メッセージが表示されます。
文字
*Main> three ['c']
<interactive>:11:1:
No instance for (Three Char)
arising from a use of `three'
Possible fix: add an instance declaration for (Three Char)
In the expression: three ['c']
In an equation for `it': it = three ['c']
整数
*メイン> 3 [1, 2]
<interactive>:12:8:
Ambiguous type variable `t0' in the constraints:
(Num t0) arising from the literal `1' at <interactive>:12:8
(Three t0) arising from a use of `three' at <interactive>:12:1-5
Probable fix: add a type signature that fixes these type variable(s)
In the expression: 1
In the first argument of `three', namely `[1, 2]'
In the expression: three [1, 2]
どうして??
(これで、Chars のエラーは理解されました。そのインスタンスはありません。しかし、Integers のあいまいなエラーは私にはわかりません。整数は既に何か (Num) のインスタンスであるため、そうではないのではないかと考えたので、作成しました別の任意の型クラスと Char のインスタンスですが、以前と同じ Char のエラーが発生しました)。
私はあなたの助けに感謝します