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

Scott L. Burson Scott at sympoiesis.com
Sun Sep 5 21:17:53 UTC 2010


On Sun, Sep 5, 2010 at 5:24 AM, Kazimir Majorinc <kazimir at chem.pmf.hr> wrote:
>
> 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?

This is one of my favorite example macros.  Although in this simple
form it's not something I use in real code, I do use a variation on
this theme all the time.  The best part about it is, there are few
languages other than Lisp in which a tree walk can be so neatly
encapsulated into an iteration primitive.

(defmacro do-leaves (var tree &body body)
  "Walks TREE, a list structure; for any descendant that is not a list, binds
VAR to it and executes BODY."
  `(labels ((rec (,var)
	      (when ,var
		(if (listp ,var)
		    (progn
		      (rec (car ,var))
		      (rec (cdr ,var)))
		  (progn , at body)))))
     (rec ,tree)))

-- Scott




More information about the pro mailing list