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

Daniel Weinreb dlw at itasoftware.com
Tue Sep 7 22:08:21 UTC 2010


There is an old saying: if you are using "eval", you are
doing it wrong.  So far I have yet to find any significant
exceptions to this rule.

I do not see what the eval is for.  Just get rid of it
and have the definition of define-struct-getter
expand into the defmacro of the concatenated
name.

Meanwhile, there are several problems here.

(1) In define-struct-getter, are the slots intended
to be symbols, or forms that evaluate to symbols?
If they are symbols, intern fails.  If they are
forms that are evaluated to symbols, symbol-name
fails because it is called on the form.  I could
not get any case to work.

(2) In access-slot, you expand "object" twice,
which can cause trouble if the form to which
"object" is bound has side-effects.  It is normal
good practice to use generated symbols here.

(3) Package arguments are usually a pain in the
neck.  Standard practice is to just use "intern"
so that it's interned in the current package.

-- 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/ac0958dc/attachment.html>


More information about the pro mailing list