help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How could I modify list element?


From: Thierry Volpiatto
Subject: Re: How could I modify list element?
Date: Tue, 09 Dec 2008 18:21:53 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux)

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





reply via email to

[Prev in Thread] Current Thread [Next in Thread]