< Back to IRCAM Forum

Multiple substitution in a list

Hi,
I am trying to make a function that substitutes all the elements of an incoming list by a set of pairs of old and new elements.

incoming list: (1 2 3 1 2 2 1 3)
pairs of old and new elements: ((1 2)(2 3)(3 1))
=> (2 3 1 2 3 3 2 1)

It should not be able only swap the old and new elements, but also to substitute them
E.g ((1 2)(2 3)(3 4))
= (2 3 4 2 3 3 2 4)
and to substitute different old items by the same new item (and in reverse)
E.g. ((1 1)(2 1)(3 2))
=> (1 1 2 1 1 1 1 2)

It seems elementary, but when I tried to make it, it turns to be more complicated then I previously have been thinking.
[I was trying to make it by looping the substitute function (accumulation) and it works as long as the pairs of substitute items are of different types:
E.g. ((1 a)(2 b)(3 c))
=> (a b c a b b a c)
Yet, if the substitute pairs are numbers in the same range (or the ranges intersects), it can’t do the work!
Please, can you help me here?

Many greetings!

Aliser

Hi,

Perhaps Karim Haddad has resolved a similar question here: Map Lists. I hope this help,

Best

fdsdb

Dear fdsdb,

Thank you very much for your answer!
I checked the old post you pointed out to me and Karim’s solution does the job.
(I was checking the same topic before, but it would never occur to me to treat a list as a table in combination with cdr. Function cassq is a very simple and elegant solution. Bravo Karim!)
Thank you (and Karim as well) once more!

Aliser