unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Replace element in list
@ 2019-09-02  9:44 Andreas Röhler
  2019-09-02 10:37 ` tomas
  2019-09-02 13:29 ` Stefan Monnier
  0 siblings, 2 replies; 23+ messages in thread
From: Andreas Röhler @ 2019-09-02  9:44 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,

is there a recommended way to replace element x at index i of somelist  
y by newelement?

Thanks,

Andreas





^ permalink raw reply	[flat|nested] 23+ messages in thread
* replace element in list
@ 2018-11-22  4:42 edgar
  2018-11-22  5:18 ` Eric Abrahamsen
                   ` (3 more replies)
  0 siblings, 4 replies; 23+ messages in thread
From: edgar @ 2018-11-22  4:42 UTC (permalink / raw)
  To: help-gnu-emacs

Hello,

I want to share and know if there is a better way (more efficient or 
clearer) to replace something within a list (I don't know LISP).  My 
code is like this, and it works for the given example.

(defun my-list-replace (obj orig new)
     "Replaces an element in a list with something else"
     (let* (
            ;; Position of the thing we need to remove
            (pos (cl-position orig obj :test 'equal))
            ;; If pos is nil, reset to zero
            (pos (if pos pos 0))
            ;; The length of the original object
            (objlen (length obj))
            ;; The elements before the element to remove
            (head (butlast obj (- objlen pos)))
            ;; The elements after the element to remove
            (trail (nthcdr (+ 1 pos) obj)))
       ;; Join (1) the sub-list before the element to be replaced
       ;; with (2) the new element and (3) the rest of the list
       (append head (append new trail))
       ))

;; Example simple
(my-list-replace '(("a" . "b") ("c" "d")) '("c" "d") '(":)"))
;; (("a" . "b") ":)")

(my-list-replace '(("a" . "b") ("c" "d")) '("c" "d") (list '("hi")))
;; (("a" . "b") ("hi"))


;; Example long
(setq org-latex-default-packages-alist
         (my-list-replace org-latex-default-packages-alist
                    '("T1" "fontenc" t ("pdflatex"))
                    (list
                     '("" "unicode-math" t ("xelatex" "xetex"))
                     '("" "lmodern" t ("pdflatex"))
                     '("QX" "fontenc" t ("pdflatex")))))

-------------------------------------------------

ONLY AT VFEmail! - Use our Metadata Mitigator to keep your email out of the NSA's hands!
$24.95 ONETIME Lifetime accounts with Privacy Features!  
15GB disk! No bandwidth quotas!
Commercial and Bulk Mail Options!  



^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2019-09-03  5:37 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-09-02  9:44 Replace element in list Andreas Röhler
2019-09-02 10:37 ` tomas
2019-09-02 10:41   ` tomas
2019-09-02 10:46     ` tomas
2019-09-02 11:03       ` Michael Heerdegen
2019-09-02 11:05         ` tomas
2019-09-02 13:38           ` Andreas Röhler
2019-09-02 13:29 ` Stefan Monnier
2019-09-02 13:43   ` tomas
2019-09-02 14:00     ` Andreas Röhler
2019-09-02 14:57       ` Stefan Monnier
2019-09-02 20:29         ` Andreas Röhler
2019-09-02 21:32           ` Stefan Monnier
2019-09-03  5:37             ` Andreas Röhler
  -- strict thread matches above, loose matches on Subject: below --
2018-11-22  4:42 replace " edgar
2018-11-22  5:18 ` Eric Abrahamsen
2018-11-22  5:39   ` Drew Adams
2018-11-22  6:04     ` Eric Abrahamsen
2018-11-22 13:19 ` Stefan Monnier
     [not found] ` <mailman.4442.1542892802.1284.help-gnu-emacs@gnu.org>
2018-11-23 12:05   ` Robert Munyer
2018-11-24  3:01 ` edgar
2018-11-24  5:27   ` Drew Adams
2018-11-24  7:22     ` edgar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).