I have always thought that f@g
will give the same result as f[g]
in all cases, and it is just a matter of style which one to use and that g
will always evaluates first, and then f
will evaluate using the result of g
evaluation. I never thought that there can be any precedence issue here, since no one ever mentioned it in all the times I have been using Mathematica.
So I was really surprised when I found one case where this was not so. So my question is: How does one know when f@g
is not the same as f[g]
?
The help says nothing about this (thanks to chat room for giving me the link to this, I searched and could not find it)
http://reference.wolfram.com/mathematica/ref/Prefix.html
Even though one can see the word precedence
and grouping
but no explanation of where these are talked about and no more links to follow
Prefix[expr, h, precedence, grouping] can be used to specify how the output form should be parenthesized.
clearly this is a precedence issue. But I have never seen this mentioned before any where.
Tr[Times @@@ {{2, 3}, {4, 5}}]
Tr @ Times @@@ {{2, 3}, {4, 5}}
Tr @ ( Times @@@ {{2, 3}, {4, 5}} )
What seems to have happened is that in Tr@Times@@@....
the command Tr
grabbed Times
before Times
was applied. You can replaced Tr
by Total
also and see the same effect.
ps. This is another reason for me to not use @
too much. I I really never liked to use @
and always liked the good old fashioned []
as it seems clearer also, and now safer also.
question is: What is the rule(s) of thumb to use? One should always look ahead and check before using @
to make sure precedence is met? Any other cases than this one might have to watch out for? If there are very few cases, may be one can add them to their cheat sheet. Where are the precedence
of all operators listed so one can check?
Precedence
cannot be easily found in the documentation. $\endgroup$