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

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

RE: replace element in list


From: Drew Adams
Subject: RE: replace element in list
Date: Wed, 21 Nov 2018 21:39:11 -0800 (PST)

> > (defun my-list-replace (obj orig new)
> >     "Replaces an element in a list with something else"
> >     (let* ((pos (cl-position orig obj :test 'equal))
> >            (pos (if pos pos 0))
> >            (objlen (length obj))
> >            (head (butlast obj (- objlen pos)))
> >            (trail (nthcdr (+ 1 pos) obj)))
> >       (append head (append new trail))))
> 
> (let ((orig '(("a" . "b") ("c" "d")))
>       (obj  '("c" "d"))
>       (new '(":)")))
>   (setf (nth (cl-position obj orig :test #'equal) orig) new)
>   orig)

(defun toto (xs old new)
  (let ((ms  (member old xs)))
    (unless ms (error "%S is not in %S" old xs))
    (setcar ms new)
    xs))



reply via email to

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