Different operating systems use different
characters to mark the end of line. Unix/Linux systems,
and Windows in text mode, use simply
"\n". DOS and Windows systems use
"\r\n", and Macs traditionally use
"\r". We could add support for
"\n\r" too, just in case
anybody uses that.
We could easily adapt our example to be
able to handle all these types of line endings in a single file. We
would need to make two modifications: adjust
eol to recognize the different endings, and
noneOf pattern in
cell to ignore
This must be done carefully. Recall that
our earlier definition of
'\n'. There is a parser called
string that we can use to match the
multicharacter patterns. Let’s start by thinking of how we would add
Our first attempt might look like this:
-- file: ch16/csv3.hs -- This function is not correct! eol = string "\n" <|> string "\n\r"
This isn’t quite right. Recall that the
<|> operator always tries the left alternative first. Looking for the
\n will match both
types of line endings, so it will look to the system that the following
line begins with
\r. Not what we
want. Try it in ghci:
let eol = string "\n" <|> string "\n\r"Loading package parsec-184.108.40.206 ... linking ... done.
parse eol "" "\n"Right "\n"
parse eol "" "\n\r"Right "\n"
It may seem like the parser worked for both endings, but actually looking at it this way, we can’t tell. If it left ...