我對Haskell的類型系統有一個相當普遍的問題。我試圖去熟悉它,我有以下功能:在Haskell中返回特定類型
getN :: Num a => a
getN = 5.0 :: Double
當我嘗試編譯,我得到以下錯誤:
Couldn't match expected type `a' against inferred type `Double'
`a' is a rigid type variable bound by
the type signature for `getN' at Perlin.hs:15:12
In the expression: 5.0 :: Double
In the definition of `getN': getN = 5.0 :: Double
據我瞭解這一點,函數設置爲「返回」類Num中的類型。雙是在這個類(http://www.zvon.org/other/haskell/Outputprelude/Num_c.html),所以我本以爲在這種情況下可以「返回」一個雙。
有人可以解釋這個嗎?
+1 - 我們可以寫出上面的函數類型爲'FORALL一個。 Num a => a' - >它必須爲任何*類型工作*。 – Dario 2010-01-12 13:39:41