Page 1 of 1

Evaluate arithmetic infix operators

PostPosted: Wed May 08, 2013 1:52 pm
by dsjoka
I need to write a program with the following criteria:

Returns either a value or the word "ERROR"
Returns a value if it is called with a list that contains:
A single number, or`
An "ADD" ("+") command or "MULTIPLY" ("*") command in infix notation where the two operands must be recursively evaluated by MyEval.


My question is how do you take an expression that looks like: (1 + 2) and turn it into (+ 1 2) in the code?

Thanks.

Re: Evaluate arithmetic infix operators

PostPosted: Wed May 08, 2013 1:57 pm
by dsjoka
Also for more complex cases like ( ((2+1) +3*4) * (5+6))

Re: Evaluate arithmetic infix operators

PostPosted: Wed May 08, 2013 9:55 pm
by nuntius
Here's the classic algorithm for parsing infix expressions.
http://en.wikipedia.org/wiki/Shunting-yard_algorithm

Exercise 2.58 in SICP takes a slightly different approach.