Exercise 1.4

Pre-exercise theory

Compound expressions represent the application of an operator to one or more operands.

For example (* 5 2) is a compound-expression as the operator (+) operates on the operands 5 and 2.

Compound expressions are used all the time in LISP. One example of non-compound expression is the evaluation of a literal number like typing 421 and hitting enter in a REPL.

The question

Observe that our model of evaluation allows for combinations whose operators are compound expressions. Use this observation to describe the behaviour of the following procedure:

(define (a-plus-abs-b a b)
        ((if (> b 0) + -)) a b
)

The if statement checks if b is greater 0, if so, it returns +, otherwise it returns -. This indicates which operation is going to be taken on the operands a b.

In LISP, the operators + and - are procedures themselves:

scheme@(guile-user) [9]> +
$59 = #<procedure + (#:optional _ _ . _)>
scheme@(guile-user) [9]> -
$60 = #<procedure - (#:optional _ _ . _)>

So they can be selected using conditional statements.