1.21 pattern example 16

Replace variable \(x\) in expression with a list.

L:=[x, x^2,y,z]: 
 
f:=proc(X::anything,x::symbol) 
    local la,y; 
 
    if patmatch(X, conditional( y::anything, _has(y,x) ),'la') then 
        eval(y,la); 
        eval(%,x={a,b}); 
     else 
        X; 
     fi; 
end proc; 
 
map(X->f(X,x),L); 
 
 
     [{a, b}, {a, b}^2, y, z]
 

Actually, we do not need patmatch for this, but the above is just an exercise. This can be done in one line in Maple as follows

L:=[x, x^2,y,z]: 
map(X->eval(X,x={a,b}),L) 
 
   [{a, b}, {a, b}^2, y, z]
 

In Mathematica

{x, x^2, y, z} /. x -> {a, b} 
 
   {{a, b}, {a^2, b^2}, y, z}