Skip to main content
edited tags
Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Tweeted twitter.com/StackMma/status/671388908274499584
Source Link
Fred Simons
  • 10.2k
  • 19
  • 50

Orderless and Sequence

I just ran into the following subtlety.

Let us consider a function f with attribute Orderless.

Attributes[f]={Orderless};

For pattern matching, the consequence of this attribute is that when we have an expression with head f, any ordering of the arguments is tested. That can be seen in the following result:

ReplaceList[ f[a,b,c], f[x_,y_, z_] :> {x,y,z} ]

(* {{a,b,c},{a,c,b},{b,a,c},{b,c,a},{c,a,b},{c,b,a}} *)

I would have expected the same result from the next command, where in the rule I catch the three arguments of f in a BlankSequence, thereby placing a Sequence expression in the list at the right hand side:

 ReplaceList[ f[a,b,c], f[x__] :> {x} ]

(* {{a,b,c}} *)

It only gives one result! Likely, I overlooked something simple, but I fail to see a good explanation. Why does this not work?