46. Fixes

46.1 Prefix notation

I have normally written the name of the function to the left of the argument (input value), thus: $F (x)$. This is called prefix notation for functions and is familiar from analytic geometry and calculus texts.

46.1.1 Parentheses around the argument Trigonometric functions like $\sin x$ are also written in prefix notation, but it is customary to omit parentheses around the argument. (Pascal and many other computer languages require the parentheses, however, and Mathematica requires square brackets). Many mathematical writers omit the parentheses in other situations too, writing “$F x$” instead of “$F (x)$”. It is important not to confuse evaluation written like this with multiplication.

46.2 Infix notation

Many common binary operations are normally written between their two arguments, “$a +b$” instead of “$+(a,b)$”. This is called infix notation and naturally applies only to functions with two arguments.

46.2.1 Example The expression $3-(5+2)$ is in infix notation. In prefix notation, the same expression is $-(3,+(5,2))$.

46.3 Postfix notation

Some authors write functions on the right, for example “$xF$” or “$(x)F$” instead of “$F(x)$”. This is called postfix notation. This has real advantages which will become apparent when we look at composition in Chapter 98.

46.4 Polish notation

When binary operations are written in either prefix or postfix notation, parentheses are not necessary to resolve ambiguities. In infix notation, for example, parentheses are necessary to distinguish between “$a +b*c$” (which is the same as “$a + (b*c)$”) and “$(a +b )*c$”. In prefix notation, “$a +b*c$” can be written “$+\ a\ *\ b\ c$” and “$(a+b)*c$” can be written “$*\ +\ a\ b\ c$”. Note the use of spaces to separate the items. This is particularly important when multidigit constants are used: for example 35 22 + in postfix notation returns 57.

Writing functions of two or more arguments using prefix notation and no parentheses is called Polish notation after the eminent Polish logician Jan Łukasiewicz, who invented the notation in the 1920’s. Writing functions on the right which are normally infixed, without parentheses, is naturally called reverse Polish notation.

Most computer languages use prefix and infix notation similar to that of ordinary algebra. The language Lisp uses prefix notation (with parentheses) and the various dialects of Forth characteristically use reverse Polish notation (no parentheses). Either prefix or postfix notation in a computer language makes it easier to write an interpreter or compiler for the language.

46.4.1 Example The expression of Example 46.2.1 in prefix notation without using parentheses is $-\ 3\ +\ 5\ 2$. In postfix notation it is $3\ 5\ 2\ +\ -$.

46.4.2 Example $a+b+c$ in reverse Polish notation can be written either as $a\ b\ +\ c\ +$ or as $a\ b\ c\ +\ +$.

46.4.3 Exercise Write $(35+22)(6+5)$ in reverse Polish notation. Use $*$ for multiplication. (Answer on page 245.)

46.4.4 Exercise Write $b^{2} - 4ad$ in reverse Polish notation. Use $*$ for multiplication and don’t use exponents.

Fix notation in Mathematica Mathematica gives the user control over whether a function is written in infix notation or not. For example, we remarked in Section 14.4 that in Mathematica one writes Xor[p,q] for the expression $p \operatorname{XOR} q$. However, by putting tildes before and after the name of a function in Mathematica, you can use it as an infix; thus you can write p ~Xor~ q instead of for Xor[p,q].

A function F can be used in postfix form by prefixing it with //. For example, one can write Sqrt[2] or 2 // Sqrt.

Pages 68–69 of the book (PDF).