greghendershott / fear-of-macros

A practical guide to Racket macros
251 stars 27 forks source link

Small inconsistency in `reverse-me` macro in section 3.3 #41

Open s-truax opened 4 months ago

s-truax commented 4 months ago

I'm loving the guide, I just saw this small error in section 3.3. At the top of the section, reverse-me is defined and includes a call to datum->syntax with stx as the ctxt (lexical context) argument.

Let’s write a transformer function that reverses the syntax it was given: (define-syntax (reverse-me stx) (datum->syntax stx (reverse (cdr (syntax->datum stx)))))

(reverse-me "backwards" "am" "i" values) "i" "am" "backwards"

Later, at the end of section 3.3, the guide says

Finally we use datum->syntax to convert this back to syntax: (datum->syntax #f '(values "i" "am" "backwards"))

Here you can see that the ctxt argument is #f. This might be confusing for people reading the guide.

greghendershott commented 4 months ago

Thanks for the feedback.

In most of this section I'm writing out expressions like (datum->syntax #f '(values "i" "am" "backwards")) and letting the Scribble evaluator show the result. This is like entering expressions one by one in a REPL.

At this point there is no stx defined, as there is in the body of the real/full definition of reverse-me. So if I use that, it would be an error. Instead I just passed #f, and tried not to get side-tracked into a discussion of lexical context ... because that's a whole other ball of wax. :)

I suppose I could change the definition of reverse-me also to ignore stx and use #f for datum->syntax ... but I feel like that's setting a bad example. I think a better example is as-is, to use the provided, "input" syntax as the lexical context for something like datum->syntax. Usually that's the right thing to do (but it depends... so again, getting side-tracked is a worry.)

TL;DR: You're right that my step by step sketch differs here from the real/full definition. I appreciate it might confuse people. But the status quo might be less-worse than the alternatives I can think of.

Mainly in this section I'm trying to "de-mythologize" macros and show they're functions from syntax to syntax -- trying to focus folks on that, as the main idea to take away.