2017-09-11 HW

posted Sep 11, 2017, 5:56 AM by Samuel Konstantinovich


Goal: Infix, prefix, and postfix

Infix notation:
<value1> <operator> <value2>
examples:  
2 + 3  
5 * 2

Prefix notation
<operator> <value1> <value2>
examples:  
+ 2 3     
* 5 2

Postfix notation
<value> <value> <operator>
examples:  
2 3 +      
5 2 *

Scheme notation
Prefix with required the parenthesis: before the operator and after the final operand:
(<operator>  <value1> <value2>)

Some operatiors suppor more than 2 values:
 (<operator>  <value1> <value2> ... <valueN>)

Converting 
converting from infix to prefix requires you to know the order of operations. 
2+ 3 * 4    ->  (+ 2 (* 3 4))
2 * 3 + 4  ->  (+ (*2 3) 4)

Think about where the parenthesis could go for infix to help you convert:
2 * 3 + 4   is really (2 * 3) + 4  which converts more easily.

Be careful when converting... because Scheme has no order of operations, but infix does. You must pay attention to this.

If you want a mechanical way of doing this here:
(4+5) / 6 * 2        Start with this tricky example
(((4 + 5) / 6) * 2)  Add more parenthesis to remind yourself of the order of operations
(* (/ (+4 5) 6) 2)   Now move the operators to the beginning of the set of parenthesis that contains them.


Dr Racket:

Running it
Using it to:
  -evaluate a single line
  -evaluate multiple things
  -Save files

Homework : (Self-assessment so you know you got everything so far)
Test DrRacket at home. 
Test the examples from class in DrRacket,  by evaluating them in the program, and then tracing through it using the stepper.
Make sure the way you evaluate an expression is the same way that DrRacket traces through it. This is open ended... the people that aren't 100% confident should make a few extra attempts at correcting their way of thinking. If you are confident, you should verify that you are correct.

Comments