> But whilst this is useful, this doesn't really demonstrate why macros > are so powerful. Macros are useful because they automatically > rearrange your source code into something else. They're most similar > to the Ruby 'eval' function, but operate of data structures rather > than strings. > Nitpick, but... The function closest to ruby's eval would be 'eval'. Except clojure's eval of course operates on data structures. Macros are a feature of the Lisp evaluation model that allows you to intercept the evaluator and run your own code.
Macros have two differences to normal functions: 1) Arguments passed to them are *not* evaluated, and 2) They are run after read-time, but before compile-time, and their output is evaluated in place of the invocation. Point 1 means that you don't need a lot of quoting when passing "code literals" to macros, and point 2 is what allows these functions to be used for syntactic abstraction, as a macro form can inspect and transform its input parameters in any way imaginable, as long as the output is also valid Clojure code. The full power of macros takes a while to sink in. Because the entire language is available, one can for example use macros with Java reflection to programmatically produce Clojure wrappers for arbitrary Java methods, at compile time. Often you can solve a problem using higher order functions instead of macros, like Daniel did with Ruby, but at times you will be thinking "I wish I could just write..." and that's when you'll wish you had macros. :) -- Jarkko --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---