Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell将CSV文件绑定到句柄时出错_Csv_Haskell_Functional Programming_Io Monad_Do Notation - Fatal编程技术网

Haskell将CSV文件绑定到句柄时出错

Haskell将CSV文件绑定到句柄时出错,csv,haskell,functional-programming,io-monad,do-notation,Csv,Haskell,Functional Programming,Io Monad,Do Notation,因此,我为CSV文件编写了一个实用程序小工具箱,并在整个测试过程中手动绑定了该文件 table' <- parseCSVFromFile filepath 所以我不得不写一行简短的文字来去除这些垃圾 stripBare csv = head $ rights $ flip (:) [] csv 并将其重新定义为table=stripBare table'。在此之后,列表功能将处理csv文件的内容,并继续使用 (离题:令人惊讶的是,数据中没有直接的或a b->b函数。或。因此我使用了数据

因此,我为CSV文件编写了一个实用程序小工具箱,并在整个测试过程中手动绑定了该文件

table' <- parseCSVFromFile filepath
所以我不得不写一行简短的文字来去除这些垃圾

stripBare csv = head $ rights $ flip (:) [] csv
并将其重新定义为
table=stripBare table'
。在此之后,列表功能将处理csv文件的内容,并继续使用

(离题:令人惊讶的是,
数据中没有直接的
或a b->b
函数。或
。因此我使用了
数据。或。权利::[a b]->[b]

我想做的工作是一次脱去csv类型的衣服并将其绑定到一个手柄上。差不多

table = stripBare $ table' <- parseCSVFromFile filepath
对我大喊大叫说
do
块中的最后一条语句必须是表达式

我做错了什么

作为另一种好奇,我看到了这一点

用一种非常简单的方法在Haskell desugars中使用符号

do
  x <- foo
  e1 
  e2
  ...
我觉得这很吸引人,于是尝试了下面的一行,这给了我一个类型错误

*Toy> :type (parseCSVFromFile "~/.csv") >>= \x -> x

<interactive>:1:52: error:
    * Couldn't match type `Either
                             parsec-3.1.9:Text.Parsec.Error.ParseError'
                     with `IO'
      Expected type: IO CSV
        Actual type: Either parsec-3.1.9:Text.Parsec.Error.ParseError CSV
    * In the expression: x
      In the second argument of `(>>=)', namely `\ x -> x'
      In the expression:
        (parseCSVFromFile "~/.csv") >>= \ x -> x
*Toy>:键入(parseCSVFromFile“~/.csv”)>=\x->x
:1:52:错误:
*也无法匹配类型'
parsec-3.1.9:Text.parsec.Error.ParseError'
带“IO”
预期类型:IO CSV
实际类型:parsec-3.1.9:Text.parsec.Error.ParseError CSV
*在表达式中:x
在“(>>=)”的第二个参数中,即“\x->x”
在表达式中:
(parseCSVFromFile“~/.csv”)>>=\x->x
代码,例如

head $ rights $ flip (:) [] csv
这是危险的。您正在利用
head
的偏好来隐藏
csv
可能是
留下的东西的事实。我们可以把它改写成

= head $ rights $ (:) csv []
= head $ rights [csv]
= case csv of
     Left _  -> error "Left found!"
     Right x -> x
通常最好直接在
do
IO块中处理
Left
案例。类似:(下面是伪代码)

foo::String->IO()
foo filepath=do

表'不存在
a b->b
也就不足为奇了。如果你有一个
a
但没有
b
,你会产生哪个
b
?啊,是的,没错。允许在molbdnilo缺勤的列表
*Toy> :type (parseCSVFromFile "~/.csv") >>= \x -> x

<interactive>:1:52: error:
    * Couldn't match type `Either
                             parsec-3.1.9:Text.Parsec.Error.ParseError'
                     with `IO'
      Expected type: IO CSV
        Actual type: Either parsec-3.1.9:Text.Parsec.Error.ParseError CSV
    * In the expression: x
      In the second argument of `(>>=)', namely `\ x -> x'
      In the expression:
        (parseCSVFromFile "~/.csv") >>= \ x -> x
head $ rights $ flip (:) [] csv
= head $ rights $ (:) csv []
= head $ rights [csv]
= case csv of
     Left _  -> error "Left found!"
     Right x -> x
foo :: String -> IO ()
foo filepath = do
   table' <- parseCSVFromFile filepath
   case table' of
      Left err -> do
         putStrLn "Error in parsing CSV"
         print err
         moreErrorHandlingHere
      Right table -> do
         putStrLn "CSV loaded!"
         use table