Stories
Slash Boxes
Comments

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

TorgoX (1933)

TorgoX
  sburkeNO@SPAMcpan.org
http://search.cpan.org/~sburke/

"Il est beau comme la retractilité des serres des oiseaux rapaces [...] et surtout, comme la rencontre fortuite sur une table de dissection d'une machine à coudre et d'un parapluie !" -- Lautréamont

Journal of TorgoX (1933)

Friday July 07, 2006
06:21 AM

(defmacro ++ ...)

[ #30203 ]
Dear Log,

Today's inane new elisp macro of mine:

(defmacro ++ (x)
  "increment the value of symbol X, returning the new value"
  `(setq ,x (1+ ,x))
)

So this:

(++ lines)

is a handy shorthand that expands to this:

(setq lines (1+ lines))
  ; returns new value of the variable

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • You've just evaluated x twice when the person writing (++ x) expected x to be evaluated once.

    I consulted the manual and came up with this which doesn't work because the original x isn't getting updated anymore.

    (defmacro ++ (x)
        "Increment the value of symbol X, returning the new value"
        (let ((xt (make-symbol "quoted-x")))
            `(let ((,xt (quote ,x)))
                  (set ,xt (+ 1 (eval ,xt))))))

    This fixes three bugs. x is ev
    • You've just evaluated x twice when the person writing (++ x) expected x to be evaluated once.

      Oh, I didn't think of that! I was only thinking of passing in a symbol constant. But now I see what you mean. [emacswiki.org]

      As to the general hardness of Lisp macros: I'm glad it's not just me that thinks that.

      Maybe there's just some kind of magic syntax-highlighty thing in (e)lisp mode that will turn on all the happy macro easiness or something.

      Emacs is a pathless land.

      • I goofed slightly. I'd written an earlier version which didn't work and it looks like I left the text that mentioned that it didn't work. The one I eventually posted does work.

        As for making macros easier, I'm sure the Scheme folks have stuff on this. I'm a novice at this but I could guess that you might write something to check that each value is only used once. Something to do with data flows maybe.
  • See also the incf macro from cl.el:

    (incf x)
    (incf (car x))
    (incf x 23)

    It's definitely too hard to write such things from scratch in Lisp. cl.el also has callf and define-modify-macro macros which help somewhat.