Exercise

Translate the following expression into prefix form

$5 + 4 + (2 - ( 3- (6 + \frac{4}{5})))/ 3 \cdot (6-2)\cdot(2-7)$

Student Response

1
2
3
(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5)))))
   (* 3 (- 6 2) (- 2 7)))
; -37/150

Evaluation

explain the code

Let me break down this Scheme expression:

This is a complex mathematical expression that uses nested functions for arithmetic operations. Let’s solve it step by step from the inside out:

  1. First, let’s format it more clearly:
1
2
(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5)))))
   (* 3 (- 6 2) (- 2 7)))

Let’s solve it from inside out:

  1. (/ 4 5) = 4/5
  2. (+ 6 4/5) = 6.8
  3. (- 3 6.8) = -3.8
  4. (- 2 -3.8) = 5.8
  5. (+ 5 4 5.8) = 14.8 (this is the entire numerator)

For the denominator:

  1. (- 6 2) = 4
  2. (- 2 7) = -5
  3. (* 3 4 -5) = -60 (this is the entire denominator)

Finally: 14.8 / -60 = -37/150

The comment at the bottom shows the final result: -37/150

This is a good example of how Scheme uses prefix notation (where the operator comes before the operands) and how complex expressions can be built by nesting simpler ones.