Re: what flet doesn't work here??



In article <96OdnekPAoaQSRDYnZ2dnUVZ_sOknZ2d@xxxxxxxxxxxxx>,
rpw3@xxxxxxxx (Rob Warnock) wrote:

Ron Garret <rNOSPAMon@xxxxxxxxxxx> wrote:
+---------------
| The wonderful thing about CL is that it imposes very few constraints
| that cannot be dispensed with in a few lines of code:
|
| (defmacro ddefun (fname args &body body) ...)
+---------------

Yes, but as I pointed out upthread, that only works if you use
DDEFUN to define *all* the functions you want to be able to later
use DFLET with. I got the impression the OP wanted to be able to
override functions already defined by other code [though I could
be mistaken], which your certainly nice macros can't help with.
[AFAIK, nothing can, in general.]

Piffle.

(defmacro dflet (bindings &body body)
(loop for (fname . ignore) in bindings do
(unless (member fname *dynamically-bound-functions*)
(push fname *dynamically-bound-functions*)
(eval `(progn
(defvar ,fname)
(setf ,fname ,(and (fboundp fname) `#',fname))
(defun ,fname (&rest args) (apply ,fname args))))))
`(let ,(mapcar (lambda (b) `(,(car b) (lambda ,(second b) ,@(cddr b))))
bindings)
,@body))

? (defvar *DYNAMICALLY-BOUND-FUNCTIONS* '())
*DYNAMICALLY-BOUND-FUNCTIONS*
? (defun f1 () 'f1)
F1
? (defun f2 () (f1))
F2
? (dflet ((f1 () 'foo)) (f2))
FOO
? (f2)
F1
?

rg
.