Search code examples
maxima

Subst in a list


I will give an example to explain my problem: I have a list in the form list1:[x*y*z,x*z,y]. Now I have another list, list2:[1,2,3]. What I'm trying to do is substitute the value list2[1], i.e. 1, into the x variable of my list1, the value list2[2] into the y variable of my list1, and so on. The output should be [1*2*3,1*3,2] or [6,3,2]. Note that both lists have the same length.

What I tried to do is:

list3:for i:1 thru length(list2) do (subst(list2[i],list1[i])

But it didn't work. How can I do this?

Thanks in advance,

Best regards.


Solution

  • (%i1) list1: [x*y*z,x*z,y] $
    (%i2) kk: ['x, 'y, 'z] $
    (%i3) vv: [ 1,  2,  3] $
    (%i4) sl: maplist(lambda([k, v], k = v), kk, vv) $
    (%i5) subst(sl, list1);
    (%o5)                              [6, 3, 2]