[Haskell-cafe] Parser problem continued

Arthur Baars arthurb at cs.uu.nl
Tue Mar 15 10:53:38 EST 2005


The layout of your code is very important when writing haskell code:
Your code :
expr = do t <- term
           do symbol "+"
              e <- expr
              return e
           return (t + e)
        +++ return t

is equivalent to:

expr = do { t <- term
           ; do { symbol "+"
                ; e <- expr
                ; return e
                }
           ; return (t + e) -- e is not in scope
           }
        +++ return t -- t is not in scope
Both t and e are not in scope:
* e is in a nested do-block
* the expression 'return t' is outside the do-block


What you probably mean is:
expr = do t <- term
           do symbol "+"
              e <- expr
              return (t + e)
              +++ return t

which is equivalent to:
expr = do { t <- term
           ; do { symbol "+"
                ; e <- expr -- (dropped the return e line)
                ; return (t + e)
                }
                +++ return t
           }
Now t and e are in scope. The parser 'expr' will first recognize a 
'term' and then try to recognize a '+' symbol followed by an 
expression. If that fails it returns 't'.

Cheers,

Arthur


On 15-mrt-05, at 16:28, Nicola Whitehead wrote:

> Curiouser and curiouser...
>  
> expr :: Parser Int
> expr = do t <- term
>           do symbol "+"
>              e <- expr
>           return (t + e)
>        +++ return t
> solves the undefined variable problem but introduces a new 'Last 
> operator in do {...} must be an expression' error, which then 
> disappears if I explicitly return e
>  
> expr :: Parser Int
> expr = do t <- term
>           do symbol "+"
>              e <- expr
>              return e
>           return (t + e)
>        +++ return t
>
> to give me the original undefined variable t error at the line expr = 
> do t <- term . It looks in scope to me... :(
>  
> Thanks,
>  
> Nik
>  
> Dr Nik Freydís Whitehead
> University of Akureyri, Iceland
> *********************************************************************
> Having the moral high ground is good.
> Having the moral high ground and an FGMP-15 is better.
> *********************************************************************
>  
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list