Explanation of macros; Haskell macros

Stephen J. Bevan stephen at dino.dnsalias.com
Tue Nov 4 00:18:09 EST 2003


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.  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).




More information about the Python-list mailing list