1.16 pattern example 11

Write pattern to select from list 1+a,2+a,-3+a] only those that have negative number added to \(a\) and output \(p(x)\) where \(x\) is that number.

In Maple, we use the form patmatch(X, conditional(patttern,condition))

L:=[1+a,2+a,-3+a]; 
map( proc(X) local la,x; 
     if patmatch(X, conditional(a+x::integer,x<0),'la') then 
        p(eval(x,la)); 
     else 
        X; 
     fi; 
     end proc, L 
    ); 
 
 
       [1 + a, 2 + a, p(-3)]
 

In Mathematica

{1+a,2+a,-3+a}/. (x_/;x<0)+a->p[x] 
 
   {1 + a, 2 + a, p[-3]}