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: Heime
Subject: Re: Easy to add with push but not to the end of a list
Date: Mon, 28 Nov 2022 22:01:24 +0000

------- Original Message -------
On Monday, November 28th, 2022 at 8:19 PM, Emanuel Berg <incal@dataswamp.org> 
wrote:


> Stefan Monnier via Users list for the GNU Emacs text editor 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.  Suppose I am accumulating
indicators that are done at increasing value of time and want to introduce
them into another list in the order they were encountered.  What should one 
handle these things? 
 
> Let's make it as good as possible first ...
> 
> Why is the below O(N), because of `last'?
> 
> (defun push-last (elem lst)
> (let ((elem-lst (list elem)))
> (if lst
> (setcdr (last lst) elem-lst)
> (setq lst elem-lst) )
> lst) )
> 
> ;; (setq nil-lst nil)
> ;;
> ;; (push-last 1 nil-lst) ; (1)
> ;;
> ;; (setq lst '(1 2 3 4))
> ;;
> ;; (push-last 5 lst) ; (1 2 3 4 5)
> ;;
> ;; lst ; (1 2 3 4 5)
> 
> --
> underground experts united
> https://dataswamp.org/~incal



reply via email to

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