syntax-symbol?


A macro for testing symbol expressions syntactically. Syntax-symbol? expands to ?sk if its first operand is a symbol and ?fk if not.

 (define-syntax syntax-symbol? 
   (syntax-rules () 
     ((syntax-symbol? (?x . ?y) ?sk ?fk) ?fk) 
     ((syntax-symbol? #(?x ...) ?sk ?fk) ?fk) 
     ((syntax-symbol? ?x ?sk ?fk) 
      (let-syntax ((magical-mystery-macro 
                    (syntax-rules () 
                      ((magical-mystery-macro ?x ??sk ??fk) ??sk) 
                      ((magical-mystery-macro ?y ??sk ??fk) ??fk)))) 
        (magical-mystery-macro the-walrus ?sk ?fk))))) 

This works by first pruning out lists & vectors (which may contain ellipsis) and then constructing a pattern to test ?x. If ?x is a symbol, the-walrus will match, and magical-mystery-macro will expand to ?sk; otherwise, it will expand to ?fk -- other possible values of ?x may include numbers or strings, which the-walrus will not match against.

Note that magical-mystery-macro is passed ?sk & ?fk, which it names differently. This is because syntax-rules does not create a closure; ?sk & ?fk would be inserted literally into the right-hand sides of magical-mystery-macro's syntax-rules clauses. If they contained, for example, ellipsis, they could throw off the magical-mystery-macro entirely.

This technique may be attributed to Oleg Kiselyov?, who has a full discussion on his website.


category-code