4 ms·
Scheme has a way of defining macros that's supposed to be better than old-fashioned defmacro. These "hygienic macros" seemed to a lot of people (though not me)
by pg 19y ago
Scheme has a way of defining macros that's supposed to be better than old-fashioned defmacro. These "hygienic macros" seemed to a lot of people (though not me) to be a good idea when they were invented. They got into the Scheme standard then. I think people don't like them so much now, but once something gets into a standard it's impossible to get it out.
However (a) all the Scheme implementations I know of have implemented classic defmacro macros as well, and (b) if one hadn't, you could easily write it yourself. So in practice there's not much of a decision to make.
The Arc implementation you're using to read this is written on top of Scheme. Currently Mzscheme.
- Tichy 19y agoThanks for the info! I learned Scheme with a rather obscure implementation (LMU Scheme, from my university), so I wasn't aware of the Macro capabilities of Scheme. Edit: I just checked, LMU Scheme is based on R4RS and indeed has no Macros.
- omouse 19y agoHygienic macros do seem to be a good idea: "Hygienic macros are macros whose expansion is guaranteed not to cause collisions with existing symbol definitions." (from Wikipedia, http://en.wikipedia.org/wiki/Hygienic_macro http://en.wikipedia.org/wiki/Hygienic_macro) What are the disadvantages? Is there any reason to dislike defining syntax rules?
- abstractbill 19y agoOne reason I've heard is that sometimes you actually want those collisions (I haven't come across a good example though - anyone know of one?). For me the reason is just that defmacro is easier to read.
- pg 19y agohere's one: (defmacro aif (test then &optional else) `(let ((it ,test)) (if it ,then ,else)))
- abstractbill 19y agoAh, of course - thanks!
- edw 19y agoIn Scheme, the cases where one would use anaphoric IF can be handled using a COND clause with a "=>": (cond ((assoc 'key collection) => (lambda (it) (do-something-with it))) (else (alternative))) Alternatively, if you really wanted to create an AIF macro, you could use SYNTAX-RULES to create something that sugared up the above thusly: (aif it (assoc 'key collection) (do-something-with it) (alternative)) Or you could, going outside the standard, use SYNTAX-CASE (ugh, in R6RS) or one of the other non R5RS approaches to macros e.g. syntactic closures[1] that don't throw out the macro hygiene baby with the occasional slight inconvenience bath-water. I'm a bit of a reformed macrologist; if I get an idea for a cool macro, I try to see how far I can take a non-macrotic version and compare that to what the syntactically-sugared version would be like. Most of the time, I wind up not bothering with a macro. I put macros in the same category as EVAL: If you think you need to use a macro, think again. [1]: (http://community.schemewiki.org/?syntactic-closures http://community.schemewiki.org/?syntactic-closures)
- euccastro 19y agoI'm not much into macros yet, but while syntax-case is a R6RS feature, I thought there is a portable implementation that runs on top of R5RS? At least Chicken 2.6 provides that, and its implementor is viscerally against R6RS.
- edw 19y agoYes, SYNTAX-CASE has been around for quite a while, and there is a portable implementation that has just a few prerequisites beyond what R5RS provides. http://www.cs.indiana.edu/chezscheme/syntax-case/ http://www.cs.indiana.edu/chezscheme/syntax-case/
- brlewis 19y agoYou are right about cond, but don't forget that cond and other Scheme syntax can be implemented as hygienic macros: http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-10.html#%_sec_7.3 http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z... Macros are a build-your-own-language construct, but to a certain extent so are functions. I agree that one should try functions first, but would not go so far as to put macros in the same category as using eval.
- vegashacker 19y agoOne example is the magical introduction of special variables. Here's "aif", implemented in PLT Scheme: (require (lib "defmacro.ss")) (define-macro (aif a b c) `(let ((it ,a)) (if it ,b ,c))) (aif (* 10 10) (+ it 1) 'oops) ==> 101 The "aif" form makes it so you don't have to create a temporary variable name first -- the temp var is just assumed to be named "it". If you're thinking, "whatever, I don't need that" you are wrong. :) Somehow this turns into the most wonderful little line-saver. At my last company, we used it all over the place.
- abstractbill 19y agoYup, I've used aif a lot, but had never got around to implementing it for myself and so hadn't made the connection with variable capture.