4.26 Exercise 4.26

Suppose that Ben Bitdiddle responds to this by saying that unless could still be defined as a special form when evaluating in applicative order. He is correct: You could define unless to be a syntactic transformation to the equivalent if expression that swaps the result expressions:

> (define-syntax unless
    (syntax-rules ()
      ((_ condition alternative consequent)
       (if condition
           consequent
           alternative))))
> (define (factorial n)
    (unless (= n 1)
      (* n (factorial (- n 1)))
      1))
> (factorial 10)

3628800

We’re lucky to be able to define special forms ourselves in Lisp, but this is not universally true. In many languages, the choice of evaluation strategy is something you can only get around by the crudest means, if at all.

In fact, one of the reasons why macros in Lisp are so useful is because they let us simulate lazy evaluation. One could easily argue that using lazy evaluation would be a reasonable substitute for macros in many cases. This would also have the benefit of allowing contructions like unless to be first-class values – note that it is impossible to pass unless, if, or any other special form as an argument, for example, because they aren’t actually values. If you try to evaluate them by themselves, you’ll get a syntax error.