[Haskell-beginners] Parsing

Francesco Ariis fa-ml at ariis.it
Fri Apr 14 18:35:32 UTC 2017


On Fri, Apr 14, 2017 at 07:02:37PM +0100, mike h wrote:
> I have 
> data PackageDec = Pkg String deriving Show
> 
> and a parser for it
> 
> packageP :: Parser PackageDec
> packageP = do 
>     literal “package" 
>     x  <- identifier
>     xs <- many ((:) <$> char '.' <*> identifier)
>     return $ Pkg . concat $ (x:xs) 
> 
> so I’m parsing for this sort  of string 
> “package some.sort.of.name”
> 
> and I’m trying to rewrite the packageP parser in applicative style. As a not quite correct start I have

Hello Mike,

    I am not really sure what you are doing here? You are parsing a dot
separated list (like.this.one) but at the end you are concatenating all
together, why?
Are you sure you are not wanting [String] instead of String?

If so, Parsec comes with some handy parser combinators [1], maybe one of
them could fit your bill:

    -- should work
    packageP = literal "package" *> Pkg <$> sepEndBy1 identifier (char '.')

[1] https://hackage.haskell.org/package/parsec-3.1.11/docs/Text-Parsec-Combinator.html


More information about the Beginners mailing list