all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Thierry Volpiatto <thierry.volpiatto@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: How could I modify list element?
Date: Tue, 09 Dec 2008 18:21:53 +0100	[thread overview]
Message-ID: <87wse9clhq.fsf@thievol.homelinux.org> (raw)
In-Reply-To: 493E7D65.3020405@foxmail.com

richardeng <richardeng@foxmail.com> 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





  parent reply	other threads:[~2008-12-09 17:21 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-09 14:15 How could I modify list element? richardeng
2008-12-09 15:37 ` Alan Mackenzie
2008-12-09 17:43   ` richardeng
     [not found]   ` <mailman.2345.1228845668.26697.help-gnu-emacs@gnu.org>
2008-12-09 20:30     ` Andreas Politz
2008-12-09 17:21 ` Thierry Volpiatto [this message]
2008-12-09 17:26 ` Thierry Volpiatto
     [not found] <mailman.2319.1228836378.26697.help-gnu-emacs@gnu.org>
2008-12-09 16:36 ` Andreas Politz
2008-12-09 20:46 ` Niels Giesen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87wse9clhq.fsf@thievol.homelinux.org \
    --to=thierry.volpiatto@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.