From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: edgar@openmail.cc Newsgroups: gmane.emacs.help Subject: Re: replace element in list Date: Sat, 24 Nov 2018 03:01:18 +0000 Message-ID: <4e86b305ccbc341ee555657f0006d012@openmail.cc> References: <2087821187f90948d25d93c7ea475e66@openmail.cc> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit X-Trace: blaine.gmane.org 1543028497 10542 195.159.176.226 (24 Nov 2018 03:01:37 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 24 Nov 2018 03:01:37 +0000 (UTC) User-Agent: Roundcube Webmail/1.2.4 To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Nov 24 04:01:33 2018 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gQOCK-0002Zr-Sn for geh-help-gnu-emacs@m.gmane.org; Sat, 24 Nov 2018 04:01:33 +0100 Original-Received: from localhost ([::1]:55024 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gQOER-0005bk-5O for geh-help-gnu-emacs@m.gmane.org; Fri, 23 Nov 2018 22:03:43 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:41417) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gQODt-0005b7-Kx for help-gnu-emacs@gnu.org; Fri, 23 Nov 2018 22:03:13 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gQOD6-0000CN-2c for help-gnu-emacs@gnu.org; Fri, 23 Nov 2018 22:02:24 -0500 Original-Received: from onethreetwo.vfemail.net ([199.16.11.132]:47278 helo=vfemail.net) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gQOD5-0000BV-TL for help-gnu-emacs@gnu.org; Fri, 23 Nov 2018 22:02:20 -0500 Original-Received: (qmail 71546 invoked by uid 89); 24 Nov 2018 03:02:05 -0000 Original-Received: from localhost (HELO freequeue.vfemail.net) (127.0.0.1) by localhost with (DHE-RSA-AES256-SHA encrypted) SMTP; 24 Nov 2018 03:02:05 -0000 Original-Received: (qmail 70889 invoked by uid 89); 24 Nov 2018 03:01:48 -0000 Original-Received: by simscan 1.4.0 ppid: 70887, pid: 70888, t: 0.0023s scanners:none Original-Received: from unknown (HELO smtp102-2.vfemail.net) (172.16.100.62) by FreeQueue with SMTP; 24 Nov 2018 03:01:48 -0000 Original-Received: (qmail 26595 invoked by uid 89); 24 Nov 2018 03:01:18 -0000 Original-Received: by simscan 1.4.0 ppid: 26588, pid: 26591, t: 0.0498s scanners:none Original-Received: from unknown (HELO www.vfemail.net) (ZWRnYXJAb3Blbm1haWwuY2M=@172.16.100.93) by 172.16.100.62 with ESMTPA; 24 Nov 2018 03:01:18 -0000 Original-Received: from Uu7UWWzhRb4SY8nkitn3GEb36M+oQteBsoqDinoWc/6PVXPIAM4YFdkVMCPIKk3v via cmn8FKm5P2wSEAkB5Fr2S747NS7bKCQu by www.vfemail.net with HTTP (HTTP/1.1 POST); Fri, 23 Nov 2018 21:01:18 -0600 In-Reply-To: <2087821187f90948d25d93c7ea475e66@openmail.cc> X-Sender: edgar@openmail.cc X-detected-operating-system: by eggs.gnu.org: FreeBSD 8.x [fuzzy] X-Received-From: 199.16.11.132 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:118736 Archived-At: From Eric > You'll probably get a bunch of suggestions, but mine is to use `setf' > and `nth'. You can do: > > (let ((orig '(("a" . "b") ("c" "d"))) > (obj '("c" "d")) > (new '(":)"))) > (setf (nth (cl-position obj orig :test #'equal) orig) new) > orig) > > Hope that's useful. > > Eric Thank you, Eric. From Drew > (defun toto (xs old new) > (let ((ms (member old xs))) > (unless ms (error "%S is not in %S" old xs)) > (setcar ms new) > xs)) Thanks (I wonder why toto :P ). From Stefan > My suggestion is to not do it: > - if you do it by modifying the list in place, it means you're using > nasty side-effects, which are better avoided when possible > (especially with lists). > - if you want to do it without side-effects, your operation will > inevitably be algorithmically inefficient because a list is not > designed for that. > > > -- Stefan Thank you. From Robert > Edgar, here is one that avoids the nasty side-effects that Stefan > mentioned. It isn't especially efficient, but it is simple and clear. > Warning: it behaves the same as your original version _only_ if there > is exactly one matching item. > > (defun my-list-replace-2 (l old-item new-items) > (apply 'append > (mapcar (lambda (x) > (cond ((equal x old-item) new-items) > (t (list x)))) > l))) Thanks! This is great :). I didn't know that the first element would be replaced :S !! People, you are truly great. Thank you all. ------------------------------------------------- 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!