我有一個函數爲什麼Haskell中的字符串被識別爲(錯誤)類型[Char]?
mytest :: Int -> String
mytest = "Test"
ghci中拒絕加載該文件:
Couldn't match expected type ‘Int -> String’
with actual type ‘[Char]’
In the expression: "Test"
In an equation for ‘mytest’: mytest = "Test"
Failed, modules loaded: none.
一切正常,一旦我添加了一個通配符:
mytest :: Int -> String
mytest _ = "Test"
有誰知道爲什麼哈斯克爾解釋第一個爲[Char]
,第二個爲String
?
如果我理解正確的話,類型系統說:「我有一個'[字符]'這不是一個'詮釋 - > String'所以我只是拋出一個錯誤。」它甚至不檢查'[Char]'是否適合'String',因爲它已經知道它不正確? – Dominik
我試過'mytest = \ x - >「Test」',這也適用。 – Dominik
或者'myTest = const「Test」',因爲'const :: a - > b - > a'。 – utdemir