do...until wisdom needed...

Douglas Alan nessus at mit.edu
Mon May 7 03:30:44 EDT 2001


"Andrew Dalke" <dalke at acm.org> writes:

> >It wouldn't be small if Python had procedural macros.  Most people
> >would love them -- just like most people love them in Lisp.

> I guess I'm stuck at the point trying to figure out how procedural
> macros could be added to Python.  Is it at all possible to come up
> with a hand-waving/straw example?  Everything I think of seems to
> end up with a function that converts a large quoted string into a
> new Python module, class or function.

A procedural macro could be as simple as a function that takes a
string as its argument and returns a string.  The string handed to the
macro implementation would be the text of the macro invocation.  The
string returned from the function would be the text used to replace
the macro invocation in the original program.  To make the
programmer's life much easier, there would be a library of handy
parsing and unparsing tools.

This isn't the only possible approach, but I think it's the way I
would do it.  The approach described above is not "hygienic", which a
macro mechanism really should be.  It would need to be made *bit* more
complicated to make it so.

> >Look how long it took OO
> >to catch on (another invention that grew out of Lisp).  Most of the
> >arguments people make against procedural macros today, people used to
> >make about about OO.

> I was under the impression that OO programming grew out of Simula-67.

It grew out of Simula-67 too.  The first true OO language was
Smalltalk, which grew out of Lisp and Simula-67, and was invented in
the '70's.  OO made it's way back into Lisp in about two hours (okay
slight exaggeration, but not by much) after the invention of
Smalltalk.  This happened so quickly due, in part, to Lisp's
extensible syntax.

> >OO was invented in the '70's and it took until the '90's for it to
> > catch on.

> Right delta, but it was invented in '60s.  As to the later date, I
> remember when Borland shipped Turbo Pascal 5.5 in May 1989.  Its OO
> ideas were based off of ideas from Apple's Object Pascal, released
> in '86.  I was using Turbo C++ in summer 1991 because Borland
> shipped Turbo C++ that February.  So I would have dated things to
> "late '80s" and not "'90's".

Just because some people were using OO, doesn't mean it had "caught
on" in any serious way.  I was doing OO in Lisp in 1980, but finding a
job doing that would have been something of a challenge.

> My apologies for using a loaded word there.  I meant something that
> was the equivalent to "widely used languages" "other than Lisp"
> (which you include as a widely used language even though it "only
> really ever caught on in AI circles and for teaching programming").
> All quotes yours.

Lisp *is* widely used for AI.  In fact, the vast majority of AI is
done in Lisp.  And Scheme *is* widely used for teaching Computer
Science.  If you want to see how popular macros are, you need look no
farther than C.  I don't think many C programmers would want to give
up the macro mechanism.  The problem with the macro mechanism for C is
that it is cumbersome, flaccid, inelegant, and error-prone.  These are
not fundamental limitations of macro mechanisms in general -- just C's
lame instantiation.

> >Dylan proves that it can be done.  (It's derived from Lisp, but has a
> >more traditional syntax.)  Macros are typically executed at
> >compile-time, not run-time, which is often necessary for efficiency
> >reasons.

> Could you give an example of the Dylan syntax for doing macros?
> Perhaps this would help with my mental block in understanding how it
> could/should be applied to Python.

     define macro unless 
       { unless ?test:expression ?:body }
         => { if (~?test) ?body }
     end macro;

> > In  any case,  Scheme (a  dialect of  Lisp) is  usually considered
> > state of  the art  for teaching Computer  Science 101.   Scheme is
> > what's used at MIT, Harvard, Yale, etc.

> Just to double check, I looked up what language Yale uses for
> their intro. classes

> CPSC 201 "Introduction to Computer Science" uses Haskell, but
> http://plucky.cs.yale.edu/cs201/ says
> ] (Previously in this course we used the Scheme programming language.
> ] In some ways Haskell can be viewed as a strongly-typed variant of
> ] Scheme.)

Well, I suppose one could argue that Haskell is now state of the art
for teaching Computer Science, rather than Scheme.  Haskell is newer
than Scheme, and it is derived from ML, which is derived from Lisp.
(Calling Haskell a dialect of Lisp or a dialect of Scheme would be a
bit too strong of a statement.) I wouldn't have been displeased to
have learned Haskell instead of Lisp in CS 101.  But I still think
that Scheme is preferable for teaching CS.

> On a roll, I decided to check out Harvard:
> CS50 "Introduction to Computer Science I" uses C
>   http://www.fas.harvard.edu/~lib50/
> CS51 "Intro. to CS II" uses LISP, C++, and Java
>   http://www.fas.harvard.edu/~lib51/write.cgi

I took Computer Science at Harvard when I was in high school, and the
equivalent of CS50 at the time was for dolts.  The equivalent of CS51
was for students that had a clue.  The most interesting part of the
course was, by far, the Lisp section.  And in the C section they just
had us implement Lisp in C.

> Judging from your email address, I'll assume you know about what MIT
> uses for their intro courses.

Yes, they use Scheme and the incredible textbook *Structure and
Interpretation of Computer Programs*, which is the only book that
anyone should ever use to teach Computer Science 101.

|>oug



More information about the Python-list mailing list