SICP Exercise 4.5 cond support for test => recipient

Exercise 4.5.  Scheme allows an additional syntax for cond clauses, (<test> => <recipient>). If <test> evaluates to a true value, then <recipient> is evaluated. Its value must be a procedure of one argument; this procedure is then invoked on the value of the <test>, and the result is returned as the value of the cond expression. For example

(cond ((assoc 'b '((a 1) (b 2))) => cadr)
      (else false))


returns 2. Modify the handling of cond so that it supports this extended syntax.

SOLUTION

The following example code shows how the extended syntax described in this problem can be represented using an 'if' structure:

(cond ((< a b) exp1)
    ((assoc 'b '((a 1) (b 2))) => single-argument-proc)
    ((< m n) exp2)
    (else exp3))

is equivalent to:

(if (< a b)
    exp1
    (if (not (eq? (assoc 'b '((a 1) (b 2))) false))
        (single-argument-proc (assoc 'b '((a 1) (b 2))))
        (if (< m n)
            exp2
            exp3
        )
    )
)

I have implemented a new procedure 'special-cond-clause-syntax?' that determines if the cond-clause supplied to it is of the special form or not. I also added another procedure called 'recipient-proc' that extracts the name of the recipient proc from the clause. 

Then I use the above two procedures in 'expand-clauses' to construct an equivalent 'if' statement.

The code and tests are here.

Comments

Popular posts from this blog

SICP Exercise 2.56 differentiation rule

SICP Exercise 1.28 (Miller-Rabin Test)

SICP Exercise 4.18 a alternative strategy for interpreting internal definitions