4 ms·
In Scheme, the cases where one would use anaphoric IF can be handled using a COND clause with a "=>": (cond ((assoc 'key collection) => (lambda
by edw 19y ago
In 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.
- edw 19y agoI thought a lot of Scheme implementations define IF in terms of COND. Is there some reason you're pointing this out? I should have been more detailed and noted that on the "by all means use it" vs. "if you think you need it, think again" continuum, EVAL requires IMO more justification than a macro. The source of my concern over macros is that when designing them, care needs to be taken to clearly communicate their semantics, which is best done IMO by following the established conventions of the language and whatever other macros the user is expected to be familiar with.
- brlewis 19y agoYou pointed out one could achieve much the same result as the aif macro by using cond. I wanted to point out that cond itself is an example of the power of macros.
- edw 19y agoMy point was more than one isn't usually missing much by being limited to writing hygienic macros. I don't think anyone is disputing the power of macros; it's the power/cost of hygiene violation that people are discussing. Someone could give a "trust the programmer" argument for making unhygienic macros available in a language, but that nostrum is problematic because there are two programmers: the macro writer and the macro user. It's harder than you think to write a macro using DEFMACRO that always behaves the way a user might expect it to behave, and as a user of a macro, you need to wonder how hard the macro writer worked to protect you -- something that I don't like as a fan of black boxes.
- procrastitron 19y agoI'm not familiar with the arguments against using eval. Can you enlighten me as to why it would be a bad practice?
- edw 19y agoBecause EVAL is an atomic bomb, and there's no need to use it when a garrote would more elegantly perform the task at hand.