3 ms·
Oh, it comes up all the time when doing pattern-matching, which many Lisp programs do a lot of. "Anaphoric" macros like AIF [0] are one solution: (awhen (fo
by ScottBurson 8y ago
Oh, it comes up all the time when doing pattern-matching, which many Lisp programs do a lot of. "Anaphoric" macros like AIF [0] are one solution:
(awhen (foo 3) (bar it))
Here IT is bound to the result of (FOO 3), provided that's nonnull. I don't really care for this style, but I see a lot of people using it. I have a macro I sometimes use for this purpose called BCOND ("binding COND"), e.g.:
(bcond ((let ((x (foo 3)))
x)
(bar x)))
The rule is that if the test-form of a BCOND clause is a LET form, the scope of the bindings is extended to the end of the clause. I'm not going to say it's beautiful, but it's quite general; in particular, an arbitrary predicate can be applied, not only a nonnull test, and can involve any or all of the bound variables. I wrote the first version of BCOND in 1980, so I think it's safe to say the need it attempts to address is felt with some frequency.
[0] https://www.common-lisp.net/project/anaphora/ https://www.common-lisp.net/project/anaphora/