[Haskell-cafe] Parsec beginners problem

Jim Burton jim at sdf-eu.org
Fri Apr 27 10:10:21 EDT 2007

I  have a couple of questions about my first use of Parsec, which is trying
to read morse code symbols from a string. I have a map of symbols:

import qualified Data.Map as M

morsemap = M.fromList [('A', ".-")
                       , ('Z', "--..")]

a string to parse, like 

test = "...---..-....-"

and a Parser to read a single morse letter:

morse1 :: GenParser Char st String
morseletter = try $ M.fold ((<|>) . string) (string "") morsemap

which I am hoping would be equivalent to

try (string ".-")
<|> (string "-.")
<|> string ""

but I don't know what the base of the fold should be (string "" is wrong I
suppose) - what is the unit of <|>? Is there a readymade combinator for

It fails anyway:

*Main> run morse1 test
parse error at (line 1, column 1):
unexpected "."
expecting ".-"

I suppose this is because the strings I'm looking for overlap but I thought
the use of try..<|> would avoid this error...?

View this message in context: http://www.nabble.com/Parsec-beginners-problem-tf3657821.html#a10219707
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

More information about the Haskell-Cafe mailing list