1.9 pattern example 4

Gives list {f(a,a),f(a,b),f(c,d),f(b,b)} return {g(a),f(a,b),f(c,d),g(b)} where only function that takes two arguments which are the same is replaced by \(g(x)\) where \(x\) here is the argument in the original list.

In Maple

restart; 
L:={f(a,a),f(a,b),f(c,d),f(b,b)}; 
map(X->[unassign('x'),`if`(patmatch(X,f(x::anything,x::anything),'la'),[assign(la),g(x)][],X)][],L) 
 
                 {f(a, b), f(c, d), g(a), g(b)}
 

Or using the long form of map, which is more clear, even though the code is longer

restart; 
L:={f(a,a),f(a,b),f(c,d),f(b,b)}; 
 
map(proc(X) local la; 
    if  patmatch(X,f('x'::anything,'x'::anything),'la') then 
        g(eval('x',la)); 
    else 
        X; 
    fi; 
    end proc, L 
); 
 
         {f(a, b), f(c, d), g(a), g(b)}
 

In Mathematica

L = {f[a, a], f[a, b], f[c, d], f[b, b]} 
L /. f[x_, x_] -> g[x] 
 
       {g[a], f[a, b], f[c, d], g[b]}