Go to Google Groups Home    comp.lang.lisp
generating new names in macro -- style question

Albert Krewinkel <krewin...@gmx.net>

I'm struggling with my sense of beauty here, having difficulties to
decide if the following is a good or bad idea.  A few comments  about the
following code with respect to good lisp style would be highly
appreciated.

I'm working with graphs (the mathematical structures) and wrote a couple
of functions which all take a graph as it's first argument.  To safe
myself some keystrokes, I wrote a macro which takes a graph as argument,
and provides curried functions of the graph-functions, also changing
the name to avoid confusion (and to safe a few more keystrokes).

Therefore instead of

(progn
  (graph-add-vertex some-graph 4)
  (graph-delete-vertex some-graph 5))

now I can do

(with-graph (some-graph)
  (add-vertex 4)
  (delete-vertex 5)

This comes in very handy when I have to do multiple operations on the
same object.  However, it feels strange to generate new symbols, so I
wonder if there is a better way doing this kind of thing.  Global
variables would work, but that would break my attempt of doing things in
a functional manner.

Thanks
A

(defgeneric graph-add-vertex (graph ...) ...)

(defparameter *graph-functions*
  '(graph-add-vertex graph-delete-vertex graph-vertex-outdegree))

(defun graph-flet-curry (graph function new-function)
  `(,new-function (&rest args)
                  (apply #',function ,graph args)))

;; function `remove-symbol-prefix' alters a symbol by removing a prefix
;; (function definition not included for shortness)

(defmacro with-graph ((graph) &body body)
  (flet ((graph-flet-curry (graph function new-function)
           `(,new-function (&rest args)
                           (apply #',function ,graph args))))
    `(flet ,(loop for function in *graph-functions*
                  collect (graph-flet-curry graph
                                function
                                (remove-symbol-prefix function
                                       'graph)))
       ,@body)))