1. 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)))

      posted by Tyler Berry at 12:36:09 PM on March 17, 2004  
  2. 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)))

      posted by Tyler Berry at 12:38:36 PM on March 17, 2004  
  3. Seems like a good use for PROG1 (instead of LET)...
      posted by an anonymous coward at 01:05:49 PM on March 17, 2004  
  4. Definitely not a TOOWTDI language... :-)
      posted by Hans at 02:50:00 PM on March 17, 2004  
  5. 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)))

      posted by Tyler Berry at 03:16:45 PM on March 17, 2004  
  6. I don't think that last one works...
      posted by Hans at 04:43:36 PM on March 17, 2004  
  7. 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...
      posted by Holger Duerer at 01:21:11 AM on March 18, 2004  
  8. 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... :-(

      posted by Holger Duerer at 02:36:29 AM on March 19, 2004