If I define (this is

w[x : Times[xs__]] := {x, xs}
w[x : h[xs__]] := {x, xs}

Definition[w] gives me

w[x : h[xs__]] := {x, xs}
w[x : xs__] := {x, xs}

and the Times got lost. The two equations got reversed —because the one involving the Times did not see it and is more general than the other.

Is this expected behaviour? The second equation given by Definition definitely does something difference from my first equation, as xs matches the product and not the sequence of its factors.

My guess is this comes from Times being Flat. How does one do what the above definition tried to do?

  • $\begingroup$ w[x_Times] := {x, Sequence @@ x} does seem to work. $\endgroup$ – Mariano Suárez-Álvarez Sep 21 '17 at 17:10
  • $\begingroup$ Similarly, something like w[x : (Times|ThisWillNeverMatch)[xs__]] := {x, xs} does do the trick (and ThisWillNeverMatch can be meaningfully replaced by Except[_], I guess) $\endgroup$ – Mariano Suárez-Álvarez Sep 21 '17 at 17:20
  • $\begingroup$ Times[xs__] immediately evaluates to Sequence[xs]. Try this: Clear@w; w[x : HoldPattern[Times[xs__]]] := {x, xs}; w[x : h[xs__]] := {x, xs} or this: Clear@w; SetAttributes[w, HoldAll]; w[x : Times[xs__]] := {x, xs}; w[x : h[xs__]] := {x, xs}. In either case, then do ?w. $\endgroup$ – march Sep 21 '17 at 17:27
  • $\begingroup$ Why is the pattern evaluated? $\endgroup$ – Mariano Suárez-Álvarez Sep 21 '17 at 17:31
  • 1
    $\begingroup$ I don't remember! $\endgroup$ – march Sep 21 '17 at 17:36


w[x : HoldPattern[Times[xs__]]] := {x, xs}
Definition @ w

w[x : HoldPattern[Times[xs__]]] := {x, xs}

w[a b c]

{a b c, a, b, c}

Does this approach work for you?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.