Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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 - Chapter 14 - Anaphoric Macros is on page 189.



It'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).


Oh, 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.


They 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))


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


Hmm, 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.





Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

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

Search: