[Haskell-beginners] prefix instead of infix

Daniel Fischer daniel.is.fischer at web.de
Sun Jan 10 15:46:20 EST 2010


Am Sunday 10 January 2010 21:24:40 schrieben Sie:
> Hi Daniel,
>               I want to add a let expression to the program so as I can
> enter let x = y in z or something to that effect. I was going along the
> lines of using prefix as I cant use infix so I thought I would make it
> easier by using prefix notation. Probably a bad idea.
>
> John

If you want only one binding per let, I think

letExpr = do
    keyword "let" -- implementation of keyword is left as an exercise
    bind <- variable -- binding patterns like (x,y) is left for later
    -- maybe check for "=" ?
    rhs <- expression
    keyword "in"
    use <- expression
    return (Let bind rhs use)

expression = letExpr <|> expr

factor = between (char '(') (char ')') expression <|> atom

atom = variable <|> number

would work (untested).

>
> On Sun, Jan 10, 2010 at 8:14 PM, Daniel Fischer 
<daniel.is.fischer at web.de>wrote:
> > Am Sonntag 10 Januar 2010 20:33:40 schrieb John Moore:
> > > Hi,
> > >    Can anyone explain how to turn the code from infix to Prefix. I
> > > want to include a let statement below. I not sure of how prefix 
> > > works.
> >
> > Can you elaborate? I don't understand what it is you want to do.
> > Do you want to parse expressions in prefix notation (aka Polish
> > notation), such as +(3,4) (or, more Lispy, (+ 3 4))?
> > That'd be simple, but you wouldn't use buldExpressionParser for that
> > (in Polish notation, neither associativity nor precedence have a
> > meaning). Or do you want to add a parser for let-expressions to expr?
> >
> > > expr :: Parser Expr
> > > expr = buildExpressionParser table factor
> > >    <?> "expression"
> > > table :: [[Operator Char st Expr]]
> > > table = [[op "*" Mul AssocLeft, op "/" Div AssocLeft]
> > >        ,[op "+" Add AssocLeft, op "-" Sub AssocLeft]
> > >        ]
> > >  where
> > >    op s f assoc
> > >       = Infix (do{ string s; return f}) assoc
> > > factor :: Parser Expr
> > > factor = do{ char '('
> > >           ; x <- expr
> > >           ; char ')'
> > >           ; return x
> > >           }
> > >      <|> number
> > >      <?> "simple expression"
> > > number :: Parser Expr
> > > number = do{ ds <- many1 alphaNum
> > >           ; return (Val $ read ds)
> > >           }
> > >
> > > John




More information about the Beginners mailing list