From: Zachary Turner
Subject: Any way to replace an element?
Date: 
Message-ID: <29462FA5ECBC932C.764EE00DACF93933.7A0467ECAEFEF505@lp.airnews.net>
Hi, I'm learning Lisp, and I'm wondering if Lisp supplies functions for
replacing elements of a list with another element.  I need two versions:
One that takes a list and two elements, and replaces all occurences of the
first element with the second.  Another version that takes a number n and an
element and replaces the nth element of the list with the element passed in.

Here are some examples

> (setf x '(a b c d e f g))
(a b c d e f g)

> (replace1 x c d)
(a b d d e f g)

> (replace2 x 4 g)
(a b c g e f g)

> (setf x '((1 . 2) (3 4 5 (6 7)) (9 . 10)))
((1 . 2) (3 4 5 (6 7)) (9 . 10))

(replace1 x '(9 . 10) '(a . b))
((1 . 2) (3 4 5 (6 7)) (a . b))

(replace2 x 2 'a)
((1 . 2) a (9 . 10))

Does Lisp have anything like this?  I can write one, but being that I don't
know alot of the cool features of the langauge yet, it's probably going to
be very inelegant, inefficient, and involve some kind of recursion, whereas
there's probably some way to do it using one of the mapxxxx functions.

Thanks for any help,

Zach

From: Coby Beck
Subject: Re: Any way to replace an element?
Date: 
Message-ID: <951507073620@NewsSIEVE.cs.bonn.edu>
Zachary Turner <·······@bindview.com> wrote in message
·······················································@lp.airnews.net...
> Hi, I'm learning Lisp, and I'm wondering if Lisp supplies functions for
> replacing elements of a list with another element.

subst

> I need two versions:
> One that takes a list and two elements, and replaces all occurences of the
> first element with the second.  Another version that takes a number n and
an
> element and replaces the nth element of the list with the element passed
in.

There may be a ready made one but with subst you could do:

(defun subst-nth-element(new n tree)
  (subst new (nth n tree) tree))

>
> Here are some examples
>
> > (setf x '(a b c d e f g))
> (a b c d e f g)
>
> > (replace1 x c d)
> (a b d d e f g)
>
> > (replace2 x 4 g)
> (a b c g e f g)
>
> > (setf x '((1 . 2) (3 4 5 (6 7)) (9 . 10)))
> ((1 . 2) (3 4 5 (6 7)) (9 . 10))
>
> (replace1 x '(9 . 10) '(a . b))
> ((1 . 2) (3 4 5 (6 7)) (a . b))
>
> (replace2 x 2 'a)
> ((1 . 2) a (9 . 10))
>
> Does Lisp have anything like this?  I can write one, but being that I
don't
> know alot of the cool features of the langauge yet, it's probably going to
> be very inelegant, inefficient, and involve some kind of recursion,
whereas
> there's probably some way to do it using one of the mapxxxx functions.
>
> Thanks for any help,
>
> Zach


(defun symbol-swap(frog prince alist)
"changes all frogs that occur in alist into princes. 'frog' and 'prince'
must be atoms.
Non-destructive. Returns the new list."
   (let ((the-car (car alist)) (the-cdr (cdr alist)))
      (cons
        (if (symbolp the-car)
           (if (equal the-car frog) prince the-car)
           (symbol-swap frog prince the-car))
        (if (symbolp the-cdr)
           (if (equal the-cdr frog) prince the-cdr)
           (symbol-swap frog prince the-cdr)))))


Coby
(digging up an old school assignment!)
From: Lieven Marchand
Subject: Re: Any way to replace an element?
Date: 
Message-ID: <897241$aaa$1@newnews1.news.nl.uu.net>
"Coby Beck" <·····@mercury.bc.ca> writes:

> There may be a ready made one but with subst you could do:
> 
> (defun subst-nth-element(new n tree)
>   (subst new (nth n tree) tree))
> 

Maybe I didn't understand the problem but NTH can be used as a place
for SETF.

(DEFUN SUBST-NTH-ELEMENT (NEW N TREE)
  (SETF (NTH TREE) NEW))

-- 
Lieven Marchand <···@bewoner.dma.be>
If there are aliens, they play Go. -- Lasker