What would you consider the normal way of solving this small problem of mine?
Christophe
> When I found that out, I was surprised. My knowledge of lisp comes
> from reading, in the past few months:
> - ANSI Common Lisp
> - Practical Common Lisp
> and, some years ago, a few more things.
The best book to read about macros is in my opinion Paul Graham's "On
Lisp", which is now available for free download:
http://www.paulgraham.com/onlisp.html
It covers mostly Common Lisp plus a bit of Scheme, but many of the
ideas carry over very well to Clojure.
> Even now that I hurt myself against this limitation, I'm still to make
> complete sense out of it. I understand that macros aren't known to
> functions because functions only get to see their expansions, but it's
> fuzzy in my mind how some functions interact well with macros while
> some others don't.
The fundamental restriction is that you cannot pass macros to
arguments as functions, and that is exactly what would be required to
do a reduce over and. The universal workaround is to define a
function that contains nothing but the macro, such as
(fn [x y] (and x y))
which can also be written in Clojure using the shorthand notation
#(and %1 %2)
Here the macro gets expanded inside the function *body*, but what you
pass around is the function *object*.
Konrad.
http://blog.thinkrelevance.com/2008/12/12/on-lisp-clojure
Stuart
That's not the only reason. Another reason is to have the option to
avoid evaluating all of the arguments. Functions always evaluate all
of their arguments. Macros don't always do that.
--
R. Mark Volkmann
Object Computing, Inc.
I don't think this is possible without some sort of support for a
macro/function duality in core Clojure. In the current implementation
(read: you should not rely on this fact) macros' are simply functions
with the #^{:macro true} metadata attached. Since you can't have a
symbol refer to two different values, I don't see any way to do this
except if you re-bind 'and' in a local context:
(let [and (fn and [x & xs]
(if x (if (nil? xs) true (and xs)) false))]
(apply and [true true false]))
Probably the closest you can come to this without redefining and is
Konrad's suggestion:
#(and %1 %2)
/mike.
Which is one way to describe what's provided by definline. It's
marked experimental, has an unusual format and proviso's, but it's
there get a blend of macro performance and function first-classness.
--Chouser