[Haskell-beginners] Parsec, comma sperated list with special last element

Christian Maeder Christian.Maeder at dfki.de
Tue Dec 18 15:42:23 CET 2012


Hi,

you should not distinguish last or other cells.
Do you really want to discard the last cell in your result?

You could use:

   cell = many1 $ noneOf "\n,"

   endBy cell (oneOf "\n,")

This would give you all cells of a file.

To get all cells of a single line use:

   line = sepBy cell (char ',')

and skip the final newline. To get all lines of a file
use:

   endBy line (char `\n')

HTH Christian


you wrote: 2012-12-18 08:58:45 GMT

I tried

file = do
   res <- endBy (try cell) (char ',')
   l   <- lastCell
   eof
   return res

cell = many1 (noneOf ",")
lastCell = many1 (noneOf "\n")

But that does not work because cell succeeds on the last cell.
I can replace the endBy by

many (try $ do {a <- cell; string ","; return a})

Nathan



More information about the Beginners mailing list