Hacker News new | past | comments | ask | show | jobs | submit login
A Scheme syntax-rules primer (2008) (willdonnelly.net)
56 points by mac01021 78 days ago | hide | past | web | favorite | 9 comments



Another one is Joe Marshall's "syntax-rules Primer for the Merely Eccentric": 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/


I 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...)


I 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.


If 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


Cool! 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.


Confused, why is the macro name necessary when the pattern itself already specifies the macro name?


Making define-syntax bind to a name that appears in the body would be confusing. You can omit the macro name in the pattern and use an underscore instead.


Does the name have any significance at all? The apparent lack of it is far more confusing...


very useful resource, I recommend it.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: