#lang racket (define (modify-if trans code) ; locates every occurrence of an if expression in code and ; applies the transform function to it. (if (and (list? code) (not (null? code)) (eq? 'if (first code))) ; found an (if ...) expr, transform the parts (trans (second code) (third code) (fourth code) ) ; not an if, so if a ( ) then transform each of the parts ; otherwise leave alone (if (list? code) (map (lambda (c) (modify-if trans c)) code) code) )) (define c1 '(if true "c1-true" "c1-false" )) (define c2 '(if false (if (and true (if false true false)) "c11" "c12" ) "c21" )) ; transform (if c p q) into (if (not c) q p) (define (trans-1 c p q) (list 'if (list 'not c) q p) ) (define (test1) (displayln (eval c1)) (displayln (modify-if trans-1 c1)) (displayln (eval (modify-if trans-1 c1))) (newline) (displayln (modify-if trans-1 c2)) ) ; (test1)