4 ms·
A Scheme syntax-rules primer (2008)
- amirouche 7y agovery useful resource, I recommend it.
- neilv 7y agoAnother one is Joe Marshall's "syntax-rules Primer for the Merely Eccentric": http://sites.google.com/site/evalapply/eccentric.txt http://sites.google.com/site/evalapply/eccentric.txt Today, instead of `syntax-rules`, you'll probably use `syntax-case`, which is a lot easier to use for harder purposes, and also easier error-checking, just not as interesting as `syntax-rules`. Or, if you're using Racket, `syntax-parse`. Greg Hendershott's "Fear of Macros" should be an easy introduction to some of the newer ways, though it has some Racket-specific bits in it: https://www.greghendershott.com/fear-of-macros/ https://www.greghendershott.com/fear-of-macros/
- widdershins 7y agoI still use `syntax-rules` a lot for simple macros - there's hardly any boilerplate and macros come out very readable without all the quasi-quoting, quasi-unquoting etc. that happens in other macro systems. But some things just can't be accomplished with it. I only turn to `syntax-case` for these things, like introducing new identifiers, or fully procedural tasks. It blew my mind when I discovered that I could write a macro that actually reads files from disk and binds variables to each path at macro-expansion time (not saying that's a great idea though...)
- neilv 7y agoI like `syntax-rules` and have done some crazy things with it. If I were going to teach someone syntax extension in Scheme or Racket, however, I'd just use `syntax-case` or `syntax-parse` for everything. That avoids confusing people with two different languages.
- mehrdadn 7y agoConfused, why is the macro name necessary when the pattern itself already specifies the macro name?
- deleted 7y ago[deleted]
- camoy 7y agoIf you're in Racket consider using syntax-parse instead. It's syntax-rules/syntax-case's more capable cousin: https://www2.ccs.neu.edu/racket/pubs/icfp10-cf.pdf https://www2.ccs.neu.edu/racket/pubs/icfp10-cf.pdf
- norswap 7y agoCool! I'd love a follow-up that shows how syntax-rules can do things that are out of reach for something like the C macro preprocessor.