I have a little problem understanding how to do the following in Haskell:
Let's say I have a statement like these:
- a * (b + c)
- a + (b * c)
- a + (b * (c + d))
- a * (b + (c * d))
- etc .. and others.
I want to express these instructions in a tree and evaluate the result of each, for the initial I defined the following data structure:
data statement = Number Int
| Product statement statement
| Sum statement statement
deriving (Eq, Show)
For the example tree to work with, I used the following function:
a :: statement
a = Product (Number 2) (Sum (Number 5) (Number 1))
Now I want to build a treeResult function that gives me the result of my definition, but I don’t know how to approach this problem. The integer returned for the above statement must be 12.
, , "" int, .
treeResult :: statement -> Int
treeResult (Number a) = a
treeResult (Product (Number a) (Number b)) = a*b
treeResult (Sum (Number a) (Number b)) = a+b
, -, , , haskell, - , ?