[Haskell-beginners] Parsec simple question

Brandon Allbery allbery.b at gmail.com
Tue Feb 12 23:05:18 CET 2013


On Tue, Feb 12, 2013 at 4:38 PM, Sean Cormican <seancormican1 at gmail.com>wrote:

> name :: Expression
> name = ID "string"
>
> number :: Expression
> number = Num 123
>
> whileParser :: Parser Expression
> whileParser = whiteSpace >> expr8
>
> expr8 :: Parser Expression
> expr8 = name
> <|> number
>

You have defined name to be an Expression.  But <|> composes *parsers*, not
expressions.  To create a Parser Expression, you would be composing (Parser
Expression)s, not simply (Expression)s; so you don't want "name" to be
simply an Expression, but a Parser that parses an input String and produces
an Expression, something like

    name :: Parser Expression
    name = ID <*> identifier -- parse an identifier, wrap it in an ID

    number :: Parser Expression
    number = Num <*> integer -- parse an integer, wrap it in a Num

-- 
brandon s allbery kf8nh                               sine nomine associates
allbery.b at gmail.com                                  ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130212/664b7777/attachment.htm>


More information about the Beginners mailing list