4

In Common LISP you can do the following

(macro lambda (x) (list (quote car) (list (quote cdr) x)))

It looks like this is not possible (an anonymous macro) in Clojure.

  1. Is this true?
  2. Why was this left out?
hawkeye
  • 34,745
  • 30
  • 150
  • 304
  • 2
    I won't try to answer, as I'm not a Common Lisp guy, and I don't Clojure well enough to comment. I am curious what this is useful for though. Are you planning on passing it to a higher order macro? – xscott Nov 02 '10 at 05:23
  • 5
    MACRO is not defined in ANSI Common Lisp. Where does it come from? – Rainer Joswig Nov 02 '10 at 07:42

2 Answers2

4

There is some macrolet (or other things) in clojure.contrib.macro-utils. However it smells a little like a hack to me. Inclusion in clojure.core is on the TODO radar, but doesn't have high priority at the moment.

EDIT: See also here http://dev.clojure.org/display/design/letmacro.

kotarak
  • 17,099
  • 2
  • 49
  • 39
1

It is my understanding (from reading the documentation and the source) that Clojure currently (as of version 1.2) only supports named macros defined with defmacro.

It's possible however that you can abuse the internals of Clojure to get some sort of anonymous macro by directly calling the Java function Var.setMacro() on some anonymous var. This is what defmacro uses under the hood to construct macros.

e.g. you can do:

(def macro-var (var (fn [...] ...)))

(.setMacro macro-var)

And the Clojure reader will subsequently interpret the var contained in macro-var as a macro. However you'll have to figure out how to define everything correctly and it might all break in future versions of Clojure.

Clojure 1.3 (currently in alpha, but fully operational) brings various other enhancements, e.g. symbol macros, so might also be worth a look.

mikera
  • 105,238
  • 25
  • 256
  • 415
  • Maybe this worked for Clojure 1.2, but more recent versions of Clojure don't [seem to] support anonymous var's. The `var` special form expects a symbol as its only argument, not an arbitrary Clojure expression. Although, you've got me thinking: if under the hood that's all macros are, it sounds like you could treat macros as values, which could allow for a lot of interesting applications, including anonymous macros, curried macros, and more. Hmm... – Jeff Terrell Ph.D. Apr 26 '14 at 17:38