[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [External] : Re: Easy to add with push but not to the end of a list
From: |
Emanuel Berg |
Subject: |
Re: [External] : Re: Easy to add with push but not to the end of a list |
Date: |
Mon, 28 Nov 2022 23:45:35 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) |
Drew Adams wrote:
>> 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) )
>
> Yes.
>
> (append lst (list elem)) also traverses the list
> usually.
Now it works at least, O(n) or not ...
(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
- Easy to add with push but not to the end of a list, Heime, 2022/11/27
- RE: [External] : Easy to add with push but not to the end of a list, Drew Adams, 2022/11/27
- Re: Easy to add with push but not to the end of a list, Stefan Monnier, 2022/11/28
- Re: Easy to add with push but not to the end of a list, Emanuel Berg, 2022/11/28
- Re: Easy to add with push but not to the end of a list, Heime, 2022/11/28
- Re: Easy to add with push but not to the end of a list, Stefan Monnier, 2022/11/28
- Re: Easy to add with push but not to the end of a list, Emanuel Berg, 2022/11/30
- Re: Easy to add with push but not to the end of a list, tomas, 2022/11/30
- Re: Easy to add with push but not to the end of a list, tomas, 2022/11/29
- Re: Easy to add with push but not to the end of a list, Heime, 2022/11/29
- Re: Easy to add with push but not to the end of a list, Marcin Borkowski, 2022/11/29
- Re: Easy to add with push but not to the end of a list, Heime, 2022/11/29
- Re: Easy to add with push but not to the end of a list, Emanuel Berg, 2022/11/30
- Re: Easy to add with push but not to the end of a list, Emanuel Berg, 2022/11/30