From: Michael Hönisch
Subject: difference between macros and functions
Date: 
Message-ID: <41496111$1@news.adm.arcor.net>
Hello,

whats the difference between macros and functions.

Where should I use a macro and where functions?

Regards
Michael

From: jayessay
Subject: Re: difference between macros and functions
Date: 
Message-ID: <m3mzzq71zb.fsf@rigel.goldenthreadtech.com>
Michael H�nisch <················@ascad.de> writes:

> whats the difference between macros and functions.
> 
> Where should I use a macro and where functions?

Macros are a kind of function that (typically) runs at compile time.
They are intended to take code as their primary argument(s) and
transform that into some other code as output (return).  The returned
code is then inserted in place of the macro during the compilation.

An important part of this is that the arguments to macros are not
evaluated.  You can use any and all of Lisp (and any or your own code)
in writing them.

Take a look at:

http://www.gigamonkeys.com/book/macros-standard-control-constructs.html
http://www.gigamonkeys.com/book/macros-defining-our-own.html


/Jon

-- 
'j' - a n t h o n y at romeo/charley/november com
From: Duane Rettig
Subject: Re: difference between macros and functions
Date: 
Message-ID: <4mzzqz1a4.fsf@franz.com>
jayessay <······@foo.com> writes:

> Michael H�nisch <················@ascad.de> writes:
> 
> > whats the difference between macros and functions.
> > 
> > Where should I use a macro and where functions?
> 
> Macros are a kind of function that (typically) runs at compile time.

This statement is more correct than a previous poster's statement
(to which I gave a longish answer), but it contains the fodder for
the very misconception that that poster had (or at least stated):
It is too easy to read this statement without the parenthesized
qualifier which makes it correct.  A more complete statement that
would be less likely to be misunderstood would be "Macros are
a kind of function that runs at macroepand time, which occurs in
the early stages of the compilation or evaluation of a form."

-- 
Duane Rettig    ·····@franz.com    Franz Inc.  http://www.franz.com/
555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182   
From: jayessay
Subject: Re: difference between macros and functions
Date: 
Message-ID: <m3fz5i6uz5.fsf@rigel.goldenthreadtech.com>
Duane Rettig <·····@franz.com> writes:

> jayessay <······@foo.com> writes:
> 
> > Michael H�nisch <················@ascad.de> writes:
> > 
> > > whats the difference between macros and functions.
> > > 
> > > Where should I use a macro and where functions?
> > 
> > Macros are a kind of function that (typically) runs at compile time.
> 
> This statement is more correct than a previous poster's statement
> (to which I gave a longish answer), but it contains the fodder for
> the very misconception that that poster had (or at least stated):
> It is too easy to read this statement without the parenthesized
> qualifier which makes it correct.

Then people should read more carefully.  I put that "typically" in
there for just the reasons you state, but which will more than likely
go right over the head of the OP (being a newbie).  To get a more
complete understanding, the OP was pointed at a couple of links.


> A more complete statement that would be less likely to be
> misunderstood would be "Macros are a kind of function that runs at
> macroepand time, which occurs in the early stages of the compilation
> or evaluation of a form."

