Python syntax in Lisp and Scheme

Matthias no at spam.pls
Mon Oct 6 06:54:30 EDT 2003


gregm at cs.uwa.edu.au writes:

> In comp.lang.functional Erann Gat <my-first-name.my-last-name at jpl.nasa.gov> wrote:
> :> I can't see why a LISP programmer would even want to write a macro.
> : That's because you are approaching this with a fundamentally flawed
> : assumption.  Macros are mainly not used to make the syntax prettier
> : (though they can be used for that).  They are mainly used to add features
> : to the language that cannot be added as functions.
> 
> Really? Turing-completeness and all that... I presume you mean "cannot
> so easily be added as functions", but even that would surprise me.
> (Unless you mean cannot be added _to_Lisp_ as functions, because I don't
> know as much as I'd like to about Lisp's capabilities and limitations.)

IMHO, these discussions are less usefull when not accompanied by
specific examples.  What are these macros good for?  Some examples
where you might have difficulties with using ordinary functions:

1.) Inventing new control structures (implement lazy data structures,
  implement declarative control structures, etc.)  
  => This one is rarely needed in everyday application programming and
  can easily be misused.

2.) Serve as abbreviation of repeating code.  Ever used a code
  generator?  Discovered there was a bug in the generated code?  Had
  to fix it at a zillion places?
  => Macros serve as extremely flexible code generators, and there
  is only one place to fix a bug.
  => Many Design Patterns can be implemented as macros, allowing you
  to have them explicitly in your code.  This makes for better 
  documentation and maintainability.

3.) Invent pleasant syntax in limited domains.
  => Some people don't like Lips' prefix syntax.  It's changeable if you 
  have macros.
  => This feature can also be misused.

4.) Do computations at compile time instead of at runtime.
  => Have heard about template metaprogramming in the C++ world?
  People do a lot to get fast performance by shifting computation 
  to compile time.  Macros do this effortlessly.

These are four specific examples which are not easy to do without
macros.  In all cases, implementing them classically will lead to code
duplication with all the known maintainability issues.  In some cases
misuse will lead to unreadable or buggy code.  Thus, macros are
powerful tools for the hand of professionals.  You have to know if you
want a sharp knife (which may hurt you when misused) or a less sharper
one (where it takes more effort to cut with).




More information about the Python-list mailing list