Reputation: 8448
This seems so simple, but for some reason I'm confusing myself. The "thing" line is giving me the error.
The parsing functions are correct (stolen from RWH). I just have a type error.
Thanks!
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
Upvotes: 1
Views: 146
Reputation: 137947
parseCSV
is a pure function (note, no IO
in the type). So you don't use "do notation" to bind its result. Instead, just regular let
is appropriate:
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
Here, with more idiomatic naming and identation.
Upvotes: 3