It's certainly more complete.  I very much doubt that it would do much
good for the OP (what is "macroexpand time" and "evaluation of a
form"?), so I suppose it is also less likely to be misunderstood as it
likely won't be understood in any sense at all.


/Jon

-- 
'j' - a n t h o n y at romeo/charley/november com
From: lin8080
Subject: Re: difference between macros and functions
Date: 
Message-ID: <414B4675.8C72423A@freenet.de>
jayessay schrieb:

> It's certainly more complete.  I very much doubt that it would do much
> good for the OP (what is "macroexpand time" and "evaluation of a
> form"?), so I suppose it is also less likely to be misunderstood as it
> likely won't be understood in any sense at all.

Reading this I think something should be changed there to make things
easy.
Assume, how does this look in 5 years? 
(Someone spend a mechanismn to a machinerie but no one can use it in
full)

stefan
From: John Thingstad
Subject: Re: difference between macros and functions
Date: 
Message-ID: <opsefffznjpqzri1@mjolner.upc.no>
On 16 Sep 2004 08:22:43 -0700, Duane Rettig <·····@franz.com> wrote:

> qualifier which makes it correct.  A more complete statement that
> would be less likely to be misunderstood would be "Macros are
> a kind of function that runs at macroepand time, which occurs in
> the early stages of the compilation or evaluation of a form."
>                                        ^^^^^^^^^^

Since we are nitpicking..
I would say the early stages of READING the form.
Thus when the form is evaluated all macro's have been expanded.
The order for 'evaluation' is Read Evaluate Print then Loop back to Read.  
(REPL)
For compilation macro-expansion happens before compiling the form.
There is a subtle difference between macro evaluation in compile and read.
When REPL is running the macro's are expanded while the code is read each  
time.
Thus if you change the macro the change is reflected the next time you run  
the code.
When the code is compiled it sees the macro at the time it is compiled.
If you later change the macro you will have to recompile all code that uses
that macro.
(This is one of the reasons defsystem needs to track dependencies in
  large code bases despite lisp development being incremental in nature.)

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
From: Duane Rettig
Subject: Re: difference between macros and functions
Date: 
Message-ID: <4isaeyx5p.fsf@franz.com>
"John Thingstad" <··············@chello.no> writes:

> On 16 Sep 2004 08:22:43 -0700, Duane Rettig <·····@franz.com> wrote:
> 
> > qualifier which makes it correct.  A more complete statement that
> > would be less likely to be misunderstood would be "Macros are
> > a kind of function that runs at macroepand time, which occurs in
> > the early stages of the compilation or evaluation of a form."
> >                                        ^^^^^^^^^^
> 
> Since we are nitpicking..
> I would say the early stages of READING the form.

The phrase "I would say" implies opinion.  Why spout opinion, when
you should be dealing in facts?

> Thus when the form is evaluated all macro's have been expanded.

Not true.  Now, there _are_ functionalities called "character macros",
sometimes called "reader macros", which are run at read time.
thus, these "macros" have been expanded by the start of the
eval process.  And these functionalities also add a lot of power
to the language,  However, they are usually assciated with syntax,
rather than macro concepts.  Those entities we normally just call
"macros" (i.e. objects created via defmacro) are not expanded
until eval time.  If you look at the spec, in this section:

http://www.franz.com/support/documentation/6.2/ansicl/subsubsu/consesas.htm

note first at the top of the page, the context we are in is part
of the chapter on compilation and evaluaton, in sections having to
do with evaluation. And the last sentence says: "How a form is
processed depends on whether it is classified as a special form,
a macro form, a functon form, or a lambda form."  So clearly, some
macro forms have not yet been processed at this point of evaluation.

> The order for 'evaluation' is Read Evaluate Print then Loop back to
> Read.  (REPL)
> 
> For compilation macro-expansion happens before compiling the form.

No, macro-excpansion occurs as a part of compilation.  See the minimum
requirements for compilation in

http://www.franz.com/support/documentation/6.2/ansicl/subsubse/minimalc.htm

> There is a subtle difference between macro evaluation in compile and read.
> When REPL is running the macro's are expanded while the code is read
> each  time.

This may be true, but is not a necessary condition.  A smart interpreter,
which can determine if the environment has changed for an annotated
macroexpansion, or an implementation which evaluates by compiling and
executing, will only macroexpand once under proper conditions.

> Thus if you change the macro the change is reflected the next time you
> run  the code.

This is correct, but it does not follow from the previous assertion.

> When the code is compiled it sees the macro at the time it is compiled.
> If you later change the macro you will have to recompile all code that uses
> that macro.
> (This is one of the reasons defsystem needs to track dependencies in
>   large code bases despite lisp development being incremental in nature.)

Yes, this is all true.

-- 
Duane Rettig    ·····@franz.com    Franz Inc.  http://www.franz.com/
555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182   
From: John Thingstad
Subject: Re: difference between macros and functions
Date: 
Message-ID: <opseim12oepqzri1@mjolner.upc.no>
>
> Not true.  Now, there _are_ functionalities called "character macros",
> sometimes called "reader macros", which are run at read time.
> thus, these "macros" have been expanded by the start of the
> eval process.  And these functionalities also add a lot of power
> to the language,  However, they are usually assciated with syntax,
> rather than macro concepts.  Those entities we normally just call
> "macros" (i.e. objects created via defmacro) are not expanded
> until eval time.

You are right of course. And I should know the difference between read  
macro's
and eval-time macro's..

(A bit embarrassed)
John
-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
From: Kaz Kylheku
Subject: Re: difference between macros and functions
Date: 
Message-ID: <cf333042.0409161006.592bc2d2@posting.google.com>
Michael H�nisch <················@ascad.de> wrote in message news:<··········@news.adm.arcor.net>...
> Hello,
> 
> whats the difference between macros and functions.
> 
> Where should I use a macro and where functions?

Macros are functions that write code. Functions are components of the
program.

Under Lisp, it can be hard to understand the difference, because in
the same memory image, running on the target platform, you can have
parts of the program being re-compiled and loaded, even as it is
running. The activities of developing the program and running it can
be happening at the same time. So macros are potentially shippable
code that can be invoked at the deployment site.

You can understand the difference more easily in terms of a
programming environment which separates the translation environment of
the program from its execution environment: you build the program on
some development machine, then ship a resulting image to the target
machine and run it there. Lisp readily supports this model of software
development and deployment.

Under this model, macros are invoked to write parts of the program
when it is being built from sources. The macros may be compiled to
machine code, but they don't necessarily go into the compiled image
that is shipped to the target machines. The macros are loaded into the
Lisp image on the development machine where compiling is taking place.

In some other languages, macro processing is done using a separate
language which is understood to work with a representation of the
program that is almost raw text. The processing takes place upfront
when the program is being compiled, and there is no possibility of any
traces of these macros surviving into the software image that is run
on the target. So there is no little chance for programmer confusion.

In Lisp, macros work with a representation of the program as a Lisp
data structure, and are written in the same language and have access
to the same language features as the rest of the program (in fact,
maybe even more language features, if the program is being
cross-translated for some embedded environment where only a subset of
Lisp is available).

Confusion can arise because Lisp macros are seamlessly woven into the
language, in a tangled hierarchy, and they closely resemble ordinary
functions, and can survive into the target image. Whenever you have
layers that are similar to each other, without a hierarchical
precedence among them, there is a potential for confusion about what
level you are in and how it fits into the big picture.
From: Pascal Costanza
Subject: Re: difference between macros and functions
Date: 
Message-ID: <cibojj$12i6$1@f1node01.rhrz.uni-bonn.de>
Michael H�nisch wrote:

> Hello,
> 
> whats the difference between macros and functions.
> 
> Where should I use a macro and where functions?

In Lisp, a function is similar to procedures and methods in other 
languages. A function is a runtime representation of code that you can 
invoke. In the general case, you can pass parameters to such functions 
and they can return one or more values. Unlike similar constructs in 
other languages, they don't need to be named (although they often are) 
and they know something about the environment in which they were 
created. This means that they can refer to the definitions (functions 
and variables) in their surrounding code.

Macros are usually not used at runtime. Instead, they describe how to 
transform some source code fragment into some other source code 
fragment. This is very powerful because it allows you to implement new 
abstractions that you can only implement by extending the programming 
language itself in other languages. Macros allow you to add new 
definitions (functions and variables) implicitly, rearrange source code 
that the user has written, and wrap it with additional functionality. 
Unlike C macros, Lisp macros are structural. This means that Lisp macros 
"know" about the structure of Lisp source code - how code is nested, 
what kind of definitions it uses, and so on - whereas C macros are just 
transformations from strings to strings, without further knowledge about 
the structure of the code.

When to use what is a question that can only be answered with 
experience. A general rule of thumb is that you should only use macros 
when you cannot succinctly express something as a function. A typical 
approach to programming in Lisp is by first writing down some examples 
of what your code should look like in the end, and then to try to 
implement a set of functions and macros to make that work. It's an 
iterative approach in which you switch several times between the high 
level (what you want) and the low level (how you implement it). In such 
an approach, it becomes clear by the time what you can and cannot 
implement as a function, what you need to implement as a macro, and 
whether you can refactor your code in order to simplify your original 
idea what the code should look like.

There are quite a few tutorials on programming in Lisp that try to get 
you into the mood of programming this way. 
http://alu.cliki.net/Education mentions a few resources.


Pascal

-- 
Pascal Costanza               University of Bonn
···············@web.de        Institute of Computer Science III
http://www.pascalcostanza.de  R�merstr. 164, D-53117 Bonn (Germany)
From: Duane Rettig
Subject: Re: difference between macros and functions
Date: 
Message-ID: <4vfeez343.fsf@franz.com>
Pascal Costanza <········@web.de> writes:

> Michael H�nisch wrote:
> 
> > Hello,
> > whats the difference between macros and functions.
> 
> > Where should I use a macro and where functions?

 [ ... ]

> Unlike C macros, Lisp macros are structural. This means
> that Lisp macros "know" about the structure of Lisp source code - how
> code is nested, what kind of definitions it uses, and so on - whereas
> C macros are just transformations from strings to strings, without
> further knowledge about the structure of the code.

Also unlike C code, macros can _execute_ any functionality at
macroexpand time, toward the production of the new code it will
eventually generate.   That means that a macro has the whole
power of the whole language to back it up, as well as the data
and program structures of the current application.

-- 
Duane Rettig    ·····@franz.com    Franz Inc.  http://www.franz.com/
555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182   
From: Paul Dietz
Subject: Re: difference between macros and functions
Date: 
Message-ID: <4149B5F6.52A7593C@motorola.com>
Duane Rettig wrote:

> Also unlike C code, macros can _execute_ any functionality at
> macroexpand time, toward the production of the new code it will
> eventually generate.   That means that a macro has the whole
> power of the whole language to back it up, as well as the data
> and program structures of the current application.

One implication of this is that what requires a preprocessing
program for C (for example, yacc or lex) can be done directly
within lisp using macros.

	Paul
From: Edi Weitz
Subject: Re: difference between macros and functions
Date: 
Message-ID: <874qlyfrcj.fsf@miles.agharta.de>
On Thu, 16 Sep 2004 11:40:45 +0200, Michael H�nisch <················@ascad.de> wrote:

> whats the difference between macros and functions.
>
> Where should I use a macro and where functions?

Have a look at

  <http://www.gigamonkeys.com/book/>

especially chapters 7 and 8. But the whole (not-yet-finished) book is
worth reading if you're new to Lisp.

Edi.

-- 

"Lisp doesn't look any deader than usual to me."
(David Thornley, reply to a question older than most languages)

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Jeff
Subject: Re: difference between macros and functions
Date: 
Message-ID: <R4g2d.59598$D%.36812@attbi_s51>
Michael H�nisch wrote:

> whats the difference between macros and functions.

Macros are executed at compile-time (and return code to be compiled).
Functions are executed at run-time. If you are familiar with C, it is
relatively the same difference between a function and a #define that
inserts code for you.

> Where should I use a macro and where functions?

As others have pointed out, experience will be the key here. However,
perhaps an example might help you. The first macro I ever implemented
was designed to relieve my fingers of typing. I wanted a list of
symbols to be enumerated constants (Lisp doesn't come with
enumerations). I wanted the following:

(defconstant a 1)
(defconstant b 2)
...
(defconstant z 26)

Quite a lot to type and upkeep, so I decided to make a simple macro
called DEFENUM that would do this work for me:

(defmacro defenum (lst &key (start 0) (increment 1))
  "Define an enumerated list of constants"
  (labels ((enum (lst value)
             (if (null lst) `(,value)
               (let ((x (car lst)))
                 (cons `(defconstant ,x ,value) 
                       (enum (cdr lst) (+ value increment)))))))
    `(progn ,@(enum lst start))))

Now I coudl just type:

(defenum (a b c ... z) :start 1 :increment 1)

And that's it. Once the connection was made about how macros really
worked and what could be done with them, comparing them to a #define in
C is an injustice.

Jeff

-- 
Progress in computer technology is waiting for people to stop
re-inventing the wheel.
 - Dr. William Bland (comp.lang.lisp)
From: Duane Rettig
Subject: Re: difference between macros and functions
Date: 
Message-ID: <4r7p2z24b.fsf@franz.com>
"Jeff" <···@nospam.insightbb.com> writes:

> Michael H�nisch wrote:
> 
> > whats the difference between macros and functions.
> 
> Macros are executed at compile-time (and return code to be compiled).
> Functions are executed at run-time. If you are familiar with C, it is
> relatively the same difference between a function and a #define that
> inserts code for you.

This is fast becoming the new Lisp Myth.  Why are people who
explain macros so careless as to talk about macros being
executed at compile time?  Well, it's true, but ...

<offense=MS employees: heard many years ago>
The old story goes that a man was lost in a hot-air balloon, and
drifting in the northwestern US, and he finally came to a tall
building and saw a man standing on the roof.  The man in the balloon
yelled out: "where am I?", and the man said "You're about 15 stories
above the ground."  To which the man in the balloon replied, "Oh,
this must be the MS support center".  The man on the roof answered
"why, yes, how did you know?", to which the balloon man answered
"Well, the information you gave me was correct, but useless".
</offense>

The fact is that macros are executed at compile-time, but not
exclusively.  The true situation is that macros are executed at
macroexpand time, which occurs in one of two possible places:
compilation time, and evaluation time.  Please take care when
giving explanations to be accurate.

This is not just a nit; note that you got the first sentence
"correct", but fell into your own trap by implying that by
contrast functions are executed at run-time (implying that
macros are not).  But in fact, macros are indeed executed
at run-time, as part of the macroexpansion that occurs before
the final execution of non-compiled code (whether that code happens
to be interpreted by an interpreter, or whether the implementation
interprets it by compiling it as an anonymous function).  So your
second sentence is incorrect, or at least misleading.  Someone
believing you will be surprised to trace the ececution of a macro
and find that the macro's code is indeed being executed at a time
when compilation is not taking place.

-- 
Duane Rettig    ·····@franz.com    Franz Inc.  http://www.franz.com/
555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182   
From: jayessay
Subject: Re: difference between macros and functions
Date: 
Message-ID: <m3brg66uuv.fsf@rigel.goldenthreadtech.com>
Duane Rettig <·····@franz.com> writes:

> <offense=MS employees: heard many years ago>
> The old story goes that a man was lost in a hot-air balloon, and
> drifting in the northwestern US, and he finally came to a tall
> building and saw a man standing on the roof.  The man in the balloon
> yelled out: "where am I?", and the man said "You're about 15 stories
> above the ground."  To which the man in the balloon replied, "Oh,
> this must be the MS support center".  The man on the roof answered
> "why, yes, how did you know?", to which the balloon man answered
> "Well, the information you gave me was correct, but useless".
> </offense>

This is an "IT" variation of a much older story about New Englanders
(or maybe Mainers in particular).  Just replace "tall building" with
"farm" and "MS support center" with "New England/Maine".


/Jon

-- 
'j' - a n t h o n y at romeo/charley/november com
From: Kenny Tilton
Subject: Re: difference between macros and functions
Date: 
Message-ID: <Mhj2d.120680$4h7.20817572@twister.nyc.rr.com>
jayessay wrote:

> Duane Rettig <·····@franz.com> writes:
> 
> 
>><offense=MS employees: heard many years ago>
>>The old story goes that a man was lost in a hot-air balloon, and
>>drifting in the northwestern US, and he finally came to a tall
>>building and saw a man standing on the roof.  The man in the balloon
>>yelled out: "where am I?", and the man said "You're about 15 stories
>>above the ground."  To which the man in the balloon replied, "Oh,
>>this must be the MS support center".  The man on the roof answered
>>"why, yes, how did you know?", to which the balloon man answered
>>"Well, the information you gave me was correct, but useless".
>></offense>
> 
> 
> This is an "IT" variation of a much older story about New Englanders
> (or maybe Mainers in particular).  Just replace "tall building" with
> "farm" and "MS support center" with "New England/Maine".

I cannot find the Maine version, but here is the (much better) full IT 
version: http://www.fullofjokes.com/joke.asp?id=2209

And I think compile-time vs run-time is good enough for a newbie or 
homework answer. Too sophisticated an answer at this level could 
overwhelm a newbie or tip off the prof. (It /is/ September. <g>)

kenny

-- 
Cells? Cello? Celtik?: http://www.common-lisp.net/project/cells/
Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film
From: Tayssir John Gabbour
Subject: Re: difference between macros and functions
Date: 
Message-ID: <866764be.0409161638.1c2eead1@posting.google.com>
Michael H�nisch <················@ascad.de> wrote in message news:<··········@news.adm.arcor.net>...
> whats the difference between macros and functions.
> 
> Where should I use a macro and where functions?

Ernst van Waning gave a lecture on macros which I liked. You're not
likely to enjoy it now, but at some point in the future when you're
used to lisp...
http://alu.cliki.net/lisp-user-meeting-amsterdam-april-2004#macros-and-codewalkers

MfG,
Tayssir
From: Patrick O'Donnell
Subject: Re: difference between macros and functions
Date: 
Message-ID: <rtekl029eq.fsf@ascent.com>
Michael H�nisch <················@ascad.de> writes:
> 
> Where should I use a macro and where functions?

Long ago, when I was first learning Lisp, I was trying to explain
macros and functions to a fellow student -- badly.  I was transferring
my own confusion to him.  Our teacher overheard me, and scolding me
for my explanation, shared four words that cleared up the distinction
for me forever: "Macros translate, functions compute."

As others have lengthily described, macros generate code.  Typical
places that macros are used are,

To generate basically boiler-plate code that would be tedious or
error-prone to create and maintain by hand.  Example: defstruct;
macroexpand a defstruct form and look at all the accessors.

To rearrange (and abstract) code to make it more concise, to improve
maintainability, more readable, and to improve textual locality of
related code.  For example, some programmers dislike that if passing a
length lambda function to maphash, the table that is mapping can get
lost way at the end: 

	(maphash #'(lambda (key value)
		     stuff
		     stuff
		     stuff
		     stuff)
		 table)

A macro can rearrange this, so:

	(mapping-hash-table (key value table)
	   stuff
	   stuff
	   stuff
	   stuff)

Another standard example is with-open-file, which combines these two
motives.

Some programmers use macros in an attempt to optimize their
computation, as in the "square" example by another poster.  I tend to
recommend against that: macros translate, /functions/ compute.  Define
square as a function.  Then you can apply and map it.  If efficiency
is an overriding concern, there are other ways to accomplish that goal
(q.v. compiler macros).

		- Pat
From: Alan Crowe
Subject: Re: difference between macros and functions
Date: 
Message-ID: <86vfedvylj.fsf@cawtech.freeserve.co.uk>
Michael.Hoenisch asks
> whats the difference between macros and functions.

A function definition looks like this

    (defun name arg-list . code)

and is invoked like this

    (name CL-form-1 CL-form-2 ... CL-form-n)

A macro definition looks like this

    (defmacro name arg-tree . code-for-code)

and is invoked like this

    (name bend me (shape me) (anyway (you want me)))

Functions are written and used under a very different set of
constraints from macros.

Functions are written to produce the end result of the
computation. The result can be anything the author
desires. Only exceptionally will that be valid CL.

Functions are used with arguments that are valid CL. The
programmer must take care to write a valid CL form for every
argument of a functional call because the arguments are
evaluted and the function is applied to the results of those
evaluations.

Macros are written to produce code which is not the end
result of the computation. There is a second round of
evaluation in which the code produced by evaluating the body
of the macro definition is itself executed to
produce the end result of the computation. Hence the body of
a macro definition is given above as code-for-code. The
author of a macro must take pains to ensure that the execution
of the body of his macro results in valid CL, for it will be
evaluated.

Macros are invoked on the actual data structure that READ
builds when it reads the source file. This is in contrast to
functions, which do not get to see the data structure that
READ builds, but only get to see the result when that data
structure is evaluated as a CL form. There is no direct
constraint on the arguments to a macro.  The execution of
the body of a macro must result in valid CL code, so there
is an indirect constraint on the arguments to the macro,
mediated by the actual code of the macro body.

What I have written so far creates a mystery. I have
portrayed macros and functions as very
different. Nevertheless, the difference between them is a
source of legitimate perplexity. How come?

One of the key ideas in computer science is DRY - Don't
Repeat Yourself. Although macros and functions provide very
different mechanisms for removing repetition from your code,
they sometimes compete over the same piece of code. The
programmer must choose between them.

For example:

   (+ (* (sin angle)(sin angle))
      (* (cos angle)(cos angle)))

would benefit from the concept of squaring. It could be done
as a function

(defun square (x)(* x x))

(+ (square (sin angle))
   (square (cos angle)))

or as a macro

(defmacro square(x)(list (quote *) x x)) etc.

This macro is almost certainly not what the programmer
wants. I'm not referring to programming style. Yes, the macro
cries out for the use of backquote

(defmacro square (x) `(* ,x ,x))

but the real problem is that 

(defvar a 3)
(square (incf a)) => 20

How is 20 the square of an integer, it should 16 or 25,
anything but 20.

(macroexpand-1 '(square (incf a)))
=> (* (INCF A) (INCF A))

shows the source of the problem.
It can be fixed

(defmacro square (x)
  (let ((temp-variable (gensym)))
    `(let ((,temp-variable ,x))
       (* ,temp-variable ,temp-variable))))

after which (square (incf a)) => 36.

You can see that defmacro is more powerful and more general
than defun, which leads naturally to the advice to keep your
code simple and straight forward by using defun, and keep
defmacro in reserve for when defun isn't powerful enough.
Pay particular attention to the evaluation of
arguments. Arguments to functions are evaluated exactly
once, left to right. Writing macros lets you control whether
the arguments are CL forms, whether they are evaluated, and
if so, how many times they are evaluated. Macros even let
you harrass maintenance programmers into early graves by
messing with the left to right order of evaluation.
  
However, there is a sense in which what I've written above
is a huge red-herring that leads the reader far from the
point.

Consider this

(defun squaring-macro (form)
   (let ((temp (gensym)))
      `(let ((,temp ,form))
         (* ,temp ,temp))))

(+ (eval (squaring-macro '(sin angle)))
   (eval (squaring-macro '(cos angle))))
=> 1.0

If one ignores the nightmarish performance implications of
running the functions that write the program after the
program has started running, then one doesn't need
macros. So are macros a solely a performance issue? I gave a
very different impression here in before.

Suppose that you are programming in C and your project is
dying under you. At too many key points the language is
turning out to be unsuitable. C has its own concept of
macros, but for all their difficulty, they only provide
trivial capabilities. What can you do?

Well, gcc, the FSF's C compiler, is open source. You could
try hacking at it to upgrade the language and rescue your
project. Good luck. The design of C makes certain basic
decisions, static typing, rich surface syntax, no memory
management, that block upgrading the language.

CL is designed to be user upgradable, and defmacro is your
hook into the language, giving you the power of hacking at
the compiler source, but in retail packaging. defmacro is
fully defined in the standard at the language level. You do
not have to look at the source code of your
implementation. It need not even be open. Your language
upgrades are fully portable.

Let us suppose, for simplicity of exposition, that the
defmacros that upgrade the language are in macro.lisp and
the project, now being written in the upgraded language, is
in project.lisp.

(load "macro.lisp") turns your lisp image into a compiler
for the upgraded language.

(compile-file "project")
(load "project")
(run-regression-tests)

sees you back at work on it.

If it is a large project, you probably hit a snag at this
point. In many implementations (load "macro.lisp") creates a
strange hybrid. The bodies of the defmacros run
interpretively, and quite slowly. So the new compiler you
have created, although it compiles to native code, is not
itself fully compiled any more, and in places runs
relatively slowly. It is easily fixed. 

(compile-file "macro")(load "macro")

Since CL does this stuff so slickly it is easy to miss
just how remarkable it all is. You have your macros and your
functions and you build your project with

(compile-file "macro")
(load "macro")
(compile-file "project")
(load "project")
(run-regression-tests)

It is as easy a compiling a project in C, but the first
compile is actually analogous to having your own version of
gcc, and recompiling the compiler to obtain a compiler for
an upgraded language before going on to compile a project
written in the upgraded language.

So what is the difference between a macro and a function?
With defmacro CL takes the idea of hacking at the compiler
source code to produce an improved language, and packages it
so slickly that it is possible to mistake it for merely
writing a function definition.

Alan Crowe
Edinburgh
Scotland