【发布时间】:2020-07-24 17:11:01
【问题描述】:
我正在尝试使用 Cassava 解析 CSV 文件。我想要一个函数,如果解析不成功则返回Nothing,否则返回Just (V.Vector (String, String, String))。
我正在使用下面的代码:
{-# LANGUAGE ScopedTypeVariables #-}
module Lib
( someFunc
) where
import qualified Data.ByteString.Lazy as BL
import Data.Csv
import qualified Data.Vector as V
type Dataset = (String, String, String)
someFunc :: Maybe (V.Vector Dataset)
someFunc = do
csvData <- BL.readFile "TAEE3.SA.csv"
case decode HasHeader csvData :: Either String (V.Vector (String, String, String)) of
Left a -> Nothing
Right v -> Just v
错误是:
• Couldn't match type ‘IO’ with ‘Maybe’
Expected type: Maybe BL.ByteString
Actual type: IO BL.ByteString
• In a stmt of a 'do' block: csvData <- BL.readFile "TAEE3.SA.csv"
In the expression:
do csvData <- BL.readFile "TAEE3.SA.csv"
case decode HasHeader csvData ::
Either String (V.Vector (String, String, String))
of
Left a -> Nothing
Right v -> Just v
In an equation for ‘someFunc’:
someFunc
= do csvData <- BL.readFile "TAEE3.SA.csv"
case decode HasHeader csvData ::
Either String (V.Vector (String, String, String))
of
Left a -> Nothing
Right v -> Just v
|
14 | csvData <- BL.readFile "TAEE3.SA.csv"
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
就像<- 函数根本不起作用。它不应该在IO a monad 中返回a 吗?
【问题讨论】:
-
这行不通,因为您试图在 Maybe monad 中执行 IO (readFile)。
<-也不是函数,它只是>>=的语法糖
标签: haskell monads do-notation