Frank GRATKOWSKI

Hello
I try to create a function that substitutes more than one item at a time.
it should work like this
(listsubstitute ‘(1 2 3 4 5) ‘((1 8) (2 29))) => (8 29 3 4 5)
My attempt is:
(defun listsubstitute (l subs)
(loop for i in subl
for x = (substitute (first subl) (second subs) l) then (substitute (first subl) (second subs) x)
return x ))
Unfortunately list in the first inlet doesn’t get modified.
What am I doing wrong?
Thanks
Frank

fdsdb

Hi,
Perhaps this discussion (and resolution) could be useful for you:
http://forumnet.ircam.fr/usergroups/openmusic/forum/topic/maplists/
Best
fdsdb

Frank GRATKOWSKI

Thanks for the reply. Unfortunately it doesn’t solve my problem.
I would like to get the specific function I’m trying to program to work but it seems to be something wrong.
Using a recursion instead of the loop would work to but my skills are a bit to limited for that.

Frank GRATKOWSKI

Just in case, I did some mistakes in typing my function. This is the correct one.
(defun listsubstitute (l subl)
(loop for i in subl
for x = (substitute (first subl) (second subl) l) then (substitute (first subl) (second subl) x)
return x ))
I would still appreciate any help why it is not working. Also if there is a recursive solution instead…
Thanks Frank

Karim

Dear Frank,
Here is maybe what you are looking for :
(defun lstsubst (liste subst)
(let ((clone (clone liste)))
(loop for i in subst
do (nsubstitute (second i) (car i) clone))
clone))
(lstsubst ‘(1 2 3 4 5) ‘((1 8) (2 29)))
OM > (8 29 3 4 5)
The thing is to use nsubstitute instead of substitue, since the first function is a destructive one. That’s why i cloned the input list
Hope this helps
Best
K

Frank GRATKOWSKI

Dear Karim,
thanks a lot. It’s exactly what I was looking for.
best
frank
