Evaluate arithmetic infix operators

Discussion of Scheme and Racket
Post Reply
dsjoka
Posts: 14
Joined: Thu Feb 10, 2011 1:30 pm

Evaluate arithmetic infix operators

Post by dsjoka » Wed May 08, 2013 1:52 pm

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.

dsjoka
Posts: 14
Joined: Thu Feb 10, 2011 1:30 pm

Re: Evaluate arithmetic infix operators

Post by dsjoka » Wed May 08, 2013 1:57 pm

Also for more complex cases like ( ((2+1) +3*4) * (5+6))

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Evaluate arithmetic infix operators

Post by nuntius » Wed May 08, 2013 9:55 pm

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.

Post Reply