7.4.3 C:   Macros
On this page:
7.4.3.1 Python if
7.4.3.2 Loops
7.4.3.3 Automata
7.4.3.4 Submission
7.4.3 C: Macros

These problems get you warmed up writing your own macros in Racket.

7.4.3.1 Python if

Look up the truthy-falsy behavior of Python 3, and implement a python-if macro that has the same syntax as Racket’s but implements Python’s truthy-falsy behavior. There are lots of corner cases; be careful and test extensively!

Since the number of types of values is very large, we’re going to scope the problem by limiting it to the set of types you can meaningfully use in smol/hof.

Note as a language design issue that the more types there are in the language, the more a non-trivial truthy/falsy system has to consider. Think of this as a virtue of the systems in languages that are either Boolean-only (à la OCaml) or extremely simple (à la Racket: false is false, everything else is true).

7.4.3.2 Loops

Recall the different looping behavior we saw in the C: Loops assignment? You must implement two “loop” macros, one corresponding to each of Python’s and Racket’s behaviors. We will call these for-wrong and for-right, respectively. Both have the same syntactic structure:
(for-right id L B)
(for-wrong id L B)
where id is an identifier (the “loop variable”), L is an expression that evaluates to a list of values, and B is the body. The result in both cases is a list of the values produced by evaluating B with id bound to each of the values in L. Thus,
#lang racket
 
(require smol/hof/compat)
 
(define L1 (for-right x (list 1 2 4) (* x x)))
(define L2 (for-wrong x (list 1 2 4) (* x x)))
 
(test L1 '(1 4 16))
(test L2 '(1 4 16))
but if you instead build a closure in the loop body and evaluate that after the loop is over, you will get the behavior we saw in Loops.

You are welcome to use any of Racket’s constructs, such as for.

If you have the basic idea sketched out but need help with Racket constructs or with macro use syntax, by all means ask!

7.4.3.3 Automata

Finally, we strongly urge you to read this paper for another example of macros.

There is no work to turn in associated with this, but we trust you to read it anyway—we’ll refer to it in class and possibly on future homeworks. As you’re reading it, see whether you can spot the error before the paper provides the fix.

7.4.3.4 Submission

Form