Exercise 1.1

The Question

Below is a sequence of expressions. What is the result printed by the interpreter in response to each expression? Assume that the sequence is to be evaluated in the order in which it is presented.

10
(+ 5 3 4)
(- 9 1)
(/ 6 2)
(+ (* 2 4) (- 4 6))
(define a 3)
(define b (+ a 1))
(+ a b (* a b))
(= a b)
(if (and (> b a) (< b (* a b)))
    b
    a)

(cond ((= a 4) 6)
      ((= b 4) (+ 6 7 a))
      (else 25))

(+ 2 (if (> b a) b a))

(* (cond ((> a b) a)
         ((< a b) b)
         (else -1))
   (+ a 1))

The Answer

We'll break this into chunks of each top-level expression. This will help us examine each expression in enough detail (though mainly to print each expression's result).

Arithmetic

The first few expressions are simple arithmetic operations, whose results are printed upon evaluation:

10
10
(+ 5 3 4)
12
(- 9 1)
8
(/ 6 2)
3
(+ (* 2 4) (- 4 6))
6

Variables

Then the variables a and b are defined:

(define a 3)
(define b (+ a 1))

and their values are used in some simple arithmetic and boolean expressions:

(+ a b (* a b))
19
(= a b)
#f

In the if expr, b is returned since b is greater than a and b is less than the product of a and b:

(if (and (> b a) (< b (* a b)))
    b
    a)
4

In the cond we get (+ 6 7 a) since b is equal to 4, which finally evaluates to 16:

(cond ((= a 4) 6)
      ((= b 4) (+ 6 7 a))
      (else 25))
16

In the last if we get b since b is greater than a, which is added to 2:

(+ 2 (if (> b a) b a))
6

Finally we get b again since a is less than b, which is substituted into the multiplication:

(* (cond ((> a b) a)
         ((< a b) b)
         (else -1))
   (+ a 1))
16

Comments