1
我在哈斯克爾初學者和困惑這個代碼,我寫混淆IO,做記號
readRecords :: String -> [Either String Record]
readRecords path = do
f <- B.readFile path
map parseLogLine (C8.lines f)
但它給我這個錯誤:
Main.hs:15:10:
Couldn't match type `IO' with `[]'
Expected type: [C8.ByteString]
Actual type: IO C8.ByteString
In the return type of a call of `B.readFile'
In a stmt of a 'do' block: f <- B.readFile path
In the expression:
do { f <- B.readFile path;
map parseLogLine (C8.lines f) }
parseLogLine
的類型簽名parseLogLine :: B8.ByteString -> Either String Record
。
我完全驚訝。 B.readFile path
應返回IO ByteString
等f
應ByteString
。 C8.lines f
應返回[ByteString]
和地圖應return [Either String Record]
。
我在哪裏錯了?
非常感謝您,我將返回類型更改爲'IO [...]'並且如您所說的那樣在最後一條語句中使用return。 9分鐘後會接受你的回答。 – utdemir