From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Niels Giesen Newsgroups: gmane.emacs.help Subject: Re: How could I modify list element? Date: Tue, 09 Dec 2008 21:46:53 +0100 Organization: A noiseless patient Spider Message-ID: <87r64hqdoi.fsf@gmail.com> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1228858864 8324 80.91.229.12 (9 Dec 2008 21:41:04 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 9 Dec 2008 21:41:04 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Dec 09 22:42:08 2008 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1LAAKu-0002Ym-QI for geh-help-gnu-emacs@m.gmane.org; Tue, 09 Dec 2008 22:41:45 +0100 Original-Received: from localhost ([127.0.0.1]:47524 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LAAJj-0007gR-QY for geh-help-gnu-emacs@m.gmane.org; Tue, 09 Dec 2008 16:40:31 -0500 Original-Path: news.stanford.edu!newsfeed.stanford.edu!postnews.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!news.k-dsl.de!news.motzarella.org!motzarella.org!news.motzarella.org!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 38 Original-X-Trace: news.motzarella.org U2FsdGVkX19CUcWDPBXpu/W0LKX83g1yRVSy5+VUxX+rGsnwR9LkT7CkU8AKlksWRF0S4EaWzQvq0vMpDFA82fXADEzeWnDI/sIE4zyYt54gZNrFTFeWQgx47MM/aGz1EDx/5TbXP3s= Original-X-Complaints-To: Please send complaints to abuse@motzarella.org with full headers Original-NNTP-Posting-Date: Tue, 9 Dec 2008 20:46:54 +0000 (UTC) X-Auth-Sender: U2FsdGVkX1/2MupbCYX+J0QiWmTOi62M Cancel-Lock: sha1:nc+ODZNN3Q/5Ej0cP4qMJVOFG+s= sha1:V2rU+w4T3dgHekFkc5NiwDq7Nro= User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux) Original-Xref: news.stanford.edu gnu.emacs.help:165186 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:60516 Archived-At: richardeng writes: > Hi all, > setcar/setcdr is not convenient. > In a long list, ex. > (setq a '(a b c d e f g)) > I want to change 'e to 'E. > I need a function: (set-list-elt list old-elt new-elt) > > How? translate list to vector, modify, then turn it back??? Numerous ways, first the destructive ones: (setcar (member 'e a) 'E) (setcar (nthcdr 4 a) 'E) If (require 'cl) is no problem for you, nth is setfable (setf (nth 4 a) 'E) A DIY recursive function can do it too: (defun set-nth (n list new) (cond ((or (< n 0) (>= n (length list))) (error "out of bounds")) ((= n 0) (setcar list new)) (t (cons (car list) (set-nth (1- n) (cdr list) new))))) ... Or a purely functional approach, to leave the original list intact: (defun replace-nth (n list new) (cond ((or (< n 0) (>= n (length list))) (error "out of bounds")) ((= n 0) (cons new (cdr list))) (t (cons (car list) (replace-nth (1- n) (cdr list) new)))))