all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How could I modify list element?
@ 2008-12-09 14:15 richardeng
  2008-12-09 15:37 ` Alan Mackenzie
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: richardeng @ 2008-12-09 14:15 UTC (permalink / raw
  To: help-gnu-emacs

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???









^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How could I modify list element?
  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 17:21 ` Thierry Volpiatto
  2008-12-09 17:26 ` Thierry Volpiatto
  2 siblings, 2 replies; 8+ messages in thread
From: Alan Mackenzie @ 2008-12-09 15:37 UTC (permalink / raw
  To: richardeng; +Cc: help-gnu-emacs



On Tue, 9 Dec 2008, richardeng wrote:

> 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???

Try this (not tested):

(defun change-nth (liszt, n, nieuw)
   (while (> n 0)
     (setq liszt (cdr liszt)
           n (1- n)))
   (if liszt
      (setcar liszt nieuw)))

-- 
Alan Mackenzie (Nuremberg, Germany).





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How could I modify list element?
       [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
  1 sibling, 0 replies; 8+ messages in thread
From: Andreas Politz @ 2008-12-09 16:36 UTC (permalink / raw
  To: help-gnu-emacs

richardeng wrote:
> 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???
> 
>  

Why don't you ...

(let ((list  '(a b c d e)))
   (or
    (and (require 'cl nil t)
	(substitute 'E 'e list))
    (mapcar #'(lambda (elt)
	       (if (eq elt 'e) 'E elt))
	   list)))

-ap


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How could I modify list element?
  2008-12-09 14:15 How could I modify list element? richardeng
  2008-12-09 15:37 ` Alan Mackenzie
@ 2008-12-09 17:21 ` Thierry Volpiatto
  2008-12-09 17:26 ` Thierry Volpiatto
  2 siblings, 0 replies; 8+ messages in thread
From: Thierry Volpiatto @ 2008-12-09 17:21 UTC (permalink / raw
  To: help-gnu-emacs

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





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How could I modify list element?
  2008-12-09 14:15 How could I modify list element? richardeng
  2008-12-09 15:37 ` Alan Mackenzie
  2008-12-09 17:21 ` Thierry Volpiatto
@ 2008-12-09 17:26 ` Thierry Volpiatto
  2 siblings, 0 replies; 8+ messages in thread
From: Thierry Volpiatto @ 2008-12-09 17:26 UTC (permalink / raw
  To: help-gnu-emacs

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???

Sorry one func was missing in precedent post:

,----
| (defun tv-move-element-in-list (name-elm lis where)
|   "move element `name-elm' of list `lis' to index `where'
| in list `lis'.
| `name-elm' have the form of element in list.
| `lis' is a LIST
| `where' is an INTEGER"
|   (let* ((index-elm (tv-index-elt-list name-elm lis))
|          (start-part-list (subseq lis 0 where))
|          (mod-list (append (remove name-elm start-part-list)
|                            (cons name-elm
|                                  (remove name-elm (subseq lis where))))))
|     mod-list))
`----

-- 
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How could I modify list element?
  2008-12-09 15:37 ` Alan Mackenzie
@ 2008-12-09 17:43   ` richardeng
       [not found]   ` <mailman.2345.1228845668.26697.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 8+ messages in thread
From: richardeng @ 2008-12-09 17:43 UTC (permalink / raw
  To: Alan Mackenzie; +Cc: help-gnu-emacs

Alan Mackenzie wrote:
> 
> 
> On Tue, 9 Dec 2008, richardeng wrote:
> 
>> 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???
> 
> Try this (not tested):
> 
> (defun change-nth (liszt, n, nieuw)
>   (while (> n 0)
>     (setq liszt (cdr liszt)
>           n (1- n)))
>   (if liszt
>      (setcar liszt nieuw)))
> 
(setq aaa '(1 2 3 4 5 b a "ccc"))
(change-nth aaa 3 'BB)
aaa  --> (1 2 3 BB 5 b a "ccc")    , it works, thank you!





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How could I modify list element?
       [not found]   ` <mailman.2345.1228845668.26697.help-gnu-emacs@gnu.org>
@ 2008-12-09 20:30     ` Andreas Politz
  0 siblings, 0 replies; 8+ messages in thread
From: Andreas Politz @ 2008-12-09 20:30 UTC (permalink / raw
  To: help-gnu-emacs

richardeng wrote:
> Alan Mackenzie wrote:
>>
>>
>> On Tue, 9 Dec 2008, richardeng wrote:
>>
>>> 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???
>>
>> Try this (not tested):
>>
>> (defun change-nth (liszt, n, nieuw)
>>   (while (> n 0)
>>     (setq liszt (cdr liszt)
>>           n (1- n)))
>>   (if liszt
>>      (setcar liszt nieuw)))
>>
> (setq aaa '(1 2 3 4 5 b a "ccc"))
> (change-nth aaa 3 'BB)
> aaa  --> (1 2 3 BB 5 b a "ccc")    , it works, thank you!
> 
> 
> 

I guess I misunderstood. You can do it with nthcdr,
which is a built-in function.

(let ((l '(0 1 2 3 4 5 6)))
   (setcar (nthcdr 3 l) -3)
   l)

-ap


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How could I modify list element?
       [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
  1 sibling, 0 replies; 8+ messages in thread
From: Niels Giesen @ 2008-12-09 20:46 UTC (permalink / raw
  To: help-gnu-emacs

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???
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)))))



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2008-12-09 20:46 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

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.