6
$\begingroup$

I guess I've been using the Postfix notation // wrongly for some time now. I assumed it was a quick way to apply a function to all that precedes it. Now I was surprised by the fact that for example

a // f /. a -> 1

outputs

f[a]

I would have expected this to either be the same as (a // f) /. a ->1, or as applying the replacement rule first and then the function (a /. a -> 1) // f. However, both would output f[1].

Examining a // f with FullForm (and Hold) does not give me any insight to what is happening either.

The Postfix documentation is unfortunately very limited. This seems rather trivial, so sorry if this has been asked before, but I couldn't find any post about this.

$\endgroup$
2
  • 2
    $\begingroup$ Try evaluating this: FullForm[Hold[a // f /. a -> 1]], it should help (in general) understanding the order of operations Mathematica invokes. $\endgroup$
    – ktm
    Commented May 8, 2017 at 14:08
  • 1
    $\begingroup$ The wily programmer uses parentheses to exactly guarantee order of operations. I use "//" lots, and have been bitten by it before. $\endgroup$
    – MikeY
    Commented May 8, 2017 at 14:12

1 Answer 1

11
$\begingroup$

I would have expected this to either be the same as (a // f) /.a ->1, or as applying the replacement rule first and then the function (a /.a->1)//f

It is neither of those. It is a precedence issue. It is interpreted as

a // (f /. a -> 1)

You can look it up, or you can check it like this:

PrecedenceForm@HoldForm[a // f /. a -> 1]

(* (f /. a -> 1)[a] *)

Comment from @andre:

To see precedences you can also select a then extend the selection (with multi-click on a or with ctrl-shift-. on Windows (ctrl-. on Mac). Then observe the size of the blocks by wich the selection extends. Very useful, I use it all the time.

$\endgroup$
2
  • 4
    $\begingroup$ To see precedences you can also select a then extend the selection (with multi-click on a or with Ctrl-Shit-. on windows. Then observe the size of the blocks by wich the selection extends. Very usefull, I use it alt the time. $\endgroup$
    – andre314
    Commented May 8, 2017 at 14:11
  • $\begingroup$ @Szabolcs, thank you. I don't know why I didn't consider that possibility for precedence. Thanks also especially for including `PrecedenceForm@HoldForm'. $\endgroup$
    – Kvothe
    Commented May 8, 2017 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.