On Sat, Jul 18, 2009 at 1:22 AM, Meikel Brandmeyer <m...@kotka.de> wrote:
> Using eval is not really a solution. > > (def foo '[a 1 b 2]) > (let-coll foo ...) > > will probably work with eval, but > > (let [foo '[a 1 b 2]] > (let-coll foo ...)) > > will not. > No, for that you need to make the "macro" run-time, too. This can be done by turning it into an ordinary function -- change "defmacro" to "defn" and wrap the return value in "eval". The argument now doesn't need to be "eval"d, but the body now needs to be quoted. To fix the latter, you can create a macro to wrap the function call that quotes the body. The function then builds the form to be evaluated from the passed-in quoted body and other argument and calls "eval" on this. The downside to this is that the function now has a run-time computation cost, and won't work in a deployment situation that doesn't support "eval". (Thus far, with Clojure I'm not aware of any, but a stripped-down "micro edition" for use on phones might exist in the future where this wouldn't work.) On the other hand, run-time access to "eval" allows a few very powerful tricks that are otherwise unavailable, including some very interesting ways of optimizing computations on the fly. In fact, Clojure can grant all the power of self-modifying code, without (if you avoid eval-ing "def" forms) most of the usual pitfalls of same. The JIT-created code even benefits from the Hotspot JIT in turn, so it's exactly as powerful (and a lot easier! And more portable) as self-modifying x86 assembly code, which has a history of being used to run graphics demos on old hardware that many would have thought incapable of what was done with it. (See http://www.anioni.com/pauli/site1999/demoscene.html but this sort of thing long predates hardware 3D acceleration. These days, this sort of capability will be more of interest for getting maximum performance out of game or numeric code, or for AI research.) (Self-modifying assembly also has a history of being misused to write viruses and make them hard to detect. Clojure is, fortunately, not so useful for that. :-)) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---