Explanation of macros; Haskell macros

Rene de Visser Rene_de_Visser at hotmail.de
Mon Nov 3 12:28:34 EST 2003


"Joachim Durchholz" <joachim.durchholz at web.de> wrote in message
news:bo5r9t$tdq$1 at news.oberberg.net...
>
> Does anybody have a keyword-style list of useful applications of the
> macro facilities?
>
I don't write macros that often, I mainly use HOF's.

I always think of a macro as a function that takes some code and returns
some code, and takes place at compile time.

I think that macro's can be categorized as follows:

1) Create new bindings (variable for function bindings).

One of the simplest macros that I use is mvlet* (for multiple value
bindings). This lets you do things like

(mvlet ((
    (c (func1 d)
    (:values a b) (func2 e))
    ...
which binds a b to the values returned by func2. Due to an accident of fate
multiple values aren't so well integrated into common lisp,
so macros can be used to remove deficiencies from langauge.

2) Generating/changing code from meta data.

i)

(with-extracting-expressions (a b c)
   .... )

where a b c are functions that can be assumed to be side-effect free. Based
on this expressions should be bubbled up to the outside
to improve performance.

ii) Removing almost repeated code.

Often I notice that in a program there is code that is similar but not
identical. Then a macro can be written that based on meta data generates the
various different versions of this code.

3) New languages or semantics which expand to lisp code.

e.g. relation lisp, screamer (non-deterministic lisp), parsers, series,
CLOS.

As a side note, I note that in the GHC source code that the parser is
generated Haskell code. Why is not the parser directly specified in Haskell?

In common lisp you would specify the parser directly in LISP and macros
would expand/transform it to its final runable form.

Rene.








More information about the Python-list mailing list