code as data is what makes lisp macros so powerful.
In a c macro you have the power of a little macro language to manipulate
strings, in a lisp macro you have the power of lisp language to manipulate
lisp data structures. I'll elaborate.
When the compiler sees a call to a macro it passes all of the arguments to
the macro, before evaluating.
so in a call like
(dotimes (i 10)
(do-some-crazy-thing)
(print i))
the macro dotimes is passed the list (i 10) and the list
((do-some-crazy-thing) (print i))
the macro can then operate on them as data, using the full power of lisp. At
the end of all this, the macro returns another piece of data, a list, which
is then interpreted as a lisp form, code, and evaluated.
If lisp code wasn't the same as lisp data, macros would be a lot less
powerful as you would have to have two languages, one for manipulating lisp
data, and another for manipulating lisp code (this is what you get in c).
Hope this helps. Sorry the example isn't very enlightening, it's difficult
to illustrate the power of macros in (< 500 words) as they're very
complicated. If you'd like to know more* ... learn lisp
Now I will stop with the lisp advocacy.
*on lisp by Paul Graham is the bible on macros, but it's a little hard for
someone who's new to lisp, try practical common lisp by Peter Siebel, (both
are available free online).
···
On 9/27/05, Devin Mullins <twifkak@comcast.net> wrote:
Devin Mullins wrote:
> The big thing Lisp has that Ruby can't do is code-as-data. I wish I
> could provide a good example of how that might be used practically,
> but it's been quite a while since I touched lisp.
>
> Devin
Well, nobody's provided an example. The best I can do is provide more
explanation. Since code is just (blah blah (blah blah)) -- that is, just
a list -- and Lisp has the capability to delay evaluation (I think with
an '), you can pass code into another function and have that function
actually manipulate the internals of your code, and build some new code
out of it. You could, maybe, write something that stripped all the print
statements out at runtime... Crappy example...
Help! Is there a Lisper in the house?
Devin