4
$\begingroup$

Suppose we have a function f with the Attribute Flat. From my understanding, the following command should return all possible ways of rewriting f[a,b,c] as a function of two arguments at all levels:

Attributes[f] = {Flat};
f[a,b,c] /. f[x_,y_] /; Print[{x,y}] :> Null;

This command evaluates to

{f[a],f[b,c]}
{f[a,b],f[c]}    
{a,f[b,c]}    
{f[a,b],c}    
{f[a],f[b]}    
{a,b}    
{f[b],f[c]}    
{b,c}

My question: why are the level 2 cases {a,f[b]}, {f[a],b}, {b,f[c]}, and {f[c],b} not included? Similarly, when the following command is executed,

Attributes[f] = {Flat};
f[a,b,c,d] /. f[x_,y_] /; Print[{x,y}] :> Null;

why does it return two instances of {f[a,b],f[c,d]}?

I am running Mathematica 11.2 on Ubuntu 18.04.

$\endgroup$
  • $\begingroup$ Can you use ReplaceList and see what happens? I’m worried the pattern matcher might be behaving a bit differently because of the Condition you threw in there... $\endgroup$ – QuantumDot Jul 20 '18 at 17:52
  • $\begingroup$ @QuantumDot ReplaceList returns only rewritings at level 1, i.e. the first four cases of the output of the first command in my question. $\endgroup$ – Praan Jul 21 '18 at 11:44
1
$\begingroup$

Please pardon this non-answer but it doesn't fit nicely in a comment.

In version 10.1 under Windows I get different results:

Attributes[f] = {Flat};

f[a, b, c] /. f[x_, y_] /; Print[{x, y}] :> Null;

{f[a],f[b,c]}

{f[a,b],f[c]}

{f[a],f[b]}

{f[b],f[c]}

f[a, b, c, d] /. f[x_, y_] /; Print[{x, y}] :> Null;

{f[a],f[b,c,d]}

{f[a,b],f[c,d]}

{f[a,b,c],f[d]}

{f[a],f[b,c]}

{f[a,b],f[c]}

{f[b],f[c,d]}

{f[b,c],f[d]}

{f[a],f[b]}

{f[b],f[c]}

{f[c],f[d]}

| improve this answer | |
$\endgroup$

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.