Consider the following question:

Reap[a b c d /. (x_ a /; (! MemberQ[Sow[x], b])) :>  Subscript[a, x]]  
(* ===> {b Subscript[a, c d], {{b c d, b c, b d, c d}}} *)

In my thought, nothing will happen, because b is a Member of b c d.

I can understand MemberQ use b c d as its argument, but why MemberQ also test {b c, b d, c d}?

Consider another question:

Reap[a b c d /. (x_. a /; (! MemberQ[Sow[x], b])) :>  Subscript[a, x]]
(* ===> {{b c d Subscript[a, 1], {{b c d, 1}}}*)

Any idea?

  • 1
    $\begingroup$ But you get to see exactly what is being Sow[]-n, no? $\endgroup$ – J. M. will be back soon Oct 17 '15 at 4:01
  • 2
    $\begingroup$ This behavior is really related more with Times and not with MemberQ. Check for example this snippet a b c d /. x_ a /; Print[x] :> "never reached". Is its behavior clear to you? $\endgroup$ – jkuczm Oct 17 '15 at 13:15
  • 1
    $\begingroup$ It's due to Flat attribute of Times. Consider: SetAttributes[f, Flat]; f[a, b, c, d] /. f[a, x_] /; Print[x] :> "never reached" $\endgroup$ – jkuczm Oct 17 '15 at 14:23
  • 1
    $\begingroup$ Times has also Orderless attribute, which causes tests of all permutations of it's arguments. SetAttributes[g, Orderless]; g[a, b] /. g[x_, y_] /; Print[x, " ", y] :> "never reached" $\endgroup$ – jkuczm Oct 17 '15 at 14:29
  • 1
    $\begingroup$ Also Default of Times is 1 which is partly responsible for your last example. Check: Default[h] = 1; h[a] /. h[x_., y_] /; Print[x, " ", y] :> "never reached". Combination of Times attributes makes it really complicated when comes to understanding its pattern matching. $\endgroup$ – jkuczm Oct 17 '15 at 14:38

Your Answer

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

Browse other questions tagged or ask your own question.