From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Thierry Volpiatto Newsgroups: gmane.emacs.help Subject: Re: How could I modify list element? Date: Tue, 09 Dec 2008 18:21:53 +0100 Message-ID: <87wse9clhq.fsf@thievol.homelinux.org> References: <493E7D65.3020405@foxmail.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1228842819 15165 80.91.229.12 (9 Dec 2008 17:13:39 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 9 Dec 2008 17:13:39 +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 18:14:42 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 1LA68V-0001pE-6a for geh-help-gnu-emacs@m.gmane.org; Tue, 09 Dec 2008 18:12:39 +0100 Original-Received: from localhost ([127.0.0.1]:52602 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LA67K-0007Wi-25 for geh-help-gnu-emacs@m.gmane.org; Tue, 09 Dec 2008 12:11:26 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LA65o-0006Bh-PP for help-gnu-emacs@gnu.org; Tue, 09 Dec 2008 12:09:52 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1LA65n-0006Ab-8W for help-gnu-emacs@gnu.org; Tue, 09 Dec 2008 12:09:52 -0500 Original-Received: from [199.232.76.173] (port=54704 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LA65n-0006AT-4I for help-gnu-emacs@gnu.org; Tue, 09 Dec 2008 12:09:51 -0500 Original-Received: from main.gmane.org ([80.91.229.2]:57181 helo=ciao.gmane.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1LA65m-0001xY-Lg for help-gnu-emacs@gnu.org; Tue, 09 Dec 2008 12:09:51 -0500 Original-Received: from list by ciao.gmane.org with local (Exim 4.43) id 1LA65e-0006co-04 for help-gnu-emacs@gnu.org; Tue, 09 Dec 2008 17:09:42 +0000 Original-Received: from 49.78.88-79.rev.gaoland.net ([79.88.78.49]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 09 Dec 2008 17:09:41 +0000 Original-Received: from thierry.volpiatto by 49.78.88-79.rev.gaoland.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 09 Dec 2008 17:09:41 +0000 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 56 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 49.78.88-79.rev.gaoland.net User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux) Cancel-Lock: sha1:gbFAvYKTKmC9TgzbHfHHcyoFN1M= X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) 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:60505 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??? Hi, you can try that: ,---- | (defun tv-index-elt-list (elm lis) | "return index of `elm' in `lis' | `elm' is an element of the list `lis'" | (let ((n 0) | (index 0)) | (if (member elm lis) | (progn | (dolist (x lis) | (when (equal x elm) | (setq index n)) | (setq n (+ n 1))) | index) | (error "No element %s in %s" elm lis)))) ; TODO corriger pour CL | | | (defun tv-add-to-list-at-ind (elm lis where) | "Add `elm' in `lis' at index `where'" | (let ((cons-list (cons elm lis)) | (appended-list nil)) | (setq appended-list | (tv-move-element-in-list elm cons-list (+ 1 where))) | appended-list)) `---- ,---- | (setq A '(a b c d e f)) | (a b c d e f) | | (setq ind (tv-index-elt-list 'e A)) | 4 | | (setq A (remove 'e A)) | (a b c d f) | | (setq A (tv-add-to-list-at-ind 'E A 4)) | (a b c d E f) `---- -- A + Thierry Volpiatto Location: Saint-Cyr-Sur-Mer - France