SICP Exercise 1.4 – Compound Expressions As Operators

The exercise asks us to describe the behavior of the given procedure:

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

The return value of the procedure can be described as follows

LaTeX for a-plus-abs-b

This is because the expression (if (> b 0) + -) evaluates the predicate (> b 0) and if it is true, returns the operator +, else, returns the operator -.

3 thoughts on “SICP Exercise 1.4 – Compound Expressions As Operators

      • Random Human says:

        Show some basic code manipulations?

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

        to

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

        to

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

        OT: Scheme programmers use 2-space tabs normally.

Leave a comment