2011-08-30

SICP Exercise 1.4: Compound Expressions as Operators

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))
The procedure a-plus-abs-b determines whether or not b is negative and uses this to determine whether to respectively subtract or add the value of b from a.

As subtracting a negative value is equivalent to adding the absolute value of that value, and adding a positive value is equivalent to adding the absolute value of that value, the behaviour of the procedure is equivalent to adding the absolute value of b to a - the behaviour documented by the procedure name.

No comments:

Post a Comment