Group Abstract Group Abstract

Message Boards Message Boards

0
|
5K Views
|
1 Reply
|
0 Total Likes
View groups...
Share
Share this post:

Use Flat attribute?

Posted 8 years ago

I thought that the "Flat" attribute of a function f unravels nested function applications: E.g.:

f[x1,f[x2]] ==> f[x1,x2]. But I may not have grasp the full meaning of it.

Can someone explain why the following:

ClearAll[f];

SetAttributes[f, Flat];

f[a_, b_] := a + b;

f[a, b]

gives the output:

f[a]+f[b]
POSTED BY: Daniel Huber
Posted 8 years ago

I think I found part of an explanation.

If a function f has the "Flat" attribute like in my example, Mathematica not only checks f[a,b] but also f[f[a],f[b]], f[a,f[b]]... Now it seems a question in which order these test are done.

If the definition: f[a, b] := a + b; is given before SetAttributes[f, Flat]; it looks like the definition is used before Flat goes to work. In the other case, the function head is first wrapped around the arguments and then tested.

There is also another player, namely the Attribute "OneIdentity". If this attribute is specified together with "Flat", it looks like all nested function applications are removed before testing (the thing I would have assumed at first).

All of this looks a bit foggy, if anybody has a concise explanation, I would be most grateful.

POSTED BY: Daniel Huber
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard