This is my first foray into emacs macros, so likely silly mistakes ahead: I'm trying to mimic the definition of pop (see the manual here), but I want to remove an element of alist indexed by key, returning the value corresponding to key from alist while altering alist in place. Here's my attempt:

  (defun alist-remove (alist key)
     (lambda (pair) (not (eq key (car pair))))

  (defmacro alist-pop (alist key)
         (cdr (assq ,key ,alist))
       (setq alist (alist-remove ,alist ,key))))

The function alist-remove works just fine. The macro alist-pop returns the value of the key correctly, but doesn't modify the value of alist. What am I missing?

1 Answer 1

-       (setq alist (alist-remove ,alist ,key))))
+       (setq ,alist (alist-remove ,alist ,key))))

Get into the habit of using M-x pp-macroexpand-last-sexp.

Used following (alist-pop a 'b) it will show you:

     (assq 'b a))
  (setq alist
    (alist-remove a 'b)))

See also pp-macroexpand-expression as well as the functions described in C-hig (elisp)Expansion

  • One little comma. Thank you. Apr 11, 2023 at 1:02
  • If the answer has successfully resolved the problem, please accept it.
    – NickD
    Apr 11, 2023 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.