0

I have a list, and want to clear it with (setq slist '()). But I only want to do this when the number of elements exceeds the value N.

3
  • 1
    (when (> N (length slist)) (setq slist nil)). Check out the on-board introduction to emacs lisp: C-h R eintr which will help you answer several of your recent questions. Commented Aug 19, 2022 at 8:46
  • Have seen (setq slist '()). Is (setq slist nil) preferable for reading?
    – Dilna
    Commented Aug 19, 2022 at 8:56
  • Since (eq nil '()) is true, it doesn't matter which you use. I think the latter is less noisy. Commented Aug 19, 2022 at 14:31

1 Answer 1

0

You can do it using this macro

(defmacro normalize-list(l n)
  "empties the list L when its size reaches N"
  `(if(>=(length ,l),n)(setq ,l '())))

Examples

(setq slist'(foo bar baz)) ;->(foo bar baz)

(normalize-list slist 2);->nil

slist ;->(foo bar baz)

(normalize-list slist 5);->nil

slist;->nil
3
  • 1
    Why do you need a macro for this? Commented Aug 19, 2022 at 14:30
  • The argument l (for the list) must not be evaluated. In case l is bound to '(foo bar baz) What would elisp (setq l'()) render if the symbol l is evaluated? And what about (length l) if the argument l is quoted ?
    – gigiair
    Commented Aug 19, 2022 at 15:04
  • yup, good point. I was not thinking clearly earlier. Commented Aug 19, 2022 at 20:23

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.