[pro] The Best Examples of "Code is Data"

Daniel Weinreb dlw at itasoftware.com
Tue Sep 7 21:41:34 UTC 2010


It's very, very hard to know what "best macro" means.  It's
like asking for the "best function".  Perhaps what people
mean is the "cutest" or "most remarkable" macro.

I'm rather proud of the macros I wrote for the logging
system in QRes.  I really ought to refactor it out and
open-source it.  What I wanted to do was to encourage
people to add logging forms to their code.  One reason
people resist putting them in is that they worry about
the time (compute) overhead when logging is turned
off.  So I thought to myself, what's the lowest we
could get it to be?  Well, suppose that every logging
statement turns into a read of a special variable
and a check to see whether it's NIL or not?  You
can't do a whole lot better than that, I'd think.
Now, let's say you have hierarchical names of
categories.  You turn on logging for "foo.bar",
and you do not want calls that log to "foo.quux"
to get turned on; they too should be very, very
fast.  But you do not know in advance the names of
all the logging categories there might be.  I guess
I could have required the programmer to add the
categories to a file, but ANYTHING that makes
logging harder will discourage people from
adding logging statements.  If you try to do this,
you'll find that it's a bit tricky.  You need some
eval-when stuff.  You need to create symbols dynamically.
It's not rocket science, but it's sort of cool and it
might be useful to others.

-- Dan

Yakov Zaytsev wrote:
> My best macro so far ;-)
>
> (defmacro access-slot (object &rest slot-names)
>   `(ff:fslot-value-typed (ff:foreign-pointer-type ,object)
>                          :c (ff:foreign-pointer-address ,object)
>                          , at slot-names))
>
> (defmacro define-struct-getter (struct package &rest slots)
>   (loop for s in slots
>      do (let ((acc (intern (concatenate 'string (symbol-name struct) 
> "-" (symbol-name s))))
>                    (sp (intern s package)))
>                (eval `(defmacro ,acc (object)
>                         `(access-slot ,object ',',sp))))))
>
> because it is a macro which writes macros which uses macro. My first 
> real world macro.
>
> On Sun, Sep 5, 2010 at 4:24 PM, Kazimir Majorinc <kazimir at chem.pmf.hr 
> <mailto:kazimir at chem.pmf.hr>> wrote:
>
>     As first, good luck with this list!
>
>     I'm in search for best examples of "code is data" paradigm in Common
>     Lisp. For most CL-ers, it probably means "macros", but eval,
>     backquote,
>     anything that processes the code as data is of interest. As "best" I
>     think on the most surprising, powerful, sophisticated examples, not
>     necessarily of a pedagogical value.
>
>     Imagine that someone invited you to write the presentation "Five
>     best CL
>     macros ... I seen" or "Five best CL macros ... I wrote." What
>     would you
>     chose and why?
>
>     Kazimir Majorinc
>
>
>
>
>
>     _______________________________________________
>     pro mailing list
>     pro at common-lisp.net <mailto:pro at common-lisp.net>
>     http://common-lisp.net/cgi-bin/mailman/listinfo/pro
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> pro mailing list
> pro at common-lisp.net
> http://common-lisp.net/cgi-bin/mailman/listinfo/pro
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/pro/attachments/20100907/88fc0159/attachment.html>


More information about the pro mailing list