4 ms·
PG has written about if-let, when-let, while-let and the likes in On Lisp, but calls them anaphoric macros instead. They are basically the same macros, but with
by JeanPierre 16y ago
PG has written about if-let, when-let, while-let and the likes in On Lisp, but calls them anaphoric macros instead. They are basically the same macros, but without the use of bindings (more brevity). You should take a look at it if you're interested.
http://lib.store.yahoo.net/lib/paulgraham/onlisp.pdf http://lib.store.yahoo.net/lib/paulgraham/onlisp.pdf - Chapter 14 - Anaphoric Macros is on page 189.
- alex_stoddard 16y agoAlso appears to be available online: http://www.bookshelf.jp/texi/onlisp/onlisp_15.html#SEC99 http://www.bookshelf.jp/texi/onlisp/onlisp_15.html#SEC99
- cgrand-net 16y agoIt's a design choice to value explicit naming (and easier nesting) over brevity. Anaphoric macros are not idiomatic in Clojure (there is only one: proxy), implicit introduction of locals is frowned upon (and you have to fight the hygienic macro system to create them).
- JeanPierre 16y agoOh, I am by no means against explicit naming. I'm just recommending people to read chapter 14 of On Lisp if they find if-let and when-let good macros they use often.
- felideon 16y agoThey are basically the same macros, but without the use of bindings (more brevity). So if-let and when-let are not really anaphoric. In Common Lisp, I use b-when and b-if that Kenny Tilton wrote: (defmacro b-when (bindvar boundform &body body) `(let ((,bindvar ,boundform)) (when ,bindvar ,@body))) (defmacro b-if (bindvar boundform yup &optional nope) `(let ((,bindvar ,boundform)) (if ,bindvar ,yup ,nope))) Usage: (b-when foo (plusp 1) (frob foo))
- Hexstream 16y agoI find your lack of parentheses disturbing. I'd personally prefer: (b-when foo (plusp 1) (frob foo)) These implementations also don't handle declarations correctly ;P A surprisingly tricky topic.
- felideon 16y agoHmm, did you mean... (bwhen (foo (plusp 1)) (frob foo)) bwhen is actually the first version of the macro, heh. It does seem a bit more idiomatic with parens, but I've gotten used to the other.