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: Eric Abrahamsen
Subject: Re: replace element in list
Date: Wed, 21 Nov 2018 22:04:35 -0800
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Drew Adams <drew.adams@oracle.com> writes:

>> > (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))

Old school, and proper :)




reply via email to

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