[Haskell-beginners] A problem with a simple csv file parser

Andy Elvey andy.elvey at paradise.net.nz
Sat Oct 16 03:09:45 EDT 2010


Hi Jimmy -

Thanks very much for that!  I'll try that - it sounds good to me!  
Bye for now -
- Andy 


Jimmy Wylie wrote:
>  Hi Andy,
>
> If I remember right, runhaskell is expecting a full fledged haskell 
> program with a "main" defined. Instead, you've sent it a module, which 
> it isn't expecting, so you're getting a parse error on the "where".  
> If you remove the "Module parseCSV where" from the top of your file, 
> and try again, you'll get a "not in scope 'main' " error.
>
> So you can test this a couple of ways.  An easy way would be to open a 
> terminal window and type "ghci", to open up ghc's Interpreter. Then, 
> at the prompt type:
>
> :l parseCSV.hs (or whatever the name of your file is)
>
> Now, ghci, has loaded up all the functions defined in your file. So 
> now, you can invoke your functions:
>
> *Main> parseCSV "a,b,c,d\n"
> Right [["a","b","c","d"]]
>
> If you're really adamant about using runhaskell, what you need to do 
> is define a main function that opens up a file, use hGetContents to 
> read the file as a String, and then invoke parseCSV on that String.
>
> In order to take command line arguments, you'll want this:
> http://haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/System-Environment.html#v:getArgs 
>
>
>
> Good Luck,
> Jimmy
>
> On 10/16/10 12:23 AM, Andy Elvey wrote:
>> Hi all -
>>
>>   I'm just trying to get the simple csv file parser from "Real World
>> Haskell" working.
>> However, I've got a problem. Here's the code  -
>>
>> --  A simple csv parser
>> Module parseCSV where
>> import Text.ParserCombinators.Parsec
>>
>> 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
>>
>> ( Btw, I'm running Ubuntu 10.04, GHC 6.12.1  )
>>
>> When I try to run this (using "runhaskell parseCSV.hs test.csv" ) I get
>> the error -
>> parseCSV.hs:4:16: parse error on input `where'
>>
>> This has me really puzzled - as far as I can see, the first line looks
>> identical to a number of similar
>> programs that I've found (while searching for a solution to this).  So,
>> if anyone can let me know
>> what is causing this, that'd be great!  Many thanks in advance -
>> - Andy
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



More information about the Beginners mailing list