From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Eric Abrahamsen Newsgroups: gmane.emacs.help Subject: Re: replace element in list Date: Wed, 21 Nov 2018 21:18:22 -0800 Message-ID: <87zhu1y9k1.fsf@ericabrahamsen.net> References: <2087821187f90948d25d93c7ea475e66@openmail.cc> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1542863826 20404 195.159.176.226 (22 Nov 2018 05:17:06 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 22 Nov 2018 05:17:06 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Nov 22 06:17:02 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 1gPhMM-0005Be-1A for geh-help-gnu-emacs@m.gmane.org; Thu, 22 Nov 2018 06:17:02 +0100 Original-Received: from localhost ([::1]:44070 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gPhOR-0000tE-U1 for geh-help-gnu-emacs@m.gmane.org; Thu, 22 Nov 2018 00:19:11 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:52261) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gPhNx-0000t9-5N for help-gnu-emacs@gnu.org; Thu, 22 Nov 2018 00:18:41 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gPhNu-0001sS-17 for help-gnu-emacs@gnu.org; Thu, 22 Nov 2018 00:18:41 -0500 Original-Received: from [195.159.176.226] (port=42475 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gPhNr-0001hn-W6 for help-gnu-emacs@gnu.org; Thu, 22 Nov 2018 00:18:36 -0500 Original-Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1gPhLg-0004Ur-8n for help-gnu-emacs@gnu.org; Thu, 22 Nov 2018 06:16:20 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 38 Original-X-Complaints-To: usenet@blaine.gmane.org Cancel-Lock: sha1:5I8rknL3I3NuPbey11BH4nfkAls= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 195.159.176.226 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:118727 Archived-At: edgar@openmail.cc writes: > 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)) > )) 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