4

This is an example from Leonid Shifrin's tutorial:

testexpr = Expand[(1 + x)^10]
newexpr = 
 Replace[testexpr, {(a : _ : 1)*x^(y_?EvenQ) :> a*f[x^y], 
   a_ /; FreeQ[a, x] :> f[a]}, 1]

Basic question: what does (a : _ : 1) mean and do?

GambitSquared
  • 2,311
  • 15
  • 23

1 Answers1

4

If you evaluate HoldForm@FullForm[a : _ : 1] you get Optional[Pattern[a,Blank[]],1]. This is a pattern object that will will be named a for later use and will be replaced by 1 if missing.

f[x] /. f[(a : _ : 1)] -> g[a]  (* g[x] *)
f[] /. f[(a : _ : 1)] -> g[a]  (* g[1] *)
Alan
  • 13,686
  • 19
  • 38