Evaluating Arithmetic Expressions
Order of Operations
Circles of Evaluation
Another way to show order of operations uses circles.
−
4
2
4 − 2
Circle Rules
- Each circle must have one function, which goes at the top.
- The numbers are written below the function, in order from left to right.
×
8
3
8 × 3
×
7
−
4
1
7 × (4 − 1)
×
+
6
9
−
4
1
(6 + 9) × (4 − 1)
Legal Expressions in Scheme
−
4
2
(− 4 2)
Rules for Legal Expressions
- Any value is a legal expression.
- Each open parenthesis is followed by one function, then by one or more legal expressions, and finally by a closing parenthesis.
×
8
3
(× 8 3)
×
7
−
4
1
(× 7 (− 4 1))
×
+
6
9
−
4
1
(× (+ 6 9) (− 4 1))
Functions, Names and Arguments
- Each function has an opening and closing parenthesis.
- Each function has a name.
- All the expressions that follow the function name are called arguments to the function.
Example:
For the expression (+ 4 3), the name of the function is +, and the arguments are 4 and 3.
Writing a Math Expression as a Scheme Legal Expression
- Draw a circle of evaluation for the math expression.
- Write the legal expression for the circle of evaluation.
Example:
Given the math expression 17 + (4 − 3)
The circle of evaluation is:
+
17
−
4
3
The legal expression is: (+ 17 (− 4 3))
Final Footnote - Three Ways of Writing Expressions
infix | 4 + 2 | Used in math class |
prefix | + 4 2 | Used in Scheme |
postfix | 4 2 + | Used in other programming |