On Sat 07 Apr 2012 10:25:00 PM CEST, Benjamin Saunders wrote:
PROG1-LET is a binding macro modeled closely after WHEN-LET and friends, which I have regularly found useful in code to implement the "create, modify, return" pattern common in some imperative code. As a simple and, I believe, widely useful macro, I'd like to see this enter into Alexandria proper. Docstring follows:
I usually use something like
(aprog1 (cons 'initially 0) (incf (cdr it)))
to implement this pattern using the ANAPHORA library. Your macro would of course be a better solution if, for some reason, one doesn't want to use an anaphoric macro, but IMO it would be cleaner to just handle a single form, eg
(defmacro prog1-let ((variable initial-form) &body body) `(let ((,variable ,initial-form)) (prog1 ,variable ,@body)))
because the first form is special anyway. A multiple-value extension that returns all values in the let form would be interesting:
(defmacro prog1-values-let (bindings &body body) ;; could be named better? `(let ,bindings (multiple-value-prog1 (values ,@(mapcar (compose #'car #'ensure-list) bindings)) ,@body)))
(prog1-values-let ((mycons (cons 'initially 0)) (mynum 9)) (incf (cdr mycons)) (decf mynum))
=> (initially . 1), 9
And similarly with LET*.
Best,
Tamas