[racket] Macros and literal-id
What is the reason for using syntax-rules instead of syntax-parse? (To maintain RnRs compatibility?)
RAC
On Aug 9, 2011, at 8:15 AM, Neil Van Dyke <neil at neilvandyke.org> wrote:
> BTW, for future readers... in this example:
>
> (define-syntax if*
> (syntax-rules (then else)
> ((_ ?test (then ?consequent) (else ?alternate))
> (if ?test ?consequent ?alternate))))
>
> The "?" part of the pattern variables is just a naming convention of individual programmers. The "?" is the first character of the pattern variable identifier, not special syntax.
>
> I've tried various naming conventions here, including "?". Once my macro clauses got large, with a mix of a number of pattern variables as well as Racket identifiers to be captured, I settled on making the pattern variable identifiers all-upppercase:
>
> (define-syntax if*
> (syntax-rules (then else)
> ((_ TEST (then CONSEQUENT) (else ALTERNATE))
> (if TEST CONSEQUENT ALTERNATE))))
>
> This example is too small too appreciate the difference, with "if" being the only Racket identifier. Imagine you had a "syntax-rules" clause with block of code with 20 Racket identifiers in it, and 3 pattern variables. You'd want the pattern variables scattered throughout this to really stand out. At least on my screen, all-caps does that better than "?", and all-caps also takes up less horizontal space, which can get to be an issue with patterns that you're trying to keep to a single line for readability symmetries. Anyone else looking at the code will instantly see which convention you're using, so you can use whatever you want.
>
> I have a few more useful conventions for "syntax-rules", from which people can pick&choose depending on the situation and preferences, but no time today to type them. Typing them up might have to wait for pounding out a chapter of my tentative practical Racket book. (Not trying to sell anything; book will be free in Scribble format, probably as a PLaneT package that installs the book into your searchable local copies of Racket documentation.)
>
> --
> http://www.neilvandyke.org/
>
> _________________________________________________
> For list-related administrative tasks:
> http://lists.racket-lang.org/listinfo/users