-
The macro could actually be even simpler than that, because the body of LET is an implicit progn.
(defmacro ++ (x)
`(let ((,old-x ,x))
(incf ,x)
,old-x)))
-
Um, that was slightly off. Where's Emacs when I need it? :)
I was playing around with variable-capturable variants, and the commas in front of old-x slipped in there at some point. I'm tempted to try it with non-breaking spaces, and get some real indentation, but I'm not sure if your comment rendering allows them. Where's pre when I need it? :)
(defmacro ++ (x)
`(let ((old-x ,x))
(incf ,x)
old-x)))
-
Seems like a good use for PROG1 (instead of LET)...
-
Definitely not a TOOWTDI language... :-)
-
Nope, it's really not. Have to admit I like it that way :)
Here's another - this doesn't even need to be a macro.
(defun ++ (x)
(let ((old-x x))
(incf x)
old-x)))
-
I don't think that last one works...
-
Not sure this macros is too safe.
Have you tried something along
> (setq old-x 1)
> (++ old-x)
?
Doesn't work for me. Lisp macros aren't hygenic as Scheme's are...
-
Two more things:
a) You should definitely use setf or incf. That way you support not just simple variables but all settable forms. (E.g. (++ (aref foo 1) )
b) But when you allow settable forms, you should take care about things that can have side effect. (E.g. (++ (aref foo (++ bar)))).
To fully investigate this, I suggest to study the pop macro which is very similar to the one discussed here. I.e. note the differences between all of these:
(macroexpand '(pop foo)),
(macroexpand '(pop (aref foo 1)))),
(macroexpand '(pop (aref foo (++ bar))))).
Macros ain't easy... :-(