homework help: upn parser

Max Hoffmann max.hoffmann at web.de
Sun Dec 14 13:12:42 EST 2003


Dear Haskell cafe members,
  I am somebody one could well call a bloody beginner but the elegance of
functional programming and am therefore truly interested to learn more about
Haskell.

Now I have an assignment to write a parsing function to convert a string of
a mathematical expression into a tree in UPN style to evaluate it
afterwards.
So my evaluating function looks like this:

data Op = Plus|Minus|Times|Div|Fac
     deriving(Show,Eq)
data OpTree = Term OpTree Op OpTree
            |Number Float
            |E
     deriving(Show,Eq)
eval(Number z) = z
eval(Term li op re)
     |op == Plus= (eval li) + (eval re)
     |op == Minus= (eval li) - (eval re)
     |op == Times= (eval li) * (eval re)
     |op == Div= (eval li) / (eval re)

And I tried to write a parsing function but I got stuck in the very
beginning. So far I wrote:

--parsing
parsing :: String->[OpTree]
parsing (a:b:c:xs) t
  |isDigit b = ((parsing xs)) ((Term (read a) c (read b)):t)

And the Hugs compiler gives me so many error messages, that I have no clue
how to get rid of this mess. Could anybody give me a hint where my error is
and whether I am right in the general direction.

Thanks a lot for any kind of help,
Max Hoffmann



More information about the Haskell-Cafe mailing list