Haskell 从openFile内容解析

Haskell 从openFile内容解析,haskell,syntax,Haskell,Syntax,这看起来很简单,但出于某种原因,我把自己弄糊涂了。“thing”行给了我错误信息。 解析函数正确(从RWH窃取)。我只是有一个类型错误 谢谢 import Text.ParserCombinators.Parsec import System.IO main = do csv_cont <- openFile "aCSV.txt" ReadMode csv_cont1 <- hGetContents csv_cont thing <- parseCSV c

这看起来很简单,但出于某种原因,我把自己弄糊涂了。“thing”行给了我错误信息。
解析函数正确(从RWH窃取)。我只是有一个类型错误

谢谢

import Text.ParserCombinators.Parsec
import System.IO

main = do 
   csv_cont <- openFile "aCSV.txt" ReadMode
   csv_cont1 <- hGetContents csv_cont
   thing <- parseCSV csv_cont1
   return () 


csvFile = endBy line eol
line = sepBy cell (char ',')
cell = many (noneOf ",\n")
eol = char '\n'

parseCSV :: String -> Either ParseError [[String]]
parseCSV input = parse csvFile "(unknown)" input
import Text.ParserCombinators.Parsec
导入系统.IO
main=do

csv_cont
parseCSV
是一个纯函数(注意,类型中没有
IO
)。所以你不用“do符号”来绑定它的结果。相反,只需常规
let
即可:

import Text.ParserCombinators.Parsec
import System.IO

main = do
   h <- openFile "aCSV.txt" ReadMode
   s <- hGetContents h
   let thing = parseCSV s
   print thing


csvFile = endBy line eol
line    = sepBy cell (char ',')
cell    = many (noneOf ",\n")
eol     = char '\n'

parseCSV :: String -> Either ParseError [[String]]
parseCSV s = parse csvFile "(unknown)" s
import Text.ParserCombinators.Parsec
导入系统.IO
main=do
h全-(变形)-圆。我从你的书中偷了CSV解析代码,然后你帮我修复了我在下面5行中搞砸的地方:)谢谢。