From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Robert Munyer Newsgroups: gmane.emacs.help Subject: Re: replace element in list Date: Fri, 23 Nov 2018 12:05:16 +0000 (UTC) Organization: Aioe.org NNTP Server Message-ID: References: <2087821187f90948d25d93c7ea475e66@openmail.cc> NNTP-Posting-Host: blaine.gmane.org X-Trace: blaine.gmane.org 1542974912 23825 195.159.176.226 (23 Nov 2018 12:08:32 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 23 Nov 2018 12:08:32 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Nov 23 13:08:28 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 1gQAG3-00066z-HR for geh-help-gnu-emacs@m.gmane.org; Fri, 23 Nov 2018 13:08:27 +0100 Original-Received: from localhost ([::1]:51826 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gQAIA-0004Kb-0F for geh-help-gnu-emacs@m.gmane.org; Fri, 23 Nov 2018 07:10:38 -0500 Original-Path: usenet.stanford.edu!goblin3!goblin2!goblin.stu.neva.ru!aioe.org!.POSTED!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 45 Original-NNTP-Posting-Host: rEuLJGYHN3ivvEZMZWpmJw.user.gioia.aioe.org Original-X-Complaints-To: abuse@aioe.org X-Notice: Filtered by postfilter v. 0.8.3 Original-Xref: usenet.stanford.edu gnu.emacs.help:224606 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:118735 Archived-At: > edgar wrote: >> (defun my-list-replace (obj orig new) >> "Replaces an element in a list with something else" [...] >> (my-list-replace '(("a" . "b") ("c" "d")) '("c" "d") (list '("hi"))) >> ;; (("a" . "b") ("hi")) If nothing in the list matches your "orig" item, your function will replace the _first_ item. Did you intend that? (my-list-replace '(("a" . "b") ("c" "d")) '("e" "f") (list '("hi"))) ;; (("hi") ("c" "d")) Stefan Monnier wrote: > - 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). Good point. > - if you want to do it without side-effects, your operation will > inevitably be algorithmically inefficient because a list is not > designed for that. If he doesn't want to run it very frequently nor on very long lists, moderate inefficiency is OK. 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))) -- Robert Munyer E-mail: (reverse (append '(com dot munyer at) (list (* 91837 99713))))