Python syntax in Lisp and Scheme

Pekka P. Pirinen Pekka.P.Pirinen at globalgraphics.com
Mon Oct 13 14:24:25 EDT 2003


Alex Martelli <aleaxit at yahoo.com> writes:
> > Can you give an example for the presumably dangerous things macros
> > supposedly can do that you have in mind?
> 
> I have given this repeatedly: they can (and in fact have) tempt programmers
> using a language which offers macros (various versions of lisp) to, e.g.,
> "embed a domain specific language" right into the general purpose language.
> I.e., exactly the use which is claimed to be the ADVANTAGE of macros.  I
> have seen computer scientists with modest grasp of integrated circuit design
> embed half-baked hardware-description languages (_at least_ one different
> incompatible such sublanguage per lab) right into the general-purpose
> language, and tout it at conferences as the holy grail

But this type of domain-specific language is not the advantage that
people mean.  (After all, this type of task is rare.)  They don't mean
a language for end-users, they mean a language for the programmers
themselves.

Any large software system builds up a domain-specific vocabulary;
e.g., a reactor-control system would have a function called
SHUTDOWN-REACTOR.  In other languages, this vocabulary is usually
limited to constants, variables and functions, sometimes extending to
iterators (represented as a collection of the above); whereas, in
Lisp, it can include essentially any kind of language construct,
including ones that don't exist in the base language.  E.g., a
reactor-control system can have a WITH-MAINTAINED-CONDITION context
(for the lack of a better word).

Whether or not the expansion of WITH-MAINTAINED-CONDITION is
particularly complex, the ability to indicate the scope of the
construct by enclosing a code block in a "body" is one of the most
useful aspects of this style of program construction.  (This can be
done with HO functions in a fairly nice way, I know, assuming the
implementation of the construct does not need to examine or modify the
body code.)

Another very common language feature in these domain-specific
languages is a definer macro.  When a number of similar entities need
to be described, a Lisp programmer would usually write a
DEFINE-<entity> macro which would generate all the boilerplate code
for initialization, registration, serialization, and whatever else
might be needed.  This is also the way high-level interfaces to many
Lisp packages work: e.g., to use a Lisp GUI package you would
typically write something like
  (define-window my-window (bordered-window)
    :title "My Application"
    :initial-width (/ (screen-width) 2)
    ...
    :panes (sub-window ...)
  )
and that would be 80% of the functionality.  (Then you'd have to write
methods for the last 20%, which would be the hard bit.)  Matthew
Danish' DEFINSTRUCTION macro in another subthread is a good example as
well.
-- 
Pekka P. Pirinen
Controlling complexity is the essence of computer programming.
  - Kernighan




More information about the Python-list mailing list