2
import Data.Monoid
times :: Monoid a => Int -> a -> a
times i = mconcat . replicate i
main =
print $ times 5 5
此代碼提供了以下錯誤:爲什麼我在這裏得到「模糊類型變量」錯誤?
Ambiguous type variable `a0' in the constraints:
(Monoid a0) arising from a use of `times'
at :7:11-15
(Show a0) arising from a use of `print'
at :7:3-7
(Num a0) arising from the literal `5'
at :7:19
Probable fix: add a type signature that fixes these type variable(s)
In the second argument of `($)', namely `times 5 5'
In the expression: print $ times 5 5
In an equation for `main': main = print $ times 5 5
爲什麼它給這個錯誤? Num
甚至在這裏涉及?
謝謝,哈馬爾。我對代碼做了一些更改,現在我得到一個新的錯誤:http://paste.pocoo.org/show/486225/。 (這實際上應該是一個單獨的問題,如果您要求,我會創建一個新線程。) – missingfaktor
@missingfaktor:該實例需要「UndecidableInstances」擴展名。 – hammar
我的確在等待'Int'的Monoid實例,但不知道沒有一個實例。 – missingfaktor