1.22 pattern example 17

Replace \(sin\) by \(cos\)

f:=proc(X::anything) 
    local la,x; 
 
    if patmatch(X, conditional( x::anything, _has(x,sin) ),'la') then 
        eval(x,la); 
        eval(%,sin=cos); 
     else 
        X; 
     fi; 
end proc; 
 
L:=[sin(x), cos(x)+sin(2*x),tan(x)]: 
map(X->f(X), L); 
 
 
    [cos(x), cos(x) + cos(2*x), tan(x)]
 

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:=[sin(x), cos(x)+sin(2*x),tan(x)]: 
map(X->eval(X,sin=cos), L) 
 
 
    [cos(x), cos(x) + cos(2*x), tan(x)]
 

In Mathematica

{Sin[x], Cos[x] + Sin[2*x], Tan[x]} /. Sin -> Cos 
 
   {Cos[x], Cos[x] + Cos[2 x], Tan[x]}