Evaluate arithmetic infix operators

Discussion of Scheme and Racket

Evaluate arithmetic infix operators

Postby 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

Postby dsjoka » Wed May 08, 2013 1:57 pm

Also for more complex cases like ( ((2+1) +3*4) * (5+6))
dsjoka
 
Posts: 14
Joined: Thu Feb 10, 2011 1:30 pm

Re: Evaluate arithmetic infix operators

Postby 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.
User avatar
nuntius
 
Posts: 498
Joined: Sat Aug 09, 2008 10:44 am
Location: Burlington, MA


Return to Scheme

Who is online

Users browsing this forum: No registered users and 1 guest

cron