From: F. Xavier Noria
Subject: Why are macros so powerful?
Date: 
Message-ID: <upda9sgifvrdtu26nnp8bkgputbngt4gi7@4ax.com>
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

From: Andrew Cooke
Subject: Re: Why are macros so powerful?
Date: 
Message-ID: <873t1h$cgd$1@nnrp1.deja.com>
In article <··································@4ax.com>,
  F. Xavier Noria <···@retemail.es> wrote:
> 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?

Hi,

On Lisp by Paul Graham will answer all these questions and give
examples.

Macros don't have to follow the same rules that functions follow.  I
find it helps to think of them as manipulating text rather than symbols.
For example, all arguments to a function are evaluated.  This is not
true of macros - the arguments of a macro are the "text" (actually,
lists of symbols) that the arguments match.  So you, as the macro writer
can control whether things are evaulated or not.

That sounds very dry.  As an example, I was writing code yesterday that
I wanted to branch at random.  Sometimes I wanted one block of code to
be executed, sometimes another.  I knew that the number of blocks and
the relative weights could change, and that I might want the relative
weights to be evaluated, rather than constants.  It was easy to write a
macro to add a new command, weighted-cond, to Lisp, so that I could
write code like:

(weighted-cond (10
                (print "this code executed 10 percent of the time"))
               (90
                (print "this code executed 90 percent of the time")))

or

(weighted-cond ((calc-wt-1 foo bar)
                (print "executed according to calculated weight"))
               (1
                (print "other weights are relative to this"))
               ((calc-wt-2 something-else)
                (print "otherwise this")))

This involved taking the weights, evaluating them all, and finding their
sum.  A random value between zero and the sum of the weights can then be
calculated and used to select the appropriate clause.  This is a fair
amount of work, all hidden away from the user who only needs to look at
the definition of weighted-cond once.

It couldn't be written as a function because the code block, as an
argument to a function, would be evauluated (giving an error in all
likelihood) before the function was called.

No doubt I am suffering from a newbie's over-entusiasm for macros, but I
find them (and the back-quote and comma substitution mechanism) to be
amazingly powerful, elegant, and useful.

As another example, I wanted to join some filters in a circuit.  I wrote
macros so that Lisp became a language in which circuits could be
described simply.  The macros converted this simple descriptin into a
series of simpler functions that generated code to emulate the circuit.

Cheers,
Andrew

PS If you know C++, then you might think of macros as something vaguely
like templates.  There's nothing like it in C, unless you wrote C code
to generate new C code, that was then read back into the compiler to
make a library that could be called from the old C code....


Sent via Deja.com http://www.deja.com/
Before you buy.
From: Thomas A. Russ
Subject: Re: Why are macros so powerful?
Date: 
Message-ID: <ymin1pm12er.fsf@sevak.isi.edu>
In article <··································@4ax.com>,
   F. Xavier Noria <···@retemail.es> wrote:
> 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.

-- 
Thomas A. Russ,  USC/Information Sciences Institute          ···@isi.edu    
From: Tim Bradshaw
Subject: Re: Why are macros so powerful?
Date: 
Message-ID: <ey3ya96a1sb.fsf@cley.com>
* F Xavier Noria wrote:

> 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
From: Tom Breton
Subject: Re: Why are macros so powerful?
Date: 
Message-ID: <m3ya95ojg3.fsf@world.std.com>
F. Xavier Noria <···@retemail.es> writes:

> 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