Macros

Alan Bawden Alan@LCS.MIT.EDU
Fri, 4 May 2001 16:19:06 -0400 (EDT)


   Date: Thu, 3 May 2001 18:09:01 -0500 (EST)
   From: Dan Knapp <dankna@brain.mics.net>

   > >   (if (not (< x 3))
   > >       (assertion-failed '(< x 3)))
   > 
   > This is a good example, which cannot be implemented in
   > Haskell.  "Exception.assert" is built in to the ghc compiler, rather than
   > being defined within the language.  On the other hand, the built in
   > function gives you the source file and line number rather than the literal
   > expression; the macro can't do the former.

   Yeah, it's a good example, but are there any other uses for such quoting?

There are a few.  But this isn't the -only- reason to still use macros.

We could systematically go through all the macros I've written in the last
few years, and for each one we could figure out what language feature would
be needed in order to make that macro unnecessary.  At the end of the
process you would have a larger programming language, but I still wouldn't be
convinced that we had covered all the cases.

A macro facility is like a pair of vise-grips (if you don't know what those
are, see http://www.technogulf.com/ht-vise.htm).  You can do a lot of
things with a pair of vise-grips, although usually there's a better tool
for the job -- if you haven't got (say) a pipe-wrench, then a pair of
vise-grips can substitute.  Now the more tools you have in your tool box,
the less often you will use your vise-grips.  But no matter how bloated
your tool box becomes, you will still want to include a pair of vise-grips
for the unanticipated situation.

I have one problem with my own analogy: If you find yourself using your
vise-grips everyday for some task, you will probably soon go and purchase a
more appropriate tool.  But I think that in many circumstances macros do
such a good job that I don't see the need to clutter up the language with
the special-prupose features needed to replace them.

   Date: Fri, 04 May 2001 12:57:29 +0200
   From: Jerzy Karczmarczuk <karczma@info.unicaen.fr>

   ...
   I think that they are less than popular nowadays because they are dangerous,
   badly structured, difficult to write "hygienically"....

Indeed, you can screw up pretty badly with a pair of vise-grips!  A friend
of mine used to say that programmers should have to pass some kind of
licensing test before they would be allowed to write Lisp/Scheme macros.