# Converting prefix to infix

Is there a way in Mathematica to convert prefix to infix notation (where precedence order is preserved)?

For example, how would I convert S[a, S[b, c]] to a ~ S ~ (b ~ S ~ c)?

I checked the cookbook and also on line for "prefix to infix transformation". but did not find this topic.

• I noted that Infix[S[Infix[S[a, b]], c]] produces (a ~ S ~ b) ~ S ~ c So it's a matter of nesting the Infix operation in this way. Not sure how to achieve it in Mathematica. – Mike Feb 11 '19 at 21:43
• Perhaps via MapAll? But then only applied to non atomic (non leaf) parts? – Mike Feb 11 '19 at 21:49
• MapAll[Infix, S[a, S[b, c]]] gives Infix[a] ~ S ~ (Infix[b] ~ S ~ Infix[c]) so my remaining question is how to not apply Infix to a, b and c. – Mike Feb 11 '19 at 21:54

## 2 Answers

With[{S = Infix[S[##]] &}, S[a, S[b, c]] ]


a ~S~ (b ~S~ c)

Also

S[a, S[b, c]] /. S -> (Infix[S[##]] &)


a ~S~ (b ~S~ c)

• Thank you, that's helpful. I will go with the second option. – Mike Feb 11 '19 at 22:02
• Slightly neater is S -> Infix @* S. – Carl Woll Feb 13 '19 at 18:11
• @CarlWoll, yes!! Thank you. – kglr Feb 14 '19 at 18:54

You could just give S a format:

MakeBoxes[S[a_, b_], StandardForm] ^:= MakeBoxes[Infix[S[a, b]], StandardForm]


Then:

S[a, S[b, c]]


a ~S~ (b ~S~ c)

• Thanks much appreciated. – Mike Feb 13 '19 at 13:04