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: Andreas Politz
Subject: Re: How could I modify list element?
Date: Tue, 09 Dec 2008 21:30:03 +0100
User-agent: Mozilla-Thunderbird 2.0.0.17 (X11/20081018)

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


reply via email to

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