Explanation of macros; Haskell macros

Peter Seibel peter at javamonkey.com
Tue Nov 4 00:31:53 EST 2003


stephen at dino.dnsalias.com (Stephen J. Bevan) writes:

> Peter Seibel <peter at javamonkey.com> writes:
> [snip]
> > Now that's pretty trivial. But now we can use SHOW to build something
> > a bit more complex:
> > 
> >   (defmacro show-calls-to ((&rest names) &body body)
> >     `(progn
> >        ,@(loop for form in body
> >              when (member (first form) names) collect `(show ,form)
> >              else collect form)))
> > 
> > This macro takes a lis of function names to SHOW and a body of forms.
> > All the top level forms in the body that contain calls to the named
> > functions are wrapped in a call to SHOW. A more sophisticated, and
> > useful, version of this macro would use code walker to find
> > interesting calls other than at the top level.
> > 
> >   CL-USER: (show-calls-to () (- 3 4) (+ 4 5) (* 6 7))
> >   42
> >   CL-USER: (show-calls-to (+) (- 3 4) (+ 4 5) (* 6 7))
> >   TRACE>> Evaluating (+ 4 5); got 9
> >   42
> >   CL-USER: (show-calls-to (+ *) (- 3 4) (+ 4 5) (* 6 7))
> >   TRACE>> Evaluating (+ 4 5); got 9
> >   TRACE>> Evaluating (* 6 7); got 42
> >   42
> 
> Is the above something you find you use with any regularity or would
> expect others to use? I ask because unless other people look at it
> and go "Wow, that's something I really want and I can now see how to
> get it via macros", then macro examples like the above aren't going
> to win anyone over.

No, as it stands I don't think show-calls-to all that useful. It was
just a quick hack to show how a macro can analyze the its arguments
and generate new code with some new bit of functionality interleaved.
into the code it was passed without that code having to necessarily be
aware of the macro. I don't even think that's a very typical use of
macros but someone was questioning how such a thing could even be
done.

I'm about ready to give up on trying to evangelize macros on
usenet--if you show a simple macro to demonstrate a point people say:
"That's silly, who'd want to do that?" If you show a macro that allows
some simple code to expand into some complex code people say, "I don't
understand that code it expands into, show me a simpler example."

Hopefully I'll finish my chapters about macros soon and I can just
point people to them. (Which will, of course, happen sooner if I stop
spending so much time on Usenet.)

> For my part, show-calls-to is not something I would ever envisage
> using. I can envisage, and occasionally do use, the trace
> functionality found in some implementations that allows the inputs
> and output(s) of a specified function to be displayed but that is
> rather a different beast (though in some Lisp implementations it
> might well be partially implemented using a macro).

All of them, probably, since the standard (if you're talking about
Common Lisp) specifies that it's a macro. ;-)

-Peter

-- 
Peter Seibel                                      peter at javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp




More information about the Python-list mailing list