Can C-like function call syntax be easily achieved for particular function names?
There is a handy package, Notation, built-in to Mathematica. With it, we can do the following:
Needs["Notation`"]
Notation[ParsedBoxWrapper[RowBox[{"sqrt", RowBox[{"(", "x_", ")"}]}]]
⟹
ParsedBoxWrapper[SqrtBox["x_"]]]
Notation[ParsedBoxWrapper[RowBox[{"sqr", RowBox[{"(", "x_", ")"}]}]]
⟹
ParsedBoxWrapper[SuperscriptBox["x_", "2"]]]
In the Mathematica Notebook interface, it looks like this (after Needs["Notation`"]
has been executed, and then you selected the Notation[...]
and pressed Ctrl+Shift+N):
After entering this code, we can simply do
d = sqrt(sqr(a)+2*a*R)
and get our output
Sqrt[a^2 + 2 a R]
Maybe you can use the fact that TraditionalForm
by default interprets parentheses as function calls:
fromCString[s_String] := ReplaceAll[
ToExpression[s, TraditionalForm, Defer],
{
sqrt -> Sqrt,
sqr -> square
}
]
Then:
fromCString["sqrt(sqr(x)+y*z)"]
Sqrt[square[x] + y z]