SICP Exercise 1.4
Question
Observe that our model of evaluation allows for combinations whose operators are compound expressions. Use this observation to describe the behavior of the following procedure:
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
Answer
In Scheme, conditionals can be used not only for returning operands, but also operators, such as in this case +
or -
.
In the case of this particular program, a subtraction will be performed if b
is a negative number, and an addition if b
is a positive number.
The resulting expression will then be either (+ a b)
or (- a b)
.