< Back to IRCAM Forum

loop question

Hello

I try to create a function that substitutes more than one item at a time.

it should work like this
(list-substitute '(1 2 3 4 5) '((1 8) (2 29))) => (8 29 3 4 5)

My attempt is:

(defun list-substitute (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

Hi,

Perhaps this discussion (and resolution) could be useful for you:

http://forumnet.ircam.fr/user-groups/openmusic/forum/topic/map-lists/

Best

fdsdb

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.

Just in case, I did some mistakes in typing my function. This is the correct one.

(defun list-substitute (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

Dear Frank,

Here is maybe what you are looking for :

(defun lst-subst (liste subst)
(let ((clone (clone liste)))
(loop for i in subst
do (nsubstitute (second i) (car i) clone))
clone))

(lst-subst '(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

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