Why are macros so powerful? 
Author Message
 Why are macros so powerful?

It seems that macros are something specially powerful in Lisp, but while
learning Lisp I haven't figure out why.

Is there any sharp difference between macros and functions in Lisp? Is
DEFMACRO more than a trick to wrap code into something compact but
without the need of functions calls? I suppose the C-model I have in
mind isn't suitable to understand Lisp macros, would you please point
out some hints to adjust it?

Regards,

-- Shin



Fri, 19 Jul 2002 03:00:00 GMT  
 Why are macros so powerful?

Quote:
> Is there any sharp difference between macros and functions in Lisp? Is
> DEFMACRO more than a trick to wrap code into something compact but
> without the need of functions calls? I suppose the C-model I have in
> mind isn't suitable to understand Lisp macros, would you please point
> out some hints to adjust it?

The point is that macros give you the ability to manipulate the
structured source form of the language in arbitrary ways, because they
make that source form available to Lisp itself.  So the Lisp macro
system allows you to write, in Lisp, completely arbitrary
source-to-source transformations of Lisp source code.  So you can do
anything from creating new control constructs in the language, to
creating your own language.

Contrast this to C where what is available to the macro system is
basically strings, and the macro language is incredibly rudimentary.

--tim



Fri, 19 Jul 2002 03:00:00 GMT  
 Why are macros so powerful?


Quote:
> It seems that macros are something specially powerful in Lisp, but while
> learning Lisp I haven't figure out why.

> Is there any sharp difference between macros and functions in Lisp? Is
> DEFMACRO more than a trick to wrap code into something compact but
> without the need of functions calls? I suppose the C-model I have in
> mind isn't suitable to understand Lisp macros, would you please point
> out some hints to adjust it?

Well, one thing to consider is the LOOP macro.  Imagine that you wanted
to write a new looping construct in C which had lots of options, so that
you had to parse the arguments to the macro in order to figure out what
you wanted the expansion to look like.  You couldn't do that using a C
macro.  You would need to hack the pre-processor.

Suppose you wanted to define C++ using only C and without having a
pre-processor.  Could you do it?  In Common Lisp, you could define an
object system (such as CLOS) just using the Lisp macros.  In fact, that
is how the PCL CLOS implementation is done.

The power of Lisp macros is that it allows you to use the full power of
the Lisp language to transform code -- it makes it easy for any user to
define a specialized language that helps them solve their problem in a
concise and easy to use form.  It doesn't require that you go outside
the language itself by using additional tools like YACC or a
pre-processor.  All of that can be done inside the language itself.

--



Fri, 19 Jul 2002 03:00:00 GMT  
 Why are macros so powerful?

Quote:
> It seems that macros are something specially powerful in Lisp, but while
> learning Lisp I haven't figure out why.
[]
> I suppose the C-model I have in
> mind isn't suitable to understand Lisp macros, would you please point
> out some hints to adjust it?

The C-model gives you half the picture.  Lisp macros, like C macros,
are about manipulating the literal representation of the arguments,
and the final result, whatever code it is, behaves as if you'd written
that directly.

Totally unlike C/C++ macros, Lisp macros are exactly as strong as
functions.  In fact, in Lisp a macro is basically a function whose
args aren't evaluated and whose return value is.

Often when I'm writing a complicated macro, I farm some of the work
off to helper functions.  There's no problem doing that.  I don't have
to do anything special to call the helpers.  The `defmacro' (as
opposed to `defun') is just an interface that says "eval my stuff
afterwards, not before", and internally, the macro can do anything a
function can.

C could never do that, it'd be completely unreasonable for that
language model.

--
Tom Breton, http://world.std.com/~tob
Not using "gh" since 1997. http://world.std.com/~tob/ugh-free.html



Sat, 20 Jul 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. LETBIND (why Tcl is more powerful than CL)

2. why why why oh why why baby

3. A macro involving two sub-macros - where the 2nd macro needs results from the first

4. Why am I getting bind errors?

5. ERROR 48 - Why am I getting it?

6. why am i getting processor stack fault error?

7. Why I am not enthusiatic about OO COBOL

8. Why I am (core) dumping C++ for OOCOBOL

9. Why am I getting a NaN?

10. Why am I creating named fonts if....

11. Anybody know why I am timing out

12. Why I am not reaching callbacks ?

 

 
Powered by phpBB® Forum Software