help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Easy to add with push but not to the end of a list


From: Emanuel Berg
Subject: Re: Easy to add with push but not to the end of a list
Date: Mon, 28 Nov 2022 23:50:00 +0100
User-agent: Gnus/5.13 (Gnus v5.13)

Heime wrote:

>>> Although it is easy to add to a list using push, it
>>> currently looks hideous to be able to add to the end of
>>> a list.
>> 
>> That's because adding to the end of a Lisp list is
>> a bad idea.
>
> The bad idea comment disregards necessity.

Okay NOW it works ...

(defmacro push-last (elem lst)
  (if (and (symbolp lst)
           (not (symbol-value lst)) )
      (list 'setq lst `(list ,elem))
    (list 'nconc lst `(list ,elem)) ))

;; (setq lst ())         ; ()
;; (push-last 1 lst)     ; (1)
;; (push-last 1 lst)     ; (1 1)
;; lst                   ; (1 1)
;;
;; (setq lst '(1 2 3 4)) ; (1 2 3 4)
;; (push-last 5 lst)     ; (1 2 3 4 5)
;; (push-last 5 lst)     ; (1 2 3 4 5)
;; lst                   ; (1 2 3 4 5 5)

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]