I'm a novice in using Mathematica.

Please can you explain why there are a lot of input forms in Mathematica such as standard, prefix, postfix and infix?

  • $\begingroup$ they all are for code structuring, but there are some difference in precedence of functions applyed in different forms. no matter what form is used mathematica converts it to standart form when parsing input sell $\endgroup$ – k_v Mar 19 '15 at 7:44

It's because there are a lot of forms in mathematics, and Mathematica tries to follow mathematical notation as far as possible.

Note that this is not entirely specific to Mathematica; for example most programming languages allow e.g. additions to be expressed as 2+3 rather than plus(2,3). Some programming languages also allow to overload existing operators, or even to add new operators.

Mathematica goes only a slight step further by allowing any function to be used in prefix, infix or postfix form, instead of requiring special syntax for the definition of prefix/infix/postfix operators.

For example, in mathematics, you write the cross product of vectors in infix notation ($a \times b$). In Mathematica, there's a function (Cross) for the cross product, so you can write Cross[a, b]. However you might prefer the mathematical infix notation. Now that could have been done with a special operator (the normal multiplication operator would not work, because it already has different semantics), but thanks to the general infix notation, you don't need that; you can simply write a ~Cross~ b and are already quite close to the mathematical notation.

| improve this answer | |

I'm going to recycle an example from Hadley Wickham that he wrote to explain the benefits of the pipe operator in the R language - https://twitter.com/AmeliaMN/status/748193609401327616

In his example he seeks to construct a program that implements the first stanza of the popular poem Little Bunny Foo Foo

"Little bunny Foo Foo
Went hopping through the forest
Scooping up the field mice
And bopping them on the head
Down came the Good Fairy, and she said
'Little bunny Foo Foo
I don't want to see you
Scooping up the field mice
And bopping them on the head.'"

The steps of the program are:

  • define a bunny called foo_foo
  • the bunny hops through the forest
  • the bunny scoops up a field mouse
  • the bunny bops the mouse on the head.

We could write this in the Wolfram Language with standardly constructed expressions as follows:

foo$foo = "little_bunny";
  hop$through[foo$foo, "forest"],

This is a little bit difficult to read, the first operation (hopping through the forest) is deeply nested in the expression. It might be more useful to re-write this in postfix notation - noting the requirement for pure functions in the post-fixed operations

foo$foo //
hop$through[#, "forest"] & //
scoop$up[#, "field_mouse"] & //
bop$on[#, "head"] &

The prefix operator doesn't provide us with a simple method to provide multiple arguments to function, see https://mathematica.stackexchange.com/a/5440/1952, so we can't use that construction.

The procedure can be written using infix notation as follows, but this is difficult to read:


Advice on Selecting Operator Forms

Postfixing operations makes expressions easy to read when there are a large number of expressions, but the last operation is at the end of the expression. For that reason, it is slightly more difficult to read an assignment that uses postfixing:

myGraph = 
 Apply[DirectedEdge, {{1, 2}, {2, 3}, {4, 1}, {5, 2}, {3, 5}}, 1] // Graph

Reading this left to write it first appears that we are assigning an object containing DirectedEdges to myGraph, it isn't until the end of the expression we discover that the object assigned to myGraph is actually a Graph. Prefix makes the head (form, or result) of an assignment immediately obvious:

myGraph = 
 Graph@Apply[DirectedEdge, {{1, 2}, {2, 3}, {4, 1}, {5, 2}, {3, 5}}, 

Often one will combine prefix and postfix together, for instance to make an assignment and compute how long it takes for the assignment to be made.

myGraph = 
   Graph@Apply[DirectedEdge, {{1, 2}, {2, 3}, {4, 1}, {5, 2}, {3, 5}},
      1]; // AbsoluteTiming

This demonstrates that postfix is very useful when you want to output an expression.

Infixing large expressions is a symptom of madness, see Is it possible to use infix notation ~ for functions with one argument or more than 2 arguments? for further details. Generally useful applications of Infix are as follows:


| improve this answer | |
  • 4
    $\begingroup$ "Infixing large expressions is a symptom of madness..." - :D $\endgroup$ – J. M.'s technical difficulties Jul 6 '16 at 13:32
  • 2
    $\begingroup$ "... but this is difficult to read." It should not be, as it is the closest to natural English language, and reads left-to-right in a straightforward fashion. However presenting it without any spacing makes it harder to read, justasitdoesinwrittenEnglish. Instead I recommend that one always offset operators with extra space like this: foo$foo ~hop$through~ "forest" ~scoop$up~ "field_mouse" ~bop$on~ "head". Incidentally what IMHO is madness is to try to force infix for other than binary (two parameter) operators. See:(87124) $\endgroup$ – Mr.Wizard Jul 6 '16 at 18:26
  • $\begingroup$ @J.M. johnhartstudios.com/wizardofidstrips/2016/february/… $\endgroup$ – Mr.Wizard Jul 6 '16 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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