Eroxl's Notes
Evaluation

Evaluation of an expression in Racket can be thought of as a simplification to obtain values. For example Racket simplifies the expression (+ 1 1) to 2.

Sub-Expression Evaluation

Some simplifications require more than one step. For example (- 4 (+ 1 1)) which first simplifies to (- 4 2) and then simplifies again to 2. To determine which sub-expression is evaluated first Racket uses a depth-first, left-to-right evaluation strategy. This is an algorithm to say that Racket evaluates first the leftmost sub-expression.

Examples

Given the following code:

(define LOWER 10)
(define UPPER 20)

(define (foo x y)
  (if (and (> x LOWER) (< x UPPER))
      (- x (* y 2))
      (+ x (* y 3))))

The evaluation steps for the expression (foo (* 3 4) (+ 0 2)) looks as follows

1. (foo (* 3 4) (+ 0 2))
2. (foo 12 (+ 0 2))
3. (foo 12 2)
4. (if (and (> 12 10) (< 2 20))
      (- 12 (* 2 2))
      (+ 12 (* 2 3)))
5. (if (and true (< 2 20))
      (- 12 (* 2 2))
      (+ 12 (* 2 3)))
6. (if (and true true)
      (- 12 (* 2 2))
      (+ 12 (* 2 3)))
7. (if true
      (- 12 (* 2 2))
      (+ 12 (* 2 3)))
8. (- 12 (* 2 2))
9. (- 12 4)
10. 8