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

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

Re: replacing a certain element in a list with another


From: David Kastrup
Subject: Re: replacing a certain element in a list with another
Date: 30 Sep 2003 11:05:58 +0200
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50

Klaus Berndl <klaus.berndl@sdm.de> writes:

> Suppose i have the following list:
> 
> '(a b c d)
> 
> What is a good way to replace one of the list-elements with another, so foe
> example replacing the 'c with 'e?

> But: i do not want using any c-functions, so i would prefer a
> solution without using `substitute'!

Uh, what?

Anyhow, try

(let ((xxx '(a b c d)))
   (setcar (member 'c xxx) 'e)
   xxx)

> Is the solution easier if i always want to replace the last element
> of the list?

Not really.

> I think so, because it could be:
> 
> (defvar my-list '( a b c d))
> 
> (let ((temp-l (nreverse my-list)))
>   (setcar temp-l 'e)
>   (nreverse temp-l))
> 
> Is this a good elisp-solution? 

No.  Runs through the list quite more often than necessary, and
leaves my-list in a pretty undefined state.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum


reply via email to

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