Expression evaluation tree in Haskell -


in exam today asked create expression evaluation tree in haskell. answer simple as:

data expr = value integer           | add expr expr           | sub expr expr           | mul expr expr 

and evaluate it, use function such as:

eval :: expr -> integer eval (value x) = x eval (add l r) = eval l + eval r eval (sub l r) = eval l - eval r eval (mul l r) = eval l * eval r 

however today, given data type:

data op = add         | sub         | mul 

so assumed create expression tree do:

data expr = value integer           | op expr expr 

and use same eval function. however, have since written function , loaded ghci, not seem work. explain why doesn't work?

you must define data constructor (providing name)

data expr = value integer | compute op expr expr                             ^^^^^^^ 

then

eval :: expr -> integer eval (value x) = x eval (compute add l r) = eval l  + eval r 

and on.

:)


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -