3.2 Solution for Exercise 7
When implementing a macro’s dynamic behavior with a function, an expression becomes a function; an expression to be evaluated in an extended environment becomes a function with arguments.
In andlet1, e2 is evaluated in the scope of the variable x. So the function for e2 has a single formal parameter: x.
(define-syntax-rule (andlet1 x e1 e2) (andlet1-fun (lambda () e1) (lambda (x) e2))) (define (andlet1-fun thunk1 fun2) (let ([tmp (thunk1)]) (if tmp (fun2 tmp) #f)))
Of course, this technique breaks down if the variable’s scope includes multiple expressions handled separately by the macro and if the expressions use set! to mutate the variable.