PLQ #11Done on:   Tuesday, November 26th

Question 1 @ 2024-11-26 18:29

We have seen how “mainstream” macros like

#define square(x) x * x

fail miserably. What’s the “root cause” of these failures?


Question 2 @ 2024-11-26 18:31

Why do we say that Racket/Scheme (and others) macros are “Hygienic”?


Question 3 @ 2024-11-26 18:33

Given the following alternative definitions of a while facility:

(define (while condition-thunk body-thunk)
  (when (condition-thunk)
    (body-thunk)
    (while condition-thunk body-thunk)))

(define-syntax-rule (while condition body ...)
  (when condition
    body ...
    (while condition body ...)))

Choose the true statement.


Question 4 @ 2024-11-26 18:37

Consider the following macro and use:

(define-syntax for
  (syntax-rules (= to do)
    [(for x = m to n do body ...)
    (letrec ([loop (lambda (x)
                      (when (<= x n)
                        body ...
                        (loop (+ x 1))))])
      (loop m))]))

(let ([i 9])
  (for i = 1 to (add1 i) do (printf "~s\n" i)))

This prints integers going up from 1 and never stops.

What’s the problem?