[racket] Macros and literal-id

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Tue Aug 9 10:15:22 EDT 2011

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/



Posted on the users mailing list.